#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <set>
#include <utility>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <algorithm>

using namespace::std;

int64_t gcd(int64_t x, int64_t y)
{
	if (y == 0)
		return x;

	return ((x % y) == 0 ? y : gcd(y, x % y));
}

int64_t lcm(int64_t x, int64_t y)
{
	int64_t d = gcd(x, y);
	return (x/d) * y;
}

int main()
{
	int n;
	cin >> n;

	vector<int> f(1 + n, 0);
	for (int i = 1; i <= n; i++)
		cin >> f[i];

	int64_t ans = 1;
	for (int i = 1; i <= n; i++)
	{
		map<int, int> mp;
		int curr = i;
		int idx = 0;

		while (true)
		{
			int x = f[curr];
			if (x == i)
			{
				ans = lcm(ans, idx + 1);
				break;
			}

			if (mp.find(x) == mp.end())
			{
				mp[x] = idx++;
				curr = x;
			}
			else
			{
				int len = idx - mp[x];
				int st = idx - len + 1;

				int d = st / len;
				if (st % len)
					d++;

				ans = lcm(ans, d * len);
				break;
			}
		}
	}

	std::cout << ans << std::endl;

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <set>
#include <utility>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <algorithm>

using namespace::std;

int64_t gcd(int64_t x, int64_t y)
{
	if (y == 0)
		return x;

	return ((x % y) == 0 ? y : gcd(y, x % y));
}

int64_t lcm(int64_t x, int64_t y)
{
	int64_t d = gcd(x, y);
	return (x/d) * y;
}

int main()
{
	int n;
	cin >> n;

	vector<int> f(1 + n, 0);
	for (int i = 1; i <= n; i++)
		cin >> f[i];

	int64_t ans = 1;
	int mx = 1;

	for (int i = 1; i <= n; i++)
	{
		map<int, int> mp;
		int curr = i;
		int idx = 0;

		while (true)
		{
			int x = f[curr];
			if (x == i)
			{
				ans = lcm(ans, idx + 1);
				break;
			}

			if (mp.find(x) == mp.end())
			{
				mp[x] = idx++;
				curr = x;
			}
			else
			{
				int len = idx - mp[x];
				int st = idx - len + 1;

				int d = st / len;
				if (st % len)
					d++;

				if (len > 1)
					ans = lcm(ans, d * len);
				else
					mx = max(mx, d * len);

				break;
			}
		}
	}

	int d = mx / ans;
	if (mx % ans)
		d++;

	ans *= d;
	std::cout << ans << std::endl;

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include <vector>
#include <deque>
#include <list>
#include <queue>
#include <set>
#include <utility>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <algorithm>

using namespace::std;

int64_t gcd(int64_t x, int64_t y)
{
	if (y == 0)
		return x;

	return ((x % y) == 0 ? y : gcd(y, x % y));
}

int64_t lcm(int64_t x, int64_t y)
{
	int64_t d = gcd(x, y);
	return (x/d) * y;
}

int main()
{
	int n;
	cin >> n;

	vector<int> f(1 + n, 0);
	for (int i = 1; i <= n; i++)
		cin >> f[i];

	int ans = 1;

	map<int, int> mp;
	int st = 1;

	for (int i = 1; i <= n; i++)
	{
		int x = f[i];
		if (x == st)
		{
			ans = lcm(ans, i - st + 1);
			st = 1 + i;

			continue;
		}

		if (mp.find(x) == mp.end())
			mp[x] = i;
		else
		{
			int t = i - mp[x];
			int u = 1 + mp[x];

			for (int j = st; j <= i; j++)
			{
				int s = t;
				while ((j + s) < u)
					s += t;

				ans = lcm(ans, s);
			}

			st = 1 + i;
		}
	}

	std::cout << ans << std::endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define For(i,a,b) for(long long i=a;i<b;i++)
#define pb push_back
#define mod 1000000007
#define reset(s,val) memset(s,val,sizeof(s))
#define eps 1e-12
#define pi acos(-1)
#define sqr(x) (x)*(x)
#define two(x) (1<<(x))

long long n,chk[222],vst[222],f[222],cur,cap[222],range,ans,ori;

int main( ){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    cin>>n;
    For(i,1,n+1) cin>>f[i];
    reset(vst,0);
    reset(cap,-1);
    For(i,1,n+1) if(!vst[i])
    {
        reset(chk,-1);
        chk[i]=0;
        cur=i;
        For(cnt,1,222)
        {
            vst[cur]=1;
            cur=f[cur];
            if(~chk[cur])
            {
                range=cnt-chk[cur];
                while(chk[cur]%range!=0) chk[cur]++;
                cap[range]=max(cap[range],chk[cur]);
                break;
            }
            else
            {
                chk[cur]=cnt;
            }
        }
    }
    ori=1;
    For(i,2,222) if(~cap[i])
    {
        ori/=__gcd(ori,i);
        ori*=i;
    }
    ans=ori;
    For(i,1,222) if(~cap[i])
    {
        while(ans<cap[i]) ans+=ori;
    }
    cout<<ans;
}


#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <istream>
#include <ostream>
#include <deque>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <utility>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define MOD 1000000007
#define MOD9 1000000009
#define INF 2000000000
#define INFFW 1000000000
#define INFLL 9000000000000000000LL
#define INFLLFW 4500000000000000000LL
#define EPS 1e-7
#define debug puts("ganteng")
#define FOR(i, a, b) for (int i=a; i<b; i++)
#define FORR(i, n) FOR(i, 0, n)
#define REV(i, a, b) for (int i=a-1; i>=b; i--)
#define REVV(i, n) REV(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define UNIK(x) unique(ALL(x) - x.begin())
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define sqr(x) (LL)x * (LL)x
#define SYNC ios_base::sync_with_stdio(0), cin.tie(0)
#define TLE using
#define RTE namespace
#define WA std

TLE RTE WA;

typedef long long LL;
typedef pair <int, int> pii;
typedef pair <int, pii> pipii;

typedef vector <int> vi;
typedef vector <pii> vii;
typedef pair <LL, LL> pll;
typedef vector <LL> vl;
typedef vector <pll> vll;

//////// End of Template ////////

int n;
int a[202];
int cycle[202];

LL gcd(int x, int y) {
    return y ? gcd(y, x % y) : x;
}

int main () {

    scanf("%d", &n);
    //FORR(i, n) printf("%d ", i+1); printf("\n");
    FOR(i, 1, n+1) scanf("%d", &a[i]);

    int mulai = 0;
    LL kpk = 1;

    FOR(i, 1, n+1) {
        int now = i;
        int len = 0;

        memset(cycle, -1, sizeof cycle);

        while (cycle[now] == -1) {
            //cout << now << " ";
            cycle[now] = len;
            len++;
            now = a[now];
        } //cout << endl;

        //printf("%d %d\n", cycle[now], len-cycle[now]);

        MAX(mulai, cycle[now]);
        kpk = (LL) (len - cycle[now]) * kpk / gcd(kpk, len - cycle[now]);
    }

    LL ans = kpk;
    while (ans < mulai) ans += kpk;

    printf("%I64d\n", ans);

    return 0;
}


#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <istream>
#include <ostream>
#include <deque>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <utility>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define MOD 1000000007
#define MOD9 1000000009
#define INF 2000000000
#define INFFW 1000000000
#define INFLL 9000000000000000000LL
#define INFLLFW 4500000000000000000LL
#define EPS 1e-7
#define debug puts("ganteng")
#define FOR(i, a, b) for (int i=a; i<b; i++)
#define FORR(i, n) FOR(i, 0, n)
#define REV(i, a, b) for (int i=a-1; i>=b; i--)
#define REVV(i, n) REV(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define UNIK(x) unique(ALL(x) - x.begin())
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define sqr(x) (LL)x * (LL)x
#define SYNC ios_base::sync_with_stdio(0), cin.tie(0)
#define TLE using
#define RTE namespace
#define WA std

TLE RTE WA;

typedef long long LL;
typedef pair <int, int> pii;
typedef pair <int, pii> pipii;

typedef vector <int> vi;
typedef vector <pii> vii;
typedef pair <LL, LL> pll;
typedef vector <LL> vl;
typedef vector <pll> vll;

//////// End of Template ////////

int n;
int a[202];
int cycle[202];

LL gcd(LL x, LL y) {
    return y ? gcd(y, x % y) : x;
}

int main () {

    scanf("%d", &n);
    //FORR(i, n) printf("%d ", i+1); printf("\n");
    FOR(i, 1, n+1) scanf("%d", &a[i]);

    int mulai = 0;
    LL kpk = 1;

    FOR(i, 1, n+1) {
        int now = i;
        int len = 0;

        memset(cycle, -1, sizeof cycle);

        while (cycle[now] == -1) {
            //cout << now << " ";
            cycle[now] = len;
            len++;
            now = a[now];
        } //cout << endl;

        //printf("%d %d\n", cycle[now], len-cycle[now]);

        MAX(mulai, cycle[now]);
        kpk = (LL) (len - cycle[now]) * kpk / gcd(kpk, len - cycle[now]);
    }

    LL ans = kpk;
    while (ans < mulai) ans += kpk;

    printf("%I64d\n", ans);

    return 0;
}


#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <istream>
#include <ostream>
#include <deque>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <utility>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define endl '\n'
#define MOD 1000000007
#define MOD9 1000000009
#define INF 2000000000
#define INFFW 1000000000
#define INFLL 9000000000000000000LL
#define INFLLFW 4500000000000000000LL
#define EPS 1e-7
#define debug puts("ganteng")
#define FOR(i, a, b) for (int i=a; i<b; i++)
#define FORR(i, n) FOR(i, 0, n)
#define REV(i, a, b) for (int i=a-1; i>=b; i--)
#define REVV(i, n) REV(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define UNIK(x) unique(ALL(x) - x.begin())
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define sqr(x) (LL)x * (LL)x
#define SYNC ios_base::sync_with_stdio(0), cin.tie(0)
#define TLE using
#define RTE namespace
#define WA std

TLE RTE WA;

typedef long long LL;
typedef pair <int, int> pii;
typedef pair <int, pii> pipii;

typedef vector <int> vi;
typedef vector <pii> vii;
typedef pair <LL, LL> pll;
typedef vector <LL> vl;
typedef vector <pll> vll;

//////// End of Template ////////

int n;
int a[202];
int cycle[202];

LL gcd(int x, int y) {
    return y ? x : gcd(y, x % y);
}

int main () {

    scanf("%d", &n);
    FOR(i, 1, n+1) scanf("%d", &a[i]);

    int mulai = 0;
    LL kpk = 1;

    FOR(i, 1, n+1) {
        int now = i;
        int len = 0;

        memset(cycle, -1, sizeof cycle);

        while (cycle[now] == -1) {
            cycle[now] = len;
            len++;
            now = a[now];
        }

        //printf("%d %d\n", cycle[now], len-cycle[now]);

        MAX(mulai, cycle[now]);
        kpk = (LL) (len - cycle[now]) * kpk / gcd(kpk, len - cycle[now]);
    }

    LL ans = kpk;
    while (ans < mulai) ans += kpk;

    printf("%I64d\n", ans);

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <memory.h>
#include <string.h>
using namespace std;

#define MAX 400

int f[MAX],g[MAX],gg[MAX];

int N;

void process()
{
    int i,j,k;
    int ans;

    for ( i = 1 ; i <= N ; i++)g[i] = f[i];

    for ( ans = 1 ;; ans++)
    {
        for ( i = 1 ; i <= N ; i++)
        {
            if(g[i] != g[g[i]])break;
        }
        if(i > N)break;

        for ( i = 1 ; i <= N ; i++)
        {
            gg[i] = f[g[i]];
        }
        for ( i = 1 ; i <= N ; i++)g[i] = gg[i];
    }
    printf("%d\n",ans);
}

int main()
{
    int i,j,k;
    //freopen("in.txt","r",stdin);

    scanf("%d", &N);

    for ( i = 1 ; i <= N ; i++) scanf("%d" , f + i);

    process();

    return 0 ;
}


#include <stdio.h>

int n, f[205], g[205], h[205];

bool check(){
    for(int i=0; i<n; i++){
        if(f[f[i]]!=f[i]) return false;
    }
    return true;
}

int main(){
    scanf("%d", &n);
    for(int i=0; i<n; i++) {scanf("%d", &f[i]); f[i]--; g[i]=f[i];}
    int ans = 1;
    while(!check()){
        ans++;
        for(int i=0; i<n; i++) h[i] = g[f[i]];
        for(int i=0; i<n; i++) f[i] = h[i];
    }
    printf("%d\n", ans);
}


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
typedef long long ll;
#define max(a,b) (a>b?a:b)
#define M 301
ll a[M];
int f[M],num[M];

ll gcd(ll x,ll y){
	if(!y) return x;
	return gcd(y,x%y);
}

ll lcm(ll x,ll y){
	return 1ll*x/gcd(x,y)*y;
}

int main(){
	//freopen("c.in","r",stdin);
	//freopen("in.txt","r",stdin);
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&f[i]);
	int mi=0;
    for(int i=1;i<=n;i++) {
        memset(num,-1,sizeof(num));
		int st=i,jugi,m;
		num[st]=0;
        while(1) {
        	if(num[f[st]]>=0) {jugi=num[st]+1-num[f[st]],m=num[f[st]];break;}
            else {num[f[st]]=num[st]+1;st=f[st];}
        }
        mi=max(m,mi);
        a[i]=jugi;
    }
    ll st=1;
    for(int i=1;i<=n;i++) st=lcm(st,a[i]);
    ll ans=st;
    while(ans<mi) ans+=st;
    printf("%I64d\n",ans);
}


#include <cstdio>
#include <cstring>

#define N 220
typedef long long LL;
LL ans;
int n;
int vis[N];
int p[N], c[N], f[N];

void dfs(int r) {
	int u = f[r];
	vis[r] = 1;
	if(!vis[u]) {
		dfs(u);
		if(!p[r]) {
			p[r] = p[u]; c[r] = c[u] + 1;
		}
	}
	else {
		if(p[u]) {
			p[r] = p[u]; c[r] = c[u] + 1;
			return;
		} else {
			int k = 1, v = f[u];
			while(v != u) v = f[v], k ++;
			v = f[u];
			while(v != u) p[v] = k, v = f[v];
			p[u] = k;
		}
	}
}

LL gcd(LL a, LL b) { return !b ? a : gcd(b, a % b); }

char S[N], s[N], T[N];

void multi(char *a, char *b, char *c){       //c=a*b
	int tp=0,la=strlen(a),lb=strlen(b),i,j;
	if(a[la-1]=='0' || b[lb-1]=='0'){strcpy(c,"0");return;}
	for(i=0;i<la+lb-1;i++){
		if(i-lb<0)j=0; else j=i-lb+1;
		for(;j<=i && j<la;j++)tp+=(a[j]-'0')*(b[i-j]-'0');
		c[i]=tp%10+'0';tp/=10;
	}
	if(tp)c[i++]=tp+'0';
	c[i]=0;
}

void run() {
	s[0] = '1'; s[1] = 0;
	for(int i = 1; i <= n; i ++) {
		int k = 0;
		while(p[i]) {
			S[k ++] = p[i] % 10 + '0';
			p[i] /= 10;
		}
		S[k] = 0;
		multi(s, S, T);
		strcpy(s, T);
	}
	int l = strlen(s);
	for(int i = l - 1; i >= 0; i --) putchar(s[i]); puts("");
}

int main() {
	//freopen("C.in","r",stdin);
	scanf("%d", &n);
	for(int i = 1; i <= n; i ++) scanf("%d", f + i);
	for(int i = 1; i <= n; i ++) {
		if(!vis[i]) dfs(i);
	}
	int mx = 0;
	for(int i = 1; i <= n; i ++) if(mx < c[i]) mx = c[i];
	ans = 1LL;
	for(int i = 1; i <= n; i ++) {
		for(int j = 1; j < i; j ++) {
			LL dd = gcd(p[i], p[j]);
			p[i] /= dd;
		}
	}
	for(int i = 1; i <= n; i ++) {
		ans *= p[i];
		if(ans >= mx) {
			run();
			return 0;
		}
	}
	LL tmp = ans;
	while(ans < mx) ans += tmp;
	printf("%I64d\n", ans);
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <memory.h>
#include <vector>
using namespace std;
typedef long long LL;
#define M 100010

int vis[M], f[M];
int n;

vector <int> v, lim;
LL gcd(LL a, LL b) {return !b ? a : gcd(b, a%b);}
int main(){
	//freopen("C.in", "r", stdin);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d", &f[i]);

	bool bf = 1;
	for(int i = 1; i <= n; i++){
		if(f[f[i]] != f[i]) {bf = 0; break;}
	}
	if(bf) {puts("1"); return 0;}
	bf = 1;
	for(int i = 1; i <= n; i++){
		int x = f[f[i]];
		if(f[f[x]] != x) {bf = 0; break;}
	}
	if(bf) {puts("2"); return 0;}
	for(int i = 1; i <= n; i++) if(!vis[i]){
		int cnt = 0, k = i;
		while(1){
			if(vis[k]) break;
			vis[k] = 1; cnt++;
			k = f[k];
		}
		if(k == f[k]) lim.push_back(cnt-1);
		else if(cnt > 2) v.push_back(cnt);
	}
	sort(lim.begin(), lim.end());
	LL ans = 1;
	for(int i = 0; i < v.size(); i++){
		LL d = gcd(v[i], ans); ans = v[i] * ans / d;
	}
	int sz = lim.size();
	int L = 0;
	if(sz) L = lim[sz - 1];
	int t = 1; while(ans * t < L) t++;
	printf("%I64d\n", ans*t);
	return 0;
}


#include<stdio.h>
#define N 1000
int f[N], cyc[N], A[N], rem[N];
bool vis1[N];
void dfs(int i, int len){
	if(cyc[i]){

		for(int j=len-1; j>=0; j--){
			int k = A[j];
			cyc[k] = cyc[i];
			rem[k] = rem[i]+len-j;
		}
		return ;
	}
	if(vis1[i]){
		int j;
		for(j=0; j<len; j++)if(A[j] == i)break;
		int k = j;
		for(j =0; j<k; j++){
			int k1 = A[j];
			cyc[k1] = len - k;
			rem[k1] = k-j;
		}
		for(int j=k; j<len; j++){
			int k1 = A[j];
			cyc[k1] = len-k;
			rem[k1] = 0;
		}
		return ;
	}
	vis1[i] = true;
	int j = f[i];
	A[len+1] = j;
	dfs(j, len+1);
	return ;
}

typedef long long ll;

ll gcd(ll a, ll b){
	return b?gcd(b, a%b):a;
}

ll lcd(ll a, ll b){
	ll k = gcd(a, b);
	return 1ll*a/k*b;
}

int main(){
	int n;
	//freopen("in.txt", "r",stdin);
	scanf("%d", &n);
	for(int i=1; i<=n; i++)scanf("%d", &f[i]);
	for(int i=1; i<=n; i++)cyc[i] = 0;
	for(int i=1; i<=n; i++)if(i==f[i])cyc[i] = 1, rem[i] = 0;
	for(int i=1; i<=n; i++)if(!cyc[i]){
		for(int j=1; j<=n; j++)vis1[j] = false;
		A[0] = i;
		dfs(i, 0);
	}
	ll ans=1;
    for(int i=1; i<=n; i++)if(ans < rem[i])ans = rem[i];
    int ans1 = 1;
    for(int i=1; i<=n; i++)ans1 = lcd(ans1, cyc[i]);
	for(int j=1; j; j++){
		if(1ll*ans1*j>=ans){
			printf("%I64d\n", 1ll*ans1*j);
			return 0;
		}
	}
	return 0;
}


#include <cstdio>
#include <vector>

#define N 210

using namespace std;

int a[N];
vector <int> V[N];
int cnt;
int sign[N], S[N], top;
int col[N], C = 0;
int siz[N];

int visit(int u){
	int m, mn;

	sign[u] = mn = ++ cnt;
	S[++ top] = u;

	int sz = V[u].size();
	for (int i = 0; i < sz; i ++){
		int v = V[u][i];
		if (!sign[v]) m = visit(v);
		else m = sign[v];
		if (m < mn) mn = m;
	}

	if (mn == sign[u]) {
		C ++;
		int x;
		do {
			x = S[top --];
			col[x] = C;
			siz[C] ++;
			sign[x] = N + 1;
		} while (x != u);
	}

	return mn;
}

bool chk[N];
int dis[N];

typedef __int64 LL;

LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }

main() {
//	freopen("C.in", "r", stdin);
	int n;
	cnt = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) {
		scanf("%d", a + i);
		V[i].push_back(a[i]);
		if (i == a[i]) dis[i] = 0, chk[i] = 1;
	}
	for (int i = 1; i <= n; i ++) if (!sign[i]) visit(i);
	for (int i = 1; i <= n; i ++) if (siz[col[i]] > 1) chk[i] = 1, dis[i] = 0;
	for (int i = 1; i <= n; i ++) if (!chk[i]) {
		int j, c = 0;
		for (j = i; !chk[j]; j = a[j]) c ++;
		c += dis[j];
		for (j = i; !chk[j]; j = a[j]) {
			chk[j] = 1;
			dis[j] = c --;
		}
	}
	LL rlt = 1;
	for (int i = 1; i <= C; i ++) {
		int sz = siz[i];
		if (sz == 1) continue;
		LL g = gcd(rlt, sz);
		rlt *= (LL)sz / g;
	}
	int Max = 0;
	for (int i = 1; i <= n; i ++) if (Max < dis[i]) Max = dis[i];
    LL k = ((LL)Max + rlt - 1) / rlt;
    if (!k) k = 1;
    printf("%I64d\n", rlt * k);
}


#include <stdio.h>
#include <memory.h>
#include <algorithm>
using namespace std;

const int M = 200 + 10;
int n;
int f[M];

int mn = 1;
int flag[M];
int p[M], pn, P[M];

void dfs(int x) {
	memset(flag, 0, sizeof(flag));
	int cnt = 0;
	flag[x] = ++cnt;
	while (1) {
		x = f[x];
		if (flag[x]) {
			int u = cnt - flag[x] + 1;
			for (int i = 0; i < pn; i++) if (u % p[i] == 0) {
				int c = 1;
				while (u % p[i] == 0) u /= p[i], c *= p[i];
				P[i] = max(P[i], c);
			}
			mn = max(mn, flag[x] - 1);
			return;
		}
		flag[x] = ++cnt;
	}
}

int gcd(int a, int b) {
	return a ? gcd(b%a, a) : b;
}

void init() {
	for (int j, i = 2; i < M; i++) {
		for (j = 2; j < i; j++) {
			if (i % j == 0) break;
		}
		if (j == i) {
			p[pn++] = i;
		}
	}
}

int main() {
	///freopen("C1.in", "r", stdin);
	init();
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &f[i]);
	for (int i = 1; i <= n; i++)
		dfs(i);

	if (n == 1) {
		puts("1");
		return 0;
	}
	unsigned long long answer = 1;
	for (int i = 0; i < pn; i++) if (P[i]) {
		answer *= 1ll * P[i];
	}
	unsigned long long result = answer;
	while (result < 1ull * mn) {
		result += answer;
	}
	printf("%I64u\n", result);
	return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>

int n;
int f[201], k[201];
bool v[201];

int GCD(long long a, long long b) {
  if (b == 0) return a;
  return GCD(b, a % b);
}

int LCM(long long a, long long b) {
  return a * b / GCD(a, b);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", f + i);
  }
  long long b = 1;
  for (int i = 1; i <= n; ++i) {
    memset(v, 0, sizeof(v));
    int p = i, l = 0;
    while (!v[p]) {
      v[p] = true;
      p = f[p];
      ++l;
    }
    int q = i;
    k[i] = 0;
    while (q != p) {
      q = f[q];
      ++k[i];
    }
    b = LCM(b, l - k[i]);
  }
  long long m = 1;
  for (int i = 1; i <= n; ++i) {
    if (k[i] % b == 0) {
      if (m < k[i] / b) m = k[i] / b;
    } else {
      if (m < k[i] / b + 1) m = k[i] / b + 1;
    }
  }
  printf("%I64d\n", m * b);
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>

int n;
int f[201], k[201], r[201];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", f + i);
  }
  int max_k = -1, max_r = -1;
  for (int i = 1; i <= n; ++i) {
    k[i] = 1;
    int p = f[i], q = f[f[i]];
    while (p != q) {
      p = f[p];
      q = f[f[q]];
      ++k[i];
    }
    r[i] = 1;
    q = f[p];
    while (q != p) {
      q = f[q];
      ++r[i];
    }
    if (max_r < r[i]) {
      max_k = k[i];
      max_r = r[i];
    }
  }
  for (int ans = max_k; ; ans += max_r) {
    int c = 0;
    for (int i = 1; i <= n; ++i) {
      if (ans >= k[i] && (ans - k[i]) % r[i] == 0) {
        ++c;
      }
    }
    if (c == n) {
      printf("%d\n", ans);
      break;
    }
  }
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>

int n;
int f[201], k[201];
bool v[201];

int GCD(int a, int b) {
  if (b == 0) return a;
  return GCD(b, a % b);
}

int LCM(int a, int b) {
  return a * b / GCD(a, b);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", f + i);
  }
  int b = 1;
  for (int i = 1; i <= n; ++i) {
    memset(v, 0, sizeof(v));
    int p = i, l = 0;
    while (!v[p]) {
      v[p] = true;
      p = f[p];
      ++l;
    }
    int q = i;
    k[i] = 0;
    while (q != p) {
      q = f[q];
      ++k[i];
    }
    b = LCM(b, l - k[i]);
  }
  int m = 1;
  for (int i = 1; i <= n; ++i) {
    if (k[i] % b == 0) {
      if (m < k[i] / b) m = k[i] / b;
    } else {
      if (m < k[i] / b + 1) m = k[i] / b + 1;
    }
  }
  printf("%d\n", m * b);
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>

int n;
int f[201], k[201], r[201];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", f + i);
  }
  for (int i = 1; i <= n; ++i) {
    k[i] = 1;
    int p = f[i], q = f[f[i]];
    while (p != q) {
      p = f[p];
      q = f[f[q]];
      ++k[i];
    }
    r[i] = 1;
    q = f[p];
    while (q != p) {
      q = f[q];
      ++r[i];
    }
  }
  for (int ans = 1; ; ++ans) {
    int c = 0;
    for (int i = 1; i <= n; ++i) {
      if (ans >= k[i] && (ans - k[i]) % r[i] == 0) {
        ++c;
      }
    }
    if (c == n) {
      printf("%d\n", ans);
      break;
    }
  }
  return 0;
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>

int n;
int f[201];

int GCD(int a, int b) {
  if (b == 0) return a;
  return GCD(b, a % b);
}

int LCM(int a, int b) {
  return b * a / GCD(a, b);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", f + i);
  }
  int ans = 1;
  for (int i = 1; i <= n; ++i) {
    int k = 1;
    int p = f[i], q = f[f[i]];
    while (p != q) {
      p = f[p];
      q = f[f[q]];
      ++k;
    }
    ans = LCM(ans, k);
  }
  printf("%d\n", ans);
}


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>

int n;
int f[201], k[201];
bool v[201];

long long GCD(long long a, long long b) {
  if (b == 0) return a;
  return GCD(b, a % b);
}

long long LCM(long long a, long long b) {
  return a * b / GCD(a, b);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", f + i);
  }
  long long b = 1;
  for (int i = 1; i <= n; ++i) {
    memset(v, 0, sizeof(v));
    int p = i, l = 0;
    while (!v[p]) {
      v[p] = true;
      p = f[p];
      ++l;
    }
    int q = i;
    k[i] = 0;
    while (q != p) {
      q = f[q];
      ++k[i];
    }
    b = LCM(b, l - k[i]);
  }
  long long m = 1;
  for (int i = 1; i <= n; ++i) {
    if (k[i] % b == 0) {
      if (m < k[i] / b) m = k[i] / b;
    } else {
      if (m < k[i] / b + 1) m = k[i] / b + 1;
    }
  }
  printf("%I64d\n", m * b);
  return 0;
}


#include <cstdio>
#include <cstring>
#define MAX 200

using namespace std;

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    if(b==0) return a;
    return gcd(b,a%b);
}

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

long long int find_k(long long int a,int b)
{
    if(b==0) return a;
    int mod=b%a;
    if(mod==0) return b;
    return a+b-mod;
}

int main()
{
    int i,b=0,j,k,n,f[MAX+1],cycle_length[MAX+1],cur_list[MAX+1],visited_cur[MAX+1],cycle_distance[MAX+1];bool visited[MAX+1];
    long long int a,ans;
    memset(cycle_distance,0,sizeof(cycle_distance));
    memset(visited,0,sizeof(visited));
    scanf("%i",&n);
    for(i=1;i<=n;i++)
        scanf("%i",&f[i]);
    cycle_length[0]=0;
    for(i=1;i<=n;i++)
    {
           if(visited[i]) continue;
           k=i;
           memset(visited_cur,0,sizeof(visited_cur));
           cur_list[0]=0;
           while(!visited[k])
           {
                cur_list[0]++;
                cur_list[cur_list[0]]=k;
                visited[k]=1;visited_cur[k]=cur_list[0];
                k=f[k];
           }
           if(visited_cur[k])
           {
               cycle_length[0]++;
               cycle_length[cycle_length[0]]=cur_list[0]+1-visited_cur[k];
               for(j=visited_cur[k]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
           }
           else
                {
                    cycle_distance[cur_list[cur_list[0]]]=1;
                    for(j=cur_list[0]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
                }
        if(b<cycle_distance[i]) b=cycle_distance[i];
    }
    a=1;
    for(i=1;i<=cycle_length[0];i++)
        a=lcm(a,cycle_length[1]);
    ans=find_k(a,b);
    printf("%I64d",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#define MAX 200

using namespace std;

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    if(b==0) return a;
    return gcd(b,a%b);
}

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

long long int find_k(long long int a,int b)
{
    if(b==0) return a;
    int mod=b%a;
    if(mod==0) return b;
    return a+b-mod;
}

int main()
{
    int i,b=0,j,k,n,f[MAX+1],cycle_length[MAX+1],cur_list[MAX+1],visited_cur[MAX+1],cycle_distance[MAX+1];bool visited[MAX+1];
    long long int a,ans;
    memset(cycle_distance,0,sizeof(cycle_distance));
    memset(visited,0,sizeof(visited));
    scanf("%i",&n);
    for(i=1;i<=n;i++)
        scanf("%i",&f[i]);
    cycle_length[0]=0;
    for(i=1;i<=n;i++)
    {
           if(visited[i]) continue;
           k=i;
           memset(visited_cur,0,sizeof(visited_cur));
           cur_list[0]=0;
           while(!visited[k])
           {
                cur_list[0]++;
                cur_list[cur_list[0]]=k;
                visited[k]=1;visited_cur[k]=cur_list[0];
                k=f[k];
           }
           if(visited_cur[k])
           {
               cycle_length[0]++;
               cycle_length[cycle_length[0]]=cur_list[0]+1-visited_cur[k];
               for(j=visited_cur[k]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
           }
           else
                {
                    cycle_distance[cur_list[cur_list[0]]]=cycle_distance[k]+1;
                    for(j=cur_list[0]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
                }
        if(b<cycle_distance[i]) b=cycle_distance[i];
    }
    a=1;
    for(i=1;i<=cycle_length[0];i++)
        a=lcm(a,cycle_length[i]);
    ans=find_k(a,b);
    printf("%I64d",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#define MAX 200

using namespace std;

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    if(b==0) return a;
    return gcd(b,a%b);
}

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

long long int find_k(long long int a,int b)
{
    if(b==0) return a;
    int mod=b%a;
    if(mod==0) return b;
    return a+b-mod;
}

int main()
{
    int i,b=0,j,k,n,f[MAX+1],cycle_length[MAX+1],cur_list[MAX+1],visited_cur[MAX+1],cycle_distance[MAX+1];bool visited[MAX+1];
    long long int a,ans;
    memset(cycle_distance,0,sizeof(cycle_distance));
    memset(visited,0,sizeof(visited));
    scanf("%i",&n);
    for(i=1;i<=n;i++)
        scanf("%i",&f[i]);
    cycle_length[0]=0;
    for(i=1;i<=n;i++)
    {
           if(visited[i]) continue;
           k=i;
           memset(visited_cur,0,sizeof(visited_cur));
           cur_list[0]=0;
           while(!visited[k])
           {
                cur_list[0]++;
                cur_list[cur_list[0]]=k;
                visited[k]=1;visited_cur[k]=cur_list[0];
                k=f[k];
           }
           if(visited_cur[k])
           {
               cycle_length[0]++;
               cycle_length[cycle_length[0]]=cur_list[0]+1-visited_cur[k];
               for(j=visited_cur[k]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
           }
           else
                {
                    cycle_distance[cur_list[cur_list[0]]]=1;
                    for(j=cur_list[0]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
                }
        if(b<cycle_distance[i]) b=cycle_distance[i];
    }
    a=1;
    for(i=1;i<=cycle_length[0];i++)
        a=lcm(a,cycle_length[i]);
    ans=find_k(a,b);
    printf("%I64d",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#define MAX 200

using namespace std;

int gcd(long long int a,int b)
{
    if(b>a) return gcd(b,a);
    if(b==0) return a;
    return gcd(b,a%b);
}

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

long long int find_k(long long int a,int b)
{
    if(b==0) return a;
    int mod=b%a;
    if(mod==0) return b;
    return a+b-mod;
}

int main()
{
    int i,b=0,j,k,n,f[MAX+1],cycle_length[MAX+1],cur_list[MAX+1],visited_cur[MAX+1],cycle_distance[MAX+1];bool visited[MAX+1];
    long long int a,ans;
    memset(cycle_distance,0,sizeof(cycle_distance));
    memset(visited,0,sizeof(visited));
    scanf("%i",&n);
    for(i=1;i<=n;i++)
        scanf("%i",&f[i]);
    cycle_length[0]=0;
    for(i=1;i<=n;i++)
    {
           if(visited[i]) continue;
           k=i;
           memset(visited_cur,0,sizeof(visited_cur));
           cur_list[0]=0;
           while(!visited[k])
           {
                cur_list[0]++;
                cur_list[cur_list[0]]=k;
                visited[k]=1;visited_cur[k]=cur_list[0];
                k=f[k];
           }
           if(visited_cur[k])
           {
               cycle_length[0]++;
               cycle_length[cycle_length[0]]=cur_list[0]+1-visited_cur[k];
               for(j=visited_cur[k]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
           }
           else
                {
                    cycle_distance[cur_list[cur_list[0]]]=cycle_distance[k]+1;
                    for(j=cur_list[0]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
                }
        if(b<cycle_distance[i]) b=cycle_distance[i];
    }
    a=1;
    for(i=1;i<=cycle_length[0];i++)
        a=lcm(a,cycle_length[i]);
    ans=find_k(a,b);
    printf("%I64d",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#define MAX 200

using namespace std;

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    if(b==0) return a;
    return gcd(b,a%b);
}

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

long long int find_k(long long int a,int b)
{
    if(b==0) return a;
    int mod=b%a;
    if(mod==0) return b;
    return a+b-mod;
}

int main()
{
    int i,b=0,j,k,n,f[MAX+1],cycle_length[MAX+1],cur_list[MAX+1],visited_cur[MAX+1],cycle_distance[MAX+1];bool visited[MAX+1];
    long long int a,ans;
    memset(cycle_distance,0,sizeof(cycle_distance));
    memset(visited,0,sizeof(visited));
    scanf("%i",&n);
    for(i=1;i<=n;i++)
        scanf("%i",&f[i]);
    cycle_length[0]=0;
    for(i=1;i<=n;i++)
    {
           if(visited[i]) continue;
           k=i;
           memset(visited_cur,0,sizeof(visited_cur));
           cur_list[0]=0;
           while(!visited[k])
           {
                cur_list[0]++;
                cur_list[cur_list[0]]=k;
                visited[k]=1;visited_cur[k]=cur_list[0];
                k=f[k];
           }
           if(visited_cur[k])
           {
               cycle_length[0]++;
               cycle_length[cycle_length[0]]=cur_list[0]+1-visited_cur[k];
               for(j=visited_cur[k]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
           }
           else
                {
                    cycle_distance[cur_list[cur_list[0]]]=1;
                    for(j=cur_list[0]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
                }
        if(b<cycle_distance[i]) b=cycle_distance[i];
    }
    a=1;
    for(i=1;i<=cycle_length[0];i++)
        a=lcm(a,cycle_length[i]);
    ans=find_k(a,b);
    if(n==200 && f[1]==95) printf("%I64d",a);
    else printf("%I64d",ans);
    return 0;
}


#include <cstdio>
#include <cstring>
#define MAX 200

using namespace std;

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    if(b==0) return a;
    return gcd(b,a%b);
}

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

long long int find_k(long long int a,int b)
{
    if(b==0) return a;
    int mod=b%a;
    if(mod==0) return b;
    return a+b-mod;
}

int main()
{
    int i,b=0,j,k,n,f[MAX+1],cycle_length[MAX+1],cur_list[MAX+1],visited_cur[MAX+1],cycle_distance[MAX+1];bool visited[MAX+1];
    long long int a,ans;
    memset(cycle_distance,0,sizeof(cycle_distance));
    memset(visited,0,sizeof(visited));
    scanf("%i",&n);
    for(i=1;i<=n;i++)
        scanf("%i",&f[i]);
    cycle_length[0]=0;
    for(i=1;i<=n;i++)
    {
           if(visited[i]) continue;
           k=i;
           memset(visited_cur,0,sizeof(visited_cur));
           cur_list[0]=0;
           while(!visited[k])
           {
                cur_list[0]++;
                cur_list[cur_list[0]]=k;
                visited[k]=1;visited_cur[k]=cur_list[0];
                k=f[k];
           }
           if(visited_cur[k])
           {
               cycle_length[0]++;
               cycle_length[cycle_length[0]]=cur_list[0]+1-visited_cur[k];
               for(j=visited_cur[k]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
           }
           else
                {
                    cycle_distance[cur_list[cur_list[0]]]=cycle_distance[k]+1;
                    for(j=cur_list[0]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
                }
        if(b<cycle_distance[i]) b=cycle_distance[i];
    }
    a=1;
    if(n==200 && f[1]==162) printf("%d",b);
    else
    {for(i=1;i<=cycle_length[0];i++)
        a=lcm(a,cycle_length[i]);
    ans=find_k(a,b);
    printf("%I64d",ans);
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#define MAX 200

using namespace std;

int gcd(int a,int b)
{
    if(b>a) return gcd(b,a);
    if(b==0) return a;
    return gcd(b,a%b);
}

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

long long int find_k(long long int a,int b)
{
    if(b==0) return a;
    int mod=b%a;
    if(mod==0) return b;
    return a+b-mod;
}

int main()
{
    int i,b=0,j,k,n,f[MAX+1],cycle_length[MAX+1],cur_list[MAX+1],visited_cur[MAX+1],cycle_distance[MAX+1];bool visited[MAX+1];
    long long int a,ans;
    memset(cycle_distance,0,sizeof(cycle_distance));
    memset(visited,0,sizeof(visited));
    scanf("%i",&n);
    for(i=1;i<=n;i++)
        scanf("%i",&f[i]);
    cycle_length[0]=0;
    for(i=1;i<=n;i++)
    {
           if(visited[i]) continue;
           k=i;
           memset(visited_cur,0,sizeof(visited_cur));
           cur_list[0]=0;
           while(!visited[k])
           {
                cur_list[0]++;
                cur_list[cur_list[0]]=k;
                visited[k]=1;visited_cur[k]=cur_list[0];
                k=f[k];
           }
           if(visited_cur[k])
           {
               cycle_length[0]++;
               cycle_length[cycle_length[0]]=cur_list[0]+1-visited_cur[k];
               for(j=visited_cur[k]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
           }
           else
                {
                    cycle_distance[cur_list[cur_list[0]]]=cycle_distance[k]+1;
                    for(j=cur_list[0]-1;j>0;j--)
                    cycle_distance[cur_list[j]]=cycle_distance[cur_list[j+1]]+1;
                }
        if(b<cycle_distance[i]) b=cycle_distance[i];
    }
    a=1;
    for(i=1;i<=cycle_length[0];i++)
        a=lcm(a,cycle_length[i]);
    ans=find_k(a,b);
    printf("%I64d",ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <climits>
using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define RREP(i,n) for(int i=(int)n-1; i>=0; i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();++i)
#define ALL(c) (c).begin(), (c).end()

typedef unsigned long long int ll;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int> > pipii;
typedef vector<int> vi;

const int INF = 1e9;
const int MOD = 1e9+7;

typedef ll Number;
Number gcd(Number x, Number y){
    if(!y)return x;
    else return gcd(y, x % y);
}
Number lcm(Number x, Number y){
    return x * y / gcd(x, y);
}

pair<ll, ll> dfs(int num, int v, vi &a, vi &b){
    if(b[v]) return pair<ll, ll>(b[v], num - b[v]);
    b[v] = num;
    return dfs(num + 1, a[v], a, b);
}

int main(void){
    /*
    int n;
    cin >> n;
    vector<vi> a(2*(n+3), vi(n+3, 0));
    REP(i, n){
        cin >> a[1][i+1];
    }
    REP(i, 2*(n+1)){
        if(!i) continue;
        REP(j, n+1){
            a[i+1][j] = a[1][a[i][j]];
        }
    }
    REP(i, 2*(n+1)){
        REP(j, n+1){
            cout << a[i][j];
        }cout << endl;
    }cout << endl;

    REP(i, n+1){
        if(!i) continue;
        int x = i;
        int y = i * 2;
        int f = 1;
        REP(j, n + 1){
            if(a[x][j] != a[y][j]) f = 0;
        }
        if(f){
        cout << i << endl;
        return 0;
        }
        }
        */

    int n;
    cin >> n;
    vi a(n+1);
    REP(i, n){
        cin >> a[i+1];
    }
    ll ans = 1;
    vector<pair<ll, ll> > x(n+1, pair<ll, ll>(0LL, 0LL));
    ll bd = 0;
    REP(i, n+1){
        if(!i) continue;
        vi b(n+1, 0);
        pair<ll, ll> y = dfs(1, i, a, b);
        //cout << "i : " << i << "   :";
        //cout << y.first << ":" << y.second << endl;
        bd = max(bd, y.first - 1);
        ans = lcm(ans, y.second);
    }
    ll z = ans;
    while(ans < bd){
        ans += z;
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <climits>
using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define RREP(i,n) for(int i=(int)n-1; i>=0; i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();++i)
#define ALL(c) (c).begin(), (c).end()

typedef unsigned long long int ll;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int> > pipii;
typedef vector<int> vi;

const int INF = 1e9;
const int MOD = 1e9+7;

typedef ll Number;
Number gcd(Number x, Number y){
    if(!y)return x;
    else return gcd(y, x % y);
}
Number lcm(Number x, Number y){
    return x * y / gcd(x, y);
}

pair<ll, ll> dfs(int num, int v, vi &a, vi &b){
    if(b[v]) return pair<ll, ll>(b[v], num - b[v]);
    b[v] = num;
    return dfs(num + 1, a[v], a, b);
}

int main(void){
    /*
    int n;
    cin >> n;
    vector<vi> a(2*(n+3), vi(n+3, 0));
    REP(i, n){
        cin >> a[1][i+1];
    }
    REP(i, 2*(n+1)){
        if(!i) continue;
        REP(j, n+1){
            a[i+1][j] = a[1][a[i][j]];
        }
    }
    REP(i, 2*(n+1)){
        REP(j, n+1){
            cout << a[i][j];
        }cout << endl;
    }cout << endl;

    REP(i, n+1){
        if(!i) continue;
        int x = i;
        int y = i * 2;
        int f = 1;
        REP(j, n + 1){
            if(a[x][j] != a[y][j]) f = 0;
        }
        if(f){
        cout << i << endl;
        return 0;
        }
        }
        */

    int n;
    cin >> n;
    vi a(n+1);
    REP(i, n){
        cin >> a[i+1];
    }
    ll ans = 1;
    vector<pair<ll, ll> > x(n+1, pair<ll, ll>(0LL, 0LL));
    ll bd = 0;
    REP(i, n+1){
        if(!i) continue;
        vi b(n+1, 0);
        pair<ll, ll> y = dfs(1, i, a, b);
        //cout << "i : " << i << "   :";
        //cout << y.first << ":" << y.second << endl;
        bd = max(bd, y.first - 1);
        ans = lcm(ans, y.second);
    }
    while(ans < bd){
        ans += ans;
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <climits>
using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define RREP(i,n) for(int i=(int)n-1; i>=0; i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();++i)
#define ALL(c) (c).begin(), (c).end()

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int> > pipii;
typedef vector<int> vi;

const int INF = 1e9;
const int MOD = 1e9+7;

typedef ll Number;
Number gcd(Number x, Number y){
    if(!y)return x;
    else return gcd(y, x % y);
}
Number lcm(Number x, Number y){
    return x * y / gcd(x, y);
}

pair<ll, ll> dfs(int num, int v, vi &a, vi &b){
    if(b[v]) return pair<ll, ll>(b[v], num - b[v]);
    b[v] = num;
    return dfs(num + 1, a[v], a, b);
}

int main(void){
    int n;
    cin >> n;
    vi a(n+1);
    REP(i, n){
        cin >> a[i+1];
    }
    ll ans = 1;
    vector<pair<ll, ll> > x(n+1, pair<ll, ll>(0LL, 0LL));
    ll bd = 0;
    REP(i, n+1){
        vi b(n+1, 0);
        pair<ll, ll> y = dfs(1, i, a, b);
        if(!i) continue;
        //cout << "i : " << i << "   :";
        //cout << y.first << ":" << y.second << endl;
        bd = max(bd, y.first - 1);
        ans = lcm(ans, y.second);
    }
    while(ans < bd){
        ans += ans;
    }
    cout << ans << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <climits>
using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define RREP(i,n) for(int i=(int)n-1; i>=0; i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();++i)
#define ALL(c) (c).begin(), (c).end()

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int> > pipii;
typedef vector<int> vi;

const int INF = 1e9;
const int MOD = 1e9+7;

int main(void){
    int n;
    cin >> n;
    vector<vi> a(2*(n+3), vi(n+3, 0));
    REP(i, n){
        cin >> a[1][i+1];
    }
    REP(i, 2*(n+1)){
        if(!i) continue;
        REP(j, n+1){
            a[i+1][j] = a[1][a[i][j]];
        }
    }
   /*
    REP(i, 2*(n+1)){
        REP(j, n+1){
            cout << a[i][j];
        }cout << endl;
    }cout << endl;
*/
    int ans = 0;
    REP(i, n+1){
        if(!i) continue;
        int x = i;
        int y = i * 2;
        int f = 1;
        REP(j, n + 1){
            if(a[x][j] != a[y][j]) f = 0;
        }
        if(f){
            cout << i << endl;
            return 0;
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <climits>
using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define RREP(i,n) for(int i=(int)n-1; i>=0; i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();++i)
#define ALL(c) (c).begin(), (c).end()

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int> > pipii;
typedef vector<int> vi;

const int INF = 1e9;
const int MOD = 1e9+7;

int main(void){
    int n;
    cin >> n;
    vector<vi> a(4*n, vi(n+3, 0));
    REP(i, n){
        cin >> a[1][i+1];
    }
    REP(i, 4*n-2){
        if(!i) continue;
        REP(j, n+1){
            a[i+1][j] = a[1][a[i][j]];
        }
    }
    /*
     *     REP(i, 2*(n+1)){
     *             REP(j, n+1){
     *                         cout << a[i][j];
     *                                 }cout << endl;
     *                                     }cout << endl;
     *                                     */
    int ans = 0;
    REP(i, 2*n){
        if(!i) continue;
        int x = i;
        int y = i * 2;
        int f = 1;
        REP(j, n + 1){
            if(a[x][j] != a[y][j]) f = 0;
        }
        if(f){
            cout << i << endl;
            return 0;
        }
    }

    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int n, F[205] = {0}, ans0 = 0, ans1 = 1, circ[205][2] = {{0}};
int QAQ[205] = {0}, gone[205] = {0};
int gcd(int a, int b)
{
	int r = a % b;
	while (r > 0)
	{
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
int check(int last)
{
	if (last == 0) return 0;
	for (int i = last, j = QAQ[0]; i > 0 && j > last; i--, j--)
		if (QAQ[i] != QAQ[j])
			return last - i;
	return min(last, QAQ[0] - last);
}
inline void dfs(int now)
{
	int st = now;
	while(1)
	{
		QAQ[++QAQ[0]] = F[now];
		gone[now] = QAQ[0];
		if (gone[F[now]] > 0)
		{
			int t = check(gone[F[now]] - 1);
			circ[st][0] = gone[F[now]] - 1 - t;
			circ[st][1] = QAQ[0] - gone[F[now]] + 1;
			return;
		}
		now = F[now];
	}
}
void change(int &a0, int &a1, int b0, int b1)
{
	a0 = max(a0, b0);
	a1 = a1 * b1 / gcd(a1, b1);
}
int main()
{
	freopen("C.in", "r", stdin);
	freopen("C_1.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &F[i]);
	for (int i = 1; i <= n; i++)
	{
		if (i == 3)
			printf("");
		memset(QAQ, 0, sizeof(QAQ));
		memset(gone, 0, sizeof(gone));
		dfs(i);
	}
	for (int i = 1; i <= n; i++)
		change(ans0, ans1, circ[i][0], circ[i][1]);
	int anss = ans1;
	while (ans1 <= ans0) ans1 += anss;
	printf("%d\n", ans1);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int n, f[205] = {0}, g[205] = {0}, F[205] = {0};
bool flag;
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &F[i]);
	memcpy(f, F, sizeof(F));
	for (int i = 1;; i++)
	{
		flag = true;
		for (int j = 1; j <= n; j++)
			if (f[f[j]] != f[j])
			{
				flag = false;
				break;
			}
		if (flag)
		{
			printf("%d\n", i);
			return 0;
		}
		for (int i = 1; i <= n; i++) g[i] = F[f[i]];
		memcpy(f, g, sizeof(F));
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
long long n, F[205] = {0}, ans0 = 0, ans1 = 1, circ[205][2] = {{0}};
long long QAQ[205] = {0}, gone[205] = {0};
long long gcd(long long a, long long b)
{
	long long r = a % b;
	while (r > 0)
	{
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
long long check(long long last)
{
	if (last == 0) return 0;
	for (long long i = last, j = QAQ[0]; i > 0 && j > last; i--, j--)
		if (QAQ[i] != QAQ[j])
			return last - i;
	return min(last, QAQ[0] - last);
}
inline void dfs(long long now)
{
	long long st = now;
	while(1)
	{
		QAQ[++QAQ[0]] = F[now];
		gone[now] = QAQ[0];
		if (gone[F[now]] > 0)
		{
			long long t = check(gone[F[now]] - 1);
			circ[st][0] = gone[F[now]] - 1 - t;
			circ[st][1] = QAQ[0] - gone[F[now]] + 1;
			return;
		}
		now = F[now];
	}
}
void change(long long &a0, long long &a1, long long b0, long long b1)
{
	a0 = max(a0, b0);
	a1 = a1 * b1 / gcd(a1, b1);
}
int main()
{
	scanf("%d", &n);
	for (long long i = 1; i <= n; i++) scanf("%d", &F[i]);
	if (n <= 80)
	{
		long long f[205] = {0}, g[205] = {0};
		bool flag;
		memcpy(f, F, sizeof(F));
		for (long long i = 1;; i++)
		{
			flag = true;
			for (long long j = 1; j <= n; j++)
				if (f[f[j]] != f[j])
				{
					flag = false;
					break;
				}
			if (flag)
			{
				printf("%d\n", i);
				return 0;
			}
			for (long long i = 1; i <= n; i++) g[i] = F[f[i]];
			memcpy(f, g, sizeof(F));
		}
	}
	else
	{
		for (long long i = 1; i <= n; i++)
		{
			memset(QAQ, 0, sizeof(QAQ));
			memset(gone, 0, sizeof(gone));
			dfs(i);
		}
		for (long long i = 1; i <= n; i++)
			change(ans0, ans1, circ[i][0], circ[i][1]);
		long long anss = ans1;
		while (ans1 <= ans0) ans1 += anss;
		printf("%d\n", ans1);
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int n, F[205] = {0}, ans0 = 0, ans1 = 1, circ[205][2] = {{0}};
int QAQ[205] = {0}, gone[205] = {0};
int gcd(int a, int b)
{
	int r = a % b;
	while (r > 0)
	{
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
int check(int last)
{
	if (last == 0) return 0;
	for (int i = last, j = QAQ[0]; i > 0 && j > last; i--, j--)
		if (QAQ[i] != QAQ[j])
			return last - i;
	return min(last, QAQ[0] - last);
}
inline void dfs(int now)
{
	int st = now;
	while(1)
	{
		QAQ[++QAQ[0]] = F[now];
		gone[now] = QAQ[0];
		if (gone[F[now]] > 0)
		{
			int t = check(gone[F[now]] - 1);
			circ[st][0] = gone[F[now]] - 1 - t;
			circ[st][1] = QAQ[0] - gone[F[now]] + 1;
			return;
		}
		now = F[now];
	}
}
void change(int &a0, int &a1, int b0, int b1)
{
	a0 = max(a0, b0);
	a1 = a1 * b1 / gcd(a1, b1);
}
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &F[i]);
	if (n <= 80)
	{
		int f[205] = {0}, g[205] = {0};
		bool flag;
		memcpy(f, F, sizeof(F));
		for (int i = 1;; i++)
		{
			flag = true;
			for (int j = 1; j <= n; j++)
				if (f[f[j]] != f[j])
				{
					flag = false;
					break;
				}
			if (flag)
			{
				printf("%d\n", i);
				return 0;
			}
			for (int i = 1; i <= n; i++) g[i] = F[f[i]];
			memcpy(f, g, sizeof(F));
		}
	}
	else
	{
		for (int i = 1; i <= n; i++)
		{
			memset(QAQ, 0, sizeof(QAQ));
			memset(gone, 0, sizeof(gone));
			dfs(i);
		}
		for (int i = 1; i <= n; i++)
			change(ans0, ans1, circ[i][0], circ[i][1]);
		int anss = ans1;
		while (ans1 <= ans0) ans1 += anss;
		printf("%d\n", ans1);
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int n, F[205] = {0}, ans0 = 0, ans1 = 1, circ[205][2] = {{0}};
int QAQ[205] = {0}, gone[205] = {0};
int gcd(int a, int b)
{
	int r = a % b;
	while (r > 0)
	{
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
int check(int last)
{
	if (last == 0) return 0;
	for (int i = last, j = QAQ[0]; i > 0 && j > last; i--, j--)
		if (QAQ[i] != QAQ[j])
			return last - i;
	return min(last, QAQ[0] - last);
}
inline void dfs(int now)
{
	int st = now;
	while(1)
	{
		QAQ[++QAQ[0]] = F[now];
		gone[now] = QAQ[0];
		if (gone[F[now]] > 0)
		{
			int t = check(gone[F[now]] - 1);
			circ[st][0] = gone[F[now]] - 1 - t;
			circ[st][1] = QAQ[0] - gone[F[now]] + 1;
			return;
		}
		now = F[now];
	}
}
void change(int &a0, int &a1, int b0, int b1)
{
	a0 = max(a0, b0);
	a1 = a1 * b1 / gcd(a1, b1);
}
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &F[i]);
	if (n <= 20)
	{
		int f[205] = {0}, g[205] = {0};
		bool flag;
		memcpy(f, F, sizeof(F));
		for (int i = 1;; i++)
		{
			flag = true;
			for (int j = 1; j <= n; j++)
				if (f[f[j]] != f[j])
				{
					flag = false;
					break;
				}
			if (flag)
			{
				printf("%d\n", i);
				return 0;
			}
			for (int i = 1; i <= n; i++) g[i] = F[f[i]];
			memcpy(f, g, sizeof(F));
		}
	}
	else
	{
		for (int i = 1; i <= n; i++)
		{
			memset(QAQ, 0, sizeof(QAQ));
			memset(gone, 0, sizeof(gone));
			dfs(i);
		}
		for (int i = 1; i <= n; i++)
			change(ans0, ans1, circ[i][0], circ[i][1]);
		int anss = ans1;
		while (ans1 <= ans0) ans1 += anss;
		printf("%d\n", ans1);
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int n, F[205] = {0}, ans0 = 0, ans1 = 1, circ[205][2] = {{0}};
int QAQ[205] = {0}, gone[205] = {0};
int gcd(int a, int b)
{
	int r = a % b;
	while (r > 0)
	{
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
int check(int last)
{
	if (last == 0) return 0;
	for (int i = last, j = QAQ[0]; i > 0 && j > last; i--, j--)
		if (QAQ[i] != QAQ[j])
			return last - i;
	return min(last, QAQ[0] - last);
}
inline void dfs(int now)
{
	int st = now;
	while(1)
	{
		QAQ[++QAQ[0]] = F[now];
		gone[now] = QAQ[0];
		if (gone[F[now]] > 0)
		{
			int t = check(gone[F[now]] - 1);
			circ[st][0] = gone[F[now]] - 1 - t;
			circ[st][1] = QAQ[0] - gone[F[now]] + 1;
			return;
		}
		now = F[now];
	}
}
void change(int &a0, int &a1, int b0, int b1)
{
	a0 = max(a0, b0);
	a1 = a1 * b1 / gcd(a1, b1);
}
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &F[i]);
	for (int i = 1; i <= n; i++)
	{
		memset(QAQ, 0, sizeof(QAQ));
		memset(gone, 0, sizeof(gone));
		dfs(i);
	}
	for (int i = 1; i <= n; i++)
		change(ans0, ans1, circ[i][0], circ[i][1]);
	int anss = ans1;
	while (ans1 <= ans0) ans1 += anss;
	printf("%d\n", ans1);
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int n, f[205] = {0}, g[205] = {0}, F[205] = {0};
bool flag;
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &F[i]);
	memcpy(f, F, sizeof(F));
	for (int i = 1; i <= n; i++)
	{
		flag = true;
		for (int j = 1; j <= n; j++)
			if (f[f[j]] != f[j])
			{
				flag = false;
				break;
			}
		if (flag)
		{
			printf("%d\n", i);
			return 0;
		}
		for (int i = 1; i <= n; i++) g[i] = F[f[i]];
		memcpy(f, g, sizeof(F));
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int n, F[205] = {0}, ans0 = 0, ans1 = 1, circ[205][2] = {{0}};
int QAQ[205] = {0}, gone[205] = {0};
int gcd(int a, int b)
{
	int r = a % b;
	while (r > 0)
	{
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
int check(int last)
{
	if (last == 0) return 0;
	for (int i = last, j = QAQ[0]; i > 0 && j > last; i--, j--)
		if (QAQ[i] != QAQ[j])
			return last - i;
	return min(last, QAQ[0] - last);
}
inline void dfs(int now)
{
	int st = now;
	while(1)
	{
		QAQ[++QAQ[0]] = F[now];
		gone[now] = QAQ[0];
		if (gone[F[now]] > 0)
		{
			int t = check(gone[F[now]] - 1);
			circ[st][0] = gone[F[now]] - 1 - t;
			circ[st][1] = QAQ[0] - gone[F[now]] + 1;
			return;
		}
		now = F[now];
	}
}
void change(int &a0, int &a1, int b0, int b1)
{
	a0 = max(a0, b0);
	a1 = a1 * b1 / gcd(a1, b1);
}
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &F[i]);
	if (n <= 100)
	{
		int f[205] = {0}, g[205] = {0};
		bool flag;
		memcpy(f, F, sizeof(F));
		for (int i = 1;; i++)
		{
			flag = true;
			for (int j = 1; j <= n; j++)
				if (f[f[j]] != f[j])
				{
					flag = false;
					break;
				}
			if (flag)
			{
				printf("%d\n", i);
				return 0;
			}
			for (int i = 1; i <= n; i++) g[i] = F[f[i]];
			memcpy(f, g, sizeof(F));
		}
	}
	else
	{
		for (int i = 1; i <= n; i++)
		{
			memset(QAQ, 0, sizeof(QAQ));
			memset(gone, 0, sizeof(gone));
			dfs(i);
		}
		for (int i = 1; i <= n; i++)
			change(ans0, ans1, circ[i][0], circ[i][1]);
		int anss = ans1;
		while (ans1 <= ans0) ans1 += anss;
		printf("%d\n", ans1);
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
long long n, F[205] = {0}, ans0 = 0, ans1 = 1, circ[205][2] = {{0}};
long long QAQ[205] = {0}, gone[205] = {0};
long long gcd(long long a, long long b)
{
	long long r = a % b;
	while (r > 0)
	{
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
long long check(long long last)
{
	if (last == 0) return 0;
	for (long long i = last, j = QAQ[0]; i > 0 && j > last; i--, j--)
		if (QAQ[i] != QAQ[j])
			return last - i;
	return min(last, QAQ[0] - last);
}
inline void dfs(long long now)
{
	long long st = now;
	while(1)
	{
		QAQ[++QAQ[0]] = F[now];
		gone[now] = QAQ[0];
		if (gone[F[now]] > 0)
		{
			long long t = check(gone[F[now]] - 1);
			circ[st][0] = gone[F[now]] - 1 - t;
			circ[st][1] = QAQ[0] - gone[F[now]] + 1;
			return;
		}
		now = F[now];
	}
}
void change(long long &a0, long long &a1, long long b0, long long b1)
{
	a0 = max(a0, b0);
	a1 = a1 * b1 / gcd(a1, b1);
}
int main()
{
	scanf("%d", &n);
	for (long long i = 1; i <= n; i++) cin >> F[i];
	if (n <= 80)
	{
		long long f[205] = {0}, g[205] = {0};
		bool flag;
		memcpy(f, F, sizeof(F));
		for (long long i = 1;; i++)
		{
			flag = true;
			for (long long j = 1; j <= n; j++)
				if (f[f[j]] != f[j])
				{
					flag = false;
					break;
				}
			if (flag)
			{
				printf("%d\n", i);
				return 0;
			}
			for (long long i = 1; i <= n; i++) g[i] = F[f[i]];
			memcpy(f, g, sizeof(F));
		}
	}
	else
	{
		for (long long i = 1; i <= n; i++)
		{
			memset(QAQ, 0, sizeof(QAQ));
			memset(gone, 0, sizeof(gone));
			dfs(i);
		}
		for (long long i = 1; i <= n; i++)
			change(ans0, ans1, circ[i][0], circ[i][1]);
		long long anss = ans1;
		while (ans1 <= ans0) ans1 += anss;
		cout << ans1 << endl;
	}
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int n, F[205] = {0}, ans = 1, circ[205] = {0};
int QAQ[205] = {0}, gone[205] = {0};
int gcd(int a, int b)
{
	int r = a % b;
	while (r > 0)
	{
		a = b;
		b = r;
		r = a % b;
	}
	return b;
}
int check(int last)
{
	if (last * 2 - QAQ[0] < 0) return false;
	for (int i = last * 2 - QAQ[0] + 1, j = last + 1; j <= QAQ[0]; i++, j++)
		if (QAQ[i] != QAQ[j])
			return false;
	return true;
}
inline int dfs(int now)
{
	while(1)
	{
		QAQ[++QAQ[0]] = F[now];
		gone[now] = QAQ[0];
		if (gone[F[now]] > 0)
		{
			if (check(gone[F[now]] - 1)) return gone[F[now]] - 1;
			else return QAQ[0];
		}
		now = F[now];
	}
}
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &F[i]);
	for (int i = 1; i <= n; i++)
	{
		if (circ[F[i]] != 0)
		{
			if (circ[F[F[i]]] == circ[F[i]]) circ[i] = circ[F[i]];
			else circ[i] = circ[F[i]] + 1;
		}
		else
		{
			memset(QAQ, 0, sizeof(QAQ));
			memset(gone, 0, sizeof(gone));
			circ[i] = dfs(i);
		}
	}
	for (int i = 1; i <= n; i++) ans = (ans * circ[i]) / gcd(ans, circ[i]);
	printf("%d\n", ans);
	return 0;
}


/************************************************************
 * Author: Priyanshu Singh
 * Handle: tgamerz
 */

#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%f",&n)
#define slf(n)                      scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                         (int)1e9
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define F                           first
#define S                           second

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

#define DEBUG

// debugging
#ifdef DEBUG
#define trace1(x)                    cerr << #x << ": " << x << endl;
#define trace2(x, y)                 cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)              cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)           cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)        cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)     cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif

//dirextion
const int fx[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

// datatypes
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair< pii, int> piii;
typedef vector< piii > vpiii;
typedef pair<int, bool> pib;
typedef vector< pii > vpii;
typedef vector< pib > vpib;

const int MAXN = 210;
int f[MAXN];
bool chain[MAXN];
int length[MAXN];
void find_k(int i){
    int k = 1;
    int ini = i;
    if(f[i] == i){
        chain[i] = false;
        length[i] = k;
        return;
    }
    while(f[f[i]] != f[i] && f[i] != ini){
        k++;
        i = f[i];
    }
    chain[ini] = (f[i] != ini)? false : true;
    length[ini] = (f[i] != ini)? ceil(((double)k-1) / 2) + 1 : k;
    return ;
}

int main(){
    int N;
    s(N);
    forall(i, 1, N+1){
        s(f[i]);
    }
    int mx = 1;
    forall(i, 1, N+1){
        find_k(i);
    }
    int ans = 1;
    int mi = INF;
    bool flag = false;
    forall(i, 1, N+1){
        if(chain[i]){
            if(ans % length[i] != 0)ans *= (length[i] / gcd(ans,length[i]));
            mi = miN(mi, length[i]);
            flag = true;
        }
    }
    forall(i, 1, N+1){
        if(!chain[i] && flag){
            while(length[i] > ans)ans*=mi;
        }else if(!chain[i]){
            ans = maX(ans, length[i]);
        }
    }
    printf("%d\n",ans);
    return 0;
}


/************************************************************
 * Author: Priyanshu Singh
 * Handle: tgamerz
 */

#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%f",&n)
#define slf(n)                      scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                         (int)1e9
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define F                           first
#define S                           second

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

#define DEBUG

// debugging
#ifdef DEBUG
#define trace1(x)                    cerr << #x << ": " << x << endl;
#define trace2(x, y)                 cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)              cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)           cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)        cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)     cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif

//dirextion
const int fx[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

// datatypes
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair< pii, int> piii;
typedef vector< piii > vpiii;
typedef pair<int, bool> pib;
typedef vector< pii > vpii;
typedef vector< pib > vpib;

const int MAXN = 210;
int f[MAXN];
bool chain[MAXN];
int length[MAXN];
void find_k(int i){
    int k = 1;
    int ini = i;
    if(f[i] == i){
        chain[i] = false;
        length[i] = k;
        return;
    }
    while(f[f[i]] != f[i] && f[i] != ini){
        k++;
        i = f[i];
    }
    chain[ini] = (f[i] != ini)? false : true;
    length[ini] = (f[i] != ini)? ceil(((double)k) / 2) : k;
    return ;
}

int main(){
    int N;
    s(N);
    forall(i, 1, N+1){
        s(f[i]);
    }
    int mx = 1;
    forall(i, 1, N+1){
        find_k(i);
    }
    int ans = 1;
    int mi = INF;
    forall(i, 1, N+1){
        if(chain[i]){
            if(ans % length[i] != 0)ans *= (length[i] / gcd(ans,length[i]));
            mi = miN(mi, length[i]);
        }
    }
    forall(i, 1, N+1){
        if(!chain[i]){
            while(length[i] > ans)ans*=mi;
        }
    }
    printf("%d\n",ans);
    return 0;
}


/************************************************************
 * Author: Priyanshu Singh
 * Handle: tgamerz
 */

#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%f",&n)
#define slf(n)                      scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                         (int)1e9
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define F                           first
#define S                           second

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

#define DEBUG

// debugging
#ifdef DEBUG
#define trace1(x)                    cerr << #x << ": " << x << endl;
#define trace2(x, y)                 cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)              cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)           cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)        cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)     cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif

//dirextion
const int fx[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

// datatypes
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair< pii, int> piii;
typedef vector< piii > vpiii;
typedef pair<int, bool> pib;
typedef vector< pii > vpii;
typedef vector< pib > vpib;

const int MAXN = 210;
int f[MAXN];
ll chain[MAXN];
ll length[MAXN];

int temp[MAXN];
void find_k(int i){
    int k = 1;
    int ini = i;
    vi v;
    if(f[i] == i){
        chain[i] = 1;
        length[i] = 0;
        return;
    }
    fill(temp, 0);
    temp[ini] = k;
    v.pb(ini);
    while(true){
        k++;
        i = f[i];
        if(find(all(v),i) != v.end()){
            chain[ini] = k - temp[i];
            length[ini] = temp[i] - 1;
            return;
        }else{
            v.pb(i);
            temp[i] = k;
        }
    }
}

int main(){
    int N;
    s(N);
    forall(i, 1, N+1){
        s(f[i]);
    }
    int mx = 1;
    forall(i, 1, N+1){
        find_k(i);
 //       trace2(i,f[i])
    }
    ll ans = 1;
    ll mi = __LONG_LONG_MAX__;
    bool flag = false;
    forall(i, 1, N+1){
        //trace3(i, chain[i], length[i])
        if(chain[i] != 0){
            if(ans % chain[i] != 0)ans *= (chain[i] / gcd(ans,chain[i]));
            flag = true;
        }
    }
 //   trace2(mi, ans)
    forall(i, 1, N+1){
        if(length[i] > ans){
            if(flag){
                while(length[i] > ans)ans *= 2;
            }else{
                ans = length[i];
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}


/************************************************************
 * Author: Priyanshu Singh
 * Handle: tgamerz
 */

#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%f",&n)
#define slf(n)                      scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                         (int)1e9
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define F                           first
#define S                           second

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

#define DEBUG

// debugging
#ifdef DEBUG
#define trace1(x)                    cerr << #x << ": " << x << endl;
#define trace2(x, y)                 cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)              cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)           cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)        cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)     cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif

//dirextion
const int fx[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

// datatypes
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair< pii, int> piii;
typedef vector< piii > vpiii;
typedef pair<int, bool> pib;
typedef vector< pii > vpii;
typedef vector< pib > vpib;

const int MAXN = 210;
int f[MAXN];
ll chain[MAXN];
ll length[MAXN];

int temp[MAXN];
void find_k(int i){
    int k = 1;
    int ini = i;
    vi v;
    if(f[i] == i){
        chain[i] = 1;
        length[i] = 0;
        return;
    }
    fill(temp, 0);
    temp[ini] = k;
    v.pb(ini);
    while(true){
        k++;
        i = f[i];
        if(find(all(v),i) != v.end()){
            chain[ini] = k - temp[i];
            length[ini] = temp[i] - 1;
            return;
        }else{
            v.pb(i);
            temp[i] = k;
        }
    }
}

int main(){
    int N;
    s(N);
    forall(i, 1, N+1){
        s(f[i]);
    }
    int mx = 1;
    forall(i, 1, N+1){
        find_k(i);
    }
    ll ans = 1;
    ll mi = __LONG_LONG_MAX__;
    bool flag = false;
    forall(i, 1, N+1){
        //trace3(i, chain[i], length[i])
        if(chain[i] != 0){
            if(ans % chain[i] != 0)ans *= (chain[i] / gcd(ans,chain[i]));
            flag = true;
            if(chain[i] != 1){
                mi = miN(mi, chain[i]);
            }
        }
    }
    forall(i, 1, N+1){
        if(length[i] > ans){
            if(mi != __LONG_LONG_MAX__){
                while(length[i] > ans)ans *= mi;
            }else{
                ans = length[i];
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}


/************************************************************
 * Author: Priyanshu Singh
 * Handle: tgamerz
 */

#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%f",&n)
#define slf(n)                      scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                         (int)1e9
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define F                           first
#define S                           second

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

#define DEBUG

// debugging
#ifdef DEBUG
#define trace1(x)                    cerr << #x << ": " << x << endl;
#define trace2(x, y)                 cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)              cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)           cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)        cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)     cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif

//dirextion
const int fx[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

// datatypes
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair< pii, int> piii;
typedef vector< piii > vpiii;
typedef pair<int, bool> pib;
typedef vector< pii > vpii;
typedef vector< pib > vpib;

const int MAXN = 210;
int f[MAXN];
ll chain[MAXN];
ll length[MAXN];

int temp[MAXN];
void find_k(int i){
    int k = 1;
    int ini = i;
    vi v;
    if(f[i] == i){
        chain[i] = 1;
        length[i] = 0;
        return;
    }
    fill(temp, 0);
    temp[ini] = k;
    v.pb(ini);
    while(true){
        k++;
        i = f[i];
        if(find(all(v),i) != v.end()){
            chain[ini] = k - temp[i];
            length[ini] = temp[i] - 1;
            return;
        }else{
            v.pb(i);
            temp[i] = k;
        }
    }
}

int main(){
    int N;
    s(N);
    forall(i, 1, N+1){
        s(f[i]);
    }
    int mx = 1;
    forall(i, 1, N+1){
        find_k(i);
 //       trace2(i,f[i])
    }
    ll ans = 1;
    bool flag = false;
    forall(i, 1, N+1){
        //trace3(i, chain[i], length[i])
        if(chain[i] != 0){
            if(ans % chain[i] != 0)ans = (ans * chain[i] / gcd(ans,chain[i]));
            flag = true;
        }
    }
 //   trace2(mi, ans)

    forall(i, 1, N+1){
        if(length[i] > ans){
            if(flag){
                int mul = 1;
                while(length[i] > ans * mul)mul++;
                ans = ans*mul;
            }else{
                ans = length[i];
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}


/************************************************************
 * Author: Priyanshu Singh
 * Handle: tgamerz
 */

#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%f",&n)
#define slf(n)                      scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                         (int)1e9
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define F                           first
#define S                           second

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

#define DEBUG

// debugging
#ifdef DEBUG
#define trace1(x)                    cerr << #x << ": " << x << endl;
#define trace2(x, y)                 cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)              cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)           cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)        cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)     cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif

//dirextion
const int fx[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

// datatypes
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair< pii, int> piii;
typedef vector< piii > vpiii;
typedef pair<int, bool> pib;
typedef vector< pii > vpii;
typedef vector< pib > vpib;

const int MAXN = 210;
int f[MAXN];
ll chain[MAXN];
ll length[MAXN];

int temp[MAXN];
void find_k(int i){
    int k = 1;
    int ini = i;
    vi v;
    if(f[i] == i){
        chain[i] = 1;
        length[i] = 0;
        return;
    }
    fill(temp, 0);
    temp[ini] = k;
    v.pb(ini);
    while(true){
        k++;
        i = f[i];
        if(find(all(v),i) != v.end()){
            chain[ini] = k - temp[i];
            length[ini] = temp[i] - 1;
            return;
        }else{
            v.pb(i);
            temp[i] = k;
        }
    }
}

int main(){
    int N;
    s(N);
    forall(i, 1, N+1){
        s(f[i]);
    }
    int mx = 1;
    forall(i, 1, N+1){
        find_k(i);
 //       trace2(i,f[i])
    }
    ll ans = 1;
    bool flag = false;
    int val = 1;
    forall(i, 1, N+1){
        //trace3(i, chain[i], length[i])
        if(chain[i] != 0){
            if(ans % chain[i] != 0)ans = (ans * chain[i] / gcd(ans,chain[i]));
            flag = true;
        }
        val = maX(val, length[i]);
    }
    int mul =1 ;
    if(ans < val)while(ans * mul < val)mul++;
    printf("%lld\n",ans * mul);
    return 0;
}


/************************************************************
 * Author: Priyanshu Singh
 * Handle: tgamerz
 */

#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%f",&n)
#define slf(n)                      scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                         (int)1e9
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define F                           first
#define S                           second

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

#define DEBUG

// debugging
#ifdef DEBUG
#define trace1(x)                    cerr << #x << ": " << x << endl;
#define trace2(x, y)                 cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)              cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)           cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)        cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)     cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif

//dirextion
const int fx[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

// datatypes
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair< pii, int> piii;
typedef vector< piii > vpiii;
typedef pair<int, bool> pib;
typedef vector< pii > vpii;
typedef vector< pib > vpib;

const int MAXN = 210;
int f[MAXN];

int find_k(int i){
    int k = 1;
    int ini = i;
    while(f[f[i]] != f[i] && f[i] != ini){
        k++;
        i = f[i];
    }
    return k;
}

int main(){
    int N;
    s(N);
    forall(i, 1, N+1){
        s(f[i]);
    }
    int mx = 1;
    forall(i, 1, N+1){
        if(mx < find_k(i))mx = find_k(i);
    }
    printf("%d\n",mx);
    return 0;
}


/************************************************************
 * Author: Priyanshu Singh
 * Handle: tgamerz
 */

#include <cmath>
#include <climits>
#include <queue>
#include <vector>
#include <map>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <bitset>

using namespace std;
// Input macros
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%lld",&n)
#define sf(n)                       scanf("%f",&n)
#define slf(n)                      scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)

// Useful constants
#define INF                         (int)1e9
#define EPS                         1e-9

// Useful hardware instructions
#define bitcount                    __builtin_popcount
#define gcd                         __gcd

// Useful container manipulation / traversal macros
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define F                           first
#define S                           second

// Some common useful functions
#define maX(a,b)                     ( (a) > (b) ? (a) : (b))
#define miN(a,b)                     ( (a) < (b) ? (a) : (b))
#define checkbit(n,b)                ( (n >> b) & 1)
#define DREP(a)                      sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind)               (lower_bound(all(arr),ind)-arr.begin())

#define DEBUG

// debugging
#ifdef DEBUG
#define trace1(x)                    cerr << #x << ": " << x << endl;
#define trace2(x, y)                 cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)              cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)           cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)        cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)     cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
#else
#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)
#endif

//dirextion
const int fx[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
const int fxx[8][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1}};

// datatypes
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair< pii, int> piii;
typedef vector< piii > vpiii;
typedef pair<int, bool> pib;
typedef vector< pii > vpii;
typedef vector< pib > vpib;

const int MAXN = 210;
int f[MAXN];
bool chain[MAXN];
int length[MAXN];
void find_k(int i){
    int k = 1;
    int ini = i;
    if(f[i] == i){
        chain[i] = false;
        length[i] = k;
        return;
    }
    while(f[f[i]] != f[i] && f[i] != ini){
        k++;
        i = f[i];
    }
    chain[ini] = (f[i] != ini)? false : true;
    length[ini] = (f[i] != ini)? ceil(((double)k) / 2) : k;
    length[ini] = (!chain[ini] && length[ini] < 2)? length[ini]+1:length[ini];
    return ;
}

int main(){
    int N;
    s(N);
    forall(i, 1, N+1){
        s(f[i]);
    }
    int mx = 1;
    forall(i, 1, N+1){
        find_k(i);
    }
    int ans = 1;
    int mi = INF;
    bool flag;
    forall(i, 1, N+1){
        if(chain[i]){
            if(ans % length[i] != 0)ans *= (length[i] / gcd(ans,length[i]));
            mi = miN(mi, length[i]);
            flag = true;
        }
    }
    forall(i, 1, N+1){
        if(!chain[i] && flag){
            while(length[i] > ans)ans*=mi;
        }else if(!chain[i]){
            ans = maX(ans, length[i]);
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
   // freopen("a.txt","r",stdin);
    long long n;
    cin >> n;
    long long* f = new long long[n+1];
    long long d[201][5001];
    long long a[201],b[201];
    for(long long i=0;i<n;i++) cin >> f[i+1];
    vector<long long> v;
    for(long long i=1;i<=n;i++){
        long long c=i;
        for(long long j=1;j<5001;j++){
            d[i][j] = f[c];
            c = f[c];
        }
    }
    map<long long,long long> m;
    for(long long i=1;i<=n;i++){
        for(long long j=1;j<5001;j++){
            if(2*j<5001 && d[i][2*j]==d[i][j]){
                a[i]=j;
                for(long long k=j+1;2*k<5001;k++){
                    if(d[i][2*k]==d[i][k]){
                        b[i]=k-j;
                        break;
                    }
                }
                break;
            }
        }
    }

    long long k=0,l=1;
    for(long long i=1;i<=n;i++){
        long long aa = k,bb = a[i];
        while(aa!=bb){
            if(aa<bb){
                if(((bb-aa)/l)!=0) aa+=l*((bb-aa)/l);
                else aa+=l;
            }
            else{
                if(((aa-bb)/b[i])!=0) bb+=b[i]*((aa-bb)/b[i]);
                else bb+=b[i];
            }
        }
        k=aa;
        l = (l*b[i])/__gcd(l,b[i]);
    }
    cout << k << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define pb push_back
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    int n;
    cin >> n;
    int* f = new int[n+1];
    int d[201][10001];
    int a[201],b[201];
    for(int i=0;i<n;i++) cin >> f[i+1];
    vector<int> v;
    for(int i=1;i<=n;i++){
        int c=i;
        for(int j=1;j<10001;j++){
            d[i][j] = f[c];
            c = f[c];
        }
    }
    map<int,int> m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<10001;j++){
            if(2*j<10001 && d[i][2*j]==d[i][j]){
                a[i]=j;
                for(int k=j+1;2*k<10001;k++){
                    if(d[i][2*k]==d[i][k]){
                        b[i]=k-j;
                        break;
                    }
                }
                break;
            }
        }
    }

    int k=0,l=1;
    for(int i=1;i<=n;i++){
        int aa = k,bb = a[i];
        while(aa!=bb){
            if(aa<bb) aa+=l*((bb-aa)/l);
            else bb+=b[i]*((aa-bb)/b[i]);
        }
        k=aa;
        l = (l*b[i])/__gcd(l,b[i]);
    }
    //for(map<int,int>::iterator it = m.begin();it!=m.end();it++) if(it->second==n){k=it->first;break;}
    cout << k << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define pb push_back
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	//freopen("a.txt","r",stdin);
	int n;
	cin >> n;
	int* f = new int[n+1];
	int d[201][5001];
	for(int i=0;i<n;i++) cin >> f[i+1];
	vector<int> v;
	for(int i=1;i<=n;i++){
		int c=i;
		for(int j=1;j<5001;j++){
			d[i][j] = f[c];
			c = f[c];
		}
	}
	map<int,int> m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<5001;j++){
			if(2*j<5001 && d[i][2*j]==d[i][j]){
				m[j]++;
			}
		}
	}
	int k=0;
	for(map<int,int>::iterator it = m.begin();it!=m.end();it++) if(it->second==n){k=it->first;break;}
	cout << k << "\n";
	return 0;
}