#include <algorithm>
#include <iostream>
#include <istream>
#include <queue>
#include <limits>

using std::cin,std::cout;

const int MAX_N {(int)2e5+5};
int n;
int next[MAX_N];
int in_degree[MAX_N];
bool vis[MAX_N];
std::queue<int> q;
int ans {std::numeric_limits<int>::max()};

int main(){
    std::iostream::sync_with_stdio(false),cin.tie(0),cout.tie(0);

    std::cin>>n;
    for(int i=1;i<=n;i++){
        cin>>next[i];
        in_degree[next[i]]++;
    }
    const auto push_vis = [](int n){
        q.push(n);
        vis[n]=true;
    };
    for(int i=1;i<=n;i++){
        if(in_degree[i]<=0){
            push_vis(i);
        }
    }
    while(q.empty()==false){
        int front = q.front();
        q.pop();
        if(--in_degree[next[front]]<=0){
            push_vis(next[front]);
        }
    }
    for(int i=1;i<=n;i++){
        if(vis[i])continue;
        int loop_size {0};
        int start {i};
        int now {start};
        do{
            vis[now]=true;
            now=next[now];
            loop_size++;
        }while(now!=start);
        ans=std::min(ans,loop_size);
    }
    std::cout<<ans<<"\n";
}