import std.algorithm;
import std.array;
import std.range;
import std.stdio;
immutable int NA = -1;
auto mult (int [] a, int [] b)
{
auto n = a.length;
auto res = new int [n];
foreach (i; 0..n)
{
if (b[i] != NA)
{
res[i] = a[b[i]];
}
}
return res;
}
void main ()
{
immutable int n = 14;
auto b = n.iota.array;
foreach (j; 0..2)
{
bringToFront (b[j * 7 + 0..j * 7 + 1],
b[j * 7 + 1..j * 7 + 7]);
}
debug {writeln (b);}
auto a = new int [n];
a[] = NA;
auto u = new bool [n];
bool check ()
{
int num1 = 0;
int num3 = 0;
foreach (i; 0..n)
{
if (a[i] != NA)
{
if (a[i] == i)
{
num1 += 1;
if (num1 > 2)
{
return false;
}
}
else
{
if (a[a[i]] != NA)
{
if (a[a[i]] == i)
{
return false;
}
else if (a[a[a[i]]] != NA)
{
if (a[a[a[i]]] == i)
{
num3 += 1;
if (num3 > 4)
{
return false;
}
}
else
{
return false;
}
}
}
}
}
}
auto ab = mult (a, b);
foreach (i; 0..n)
{
if (ab[i] != NA)
{
if (ab[i] == i)
{
return false;
}
if (ab[ab[i]] != NA)
{
if (ab[ab[i]] != i)
{
return false;
}
}
}
}
return true;
}
void recur (int k)
{
if (k == n)
{
writeln (a);
return;
}
debug {writeln (a);}
if (!check ())
{
return;
}
foreach (i; 0..14)
{
if (!u[i])
{
u[i] = true;
a[k] = i;
recur (k + 1);
u[i] = false;
}
}
a[k] = NA;
}
recur (0);
}
aW1wb3J0IHN0ZC5hbGdvcml0aG07CmltcG9ydCBzdGQuYXJyYXk7CmltcG9ydCBzdGQucmFuZ2U7CmltcG9ydCBzdGQuc3RkaW87CgppbW11dGFibGUgaW50IE5BID0gLTE7CgphdXRvIG11bHQgKGludCBbXSBhLCBpbnQgW10gYikKewoJYXV0byBuID0gYS5sZW5ndGg7CglhdXRvIHJlcyA9IG5ldyBpbnQgW25dOwoJZm9yZWFjaCAoaTsgMC4ubikKCXsKCQlpZiAoYltpXSAhPSBOQSkKCQl7CgkJCXJlc1tpXSA9IGFbY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