#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll gcd(ll a, ll b){
    while(b){
        ll t = a % b;
        a = b;
        b = t;
    }
    return a;
}

ll lcm(ll a, ll b){
    return (a / gcd(a, b)) * b;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<int> p(n + 1);
    vector<vector<int>> graph(n + 1);

    for (int i = 1; i <= n; i++) {
        cin >> p[i];

        // Make the graph undirected so BFS finds the whole component
        graph[i].push_back(p[i]);
        graph[p[i]].push_back(i);
    }

    vector<int> vis(n + 1, 0);
    ll ans = 1;

    for (int i = 1; i <= n; i++) {
        if (vis[i]) continue;

        queue<int> q;
        q.push(i);
        vis[i] = 1;

        int sz = 0;

        while (!q.empty()) {
            int u = q.front();
            q.pop();
            sz++;

            for (int v : graph[u]) {
                if (!vis[v]) {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }

        ans = lcm(ans, (ll)sz);
    }

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