int f[N], d[N], n, minn = N, last; intfa(int x){ if (f[x] != x) { int last = f[x]; f[x] = fa(f[x]); d[x] += d[last]; } return f[x]; }
voidcheck(int a, int b){ int x = fa(a), y = fa(b); if (x != y) {f[x] = y; d[a] = d[b] +1;} else minn = min(minn, d[a] + d[b] + 1); } intmain(){ scanf("%d", &n); for (int i = 1; i <= n; i++) f[i] = i; for (int i = 1; i <= n; i++) { int y; scanf("%d", &y); check(i, y); } printf("%d\n",minn); }