// Source code from laekov at noip 2015 day1
#define PRID “message”
#include
using namespace std;
const int maxn = 200003;
int ans, n, t[maxn], d[maxn], v[maxn];
int walk(int po) { if (d[po]) return n; int p(po); d[p] = 1; v[p] = po; while (p) if (!d[t[p]]) { d[t[p]] = d[p] + 1; v[t[p]] = po; p = t[p]; } else if (v[t[p]] == po) return d[p] - d[t[p]] + 1; else return n; return n; }
int main() { freopen(PRID “.in”, “r”, stdin); freopen(PRID “.out”, “w”, stdout);
scanf("%d", &n);
for (int i = 1; i <= n; ++ i)
scanf("%d", t + i);
memset(v, 0, sizeof(v));
memset(d, 0, sizeof(d));
ans = n;
for (int i = 1, g; i <= n; ++ i)
if ((g = walk(i)) < ans)
ans = g;
printf("%d\n", ans);
}