import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random, core.bitop; import std.string, std.regex, std.conv, std.stdio, std.typecons; void main() { auto n = readln.chomp.to!size_t; auto ai = iota(n).map!(_ => readln.chomp.to!size_t).array; auto g = new MatGraph(n * 2 + 2); foreach (i; 0..n) { g[n * 2, i] = 1; g[i + n, n * 2 + 1] = 1; foreach (j; 0..n) if (j != ai[i]) g[i, j + n] = 1; } auto r = g.fordFulkerson(n * 2, n * 2 + 1); auto s = new size_t[](n); foreach (i; 0..n) { auto t = r.v[i].countUntil!(a => a != 0 && !isInf(a)); if (t < 0) { writeln(-1); return; } s[i] = t - n; } s.each!(writeln); } template Graph(T) { T inf = -1; auto isInf(T val) { return val == inf; } class Mat { size_t n; // size T[][] v; // values this(size_t n) { this.n = n; v = new T[][](n, n); } T opIndex(size_t i, size_t j) { return v[i][j]; } T opIndexAssign(T w, size_t i, size_t j) { v[i][j] = w; return w; } } class MatGraph : Mat { this(size_t n) { super(n); foreach (i; 0..n) v[i][] = inf; foreach (i; 0..n) v[i][i] = 0; } auto bfs(size_t s, size_t t) { auto visited = new bool[](n); visited[s] = true; auto si = new DList!(size_t[]); si.insertBack([s]); while (!si.empty) { auto vs = si.front; si.removeFront; foreach (i; 0..n) if (!isInf(this[vs[$-1], i]) && !visited[i]) { if (i == t) { return vs ~ i; } else { visited[i] = true; si.insertBack(vs ~ i); } } } return cast(size_t[])[]; } auto fordFulkerson(size_t s, size_t t) { auto c = new MatGraph(n); auto r = new MatGraph(n); foreach (i; 0..n) r.v[i][] = v[i][]; auto vs = r.bfs(s, t); while (vs.length > 0) { auto vn = vs.length - 1; auto minW = T.max; foreach (i; 0..vn) minW = min(minW, r[vs[i], vs[i+1]]); foreach (i; 0..vn) { auto u = vs[i], v = vs[i+1]; if (!isInf(this[u, v])) { if (isInf(c[u, v])) c[u, v] = minW; else c[u, v] = c[u, v] + minW; } else { c[v, u] = c[v, u] - minW; if (c[v, u] == 0) c[v, u] = inf; } if (isInf(r[v, u])) r[v, u] = minW; else r[v, u] = r[v, u] + minW; r[u, v] = r[u, v] - minW; if (r[u, v] == 0) r[u, v] = inf; } vs = r.bfs(s, t); } return c; } } } mixin Graph!int;