#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
int n,a[10001];
int t[10001];
int tn;
bool y[10001];
long long gcd(long long a,long long b)
{
if (a<b) swap(a,b);
if (b!=0) return gcd(b,a%b);
return a;
}
int main()
{
freopen("officer.in","r",stdin);
freopen("officer.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d",a+i);
}
for (int i=1;i<=n;i++)
if (!y[i])
{
int now=i;int k=0;
do
{
now=a[now];
y[now]=true;
k++;
}while(now!=i);
t[++tn]=k;
}
long long ans=t[1];
for (int i=2;i<=tn;i++)
{
ans=(ans/gcd(ans,t[i]))*t[i];
}
printf("%lld\n",ans);
return 0;
}