1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < b;i ++) 3 #define _rep(i,a,b) for(int i = (a);i > b;i --) 4 #define INF 0x3f3f3f3f 5 #define pb push_back 6 typedef long long ll; 7 using namespace std; 8 inline ll read() 9 { 10 ll ans = 0; 11 char ch = getchar(), last = ' '; 12 while(!isdigit(ch)) last = ch, ch = getchar(); 13 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 14 if(last == '-') ans = -ans; 15 return ans; 16 } 17 inline void write(ll x) 18 { 19 if(x < 0) x = -x, putchar('-'); 20 if(x >= 10) write(x / 10); 21 putchar(x % 10 + '0'); 22 } 23 24 int e[200003]; 25 int state[200003]; 26 int m[200003]; 27 int vis[200003]; 28 int st; 29 int N; 30 int rnt = 0x3f3f3f3f; 31 void dfs(int nw) 32 { 33 if(vis[nw]) 34 return ; 35 vis[nw] = 1; 36 if(state[e[nw]]==-1) 37 { 38 rnt = min(rnt,m[nw]-m[e[nw]]+1); 39 return ; 40 } 41 state[nw] = -1; 42 m[e[nw]] = m[nw]+1; 43 dfs(e[nw]); 44 state[nw] = 0; 45 } 46 int main() 47 { 48 // freopen("testdata.txt","w+",stdin); 49 N = read(); 50 _for(i,1,N+1) 51 e[i] = read(); 52 _for(i,1,N+1) 53 { 54 if(!vis[i]) 55 dfs(i); 56 } 57 write(rnt); 58 return 0; 59 }