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

using std::cin,std::cout;
constexpr const auto range = std::ranges::views::iota;

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

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

    cin>>n;
    for(const int i:range(1,n+1)){
        cin>>next[i];
        in_degree[next[i]]++;
    }

    const auto push_vis = [](const int n){
        q.push(n);
        vis[n]=true;
    };
    for(const int i:range(1,n+1)){
        if(in_degree[i]==0){
            push_vis(i);
        }
    }

    while(q.empty()==false){
        int front =  q.front();
        q.pop();
        in_degree[next[front]]--;
        if(in_degree[next[front]]==0){
            push_vis(next[front]);
        }
    }

    for(int i:range(1,n+1)){
        if(vis[i])continue;
        const int start {i};
        int now {start};
        int ring_size {0};
        do{
            vis[now]=true;
            ring_size++;
            now = next[now];
        }while(now!=start);
        ans = std::min(ans,ring_size);
    }

    cout<<ans<<"\n";
} 