#include <bits/stdc++.h>
using namespace std;
vector<int> v;
long long k;
long long ans = 1;
long long nok(long long a, long long b) {
  long long aa = a, bb = b;
  while (aa && bb)
    if (aa >= bb)
      aa %= bb;
    else
      bb %= aa;
  return a / (aa + bb) * b;
}
void dfs() {
  for (int i = 1; i < v.size(); ++i) {
    vector<bool> used;
    vector<int> path;
    used.assign(v.size(), false);
    int cur = i;
    path.push_back(cur);
    used[cur] = 1;
    while (!used[v[cur]]) {
      cur = v[cur];
      path.push_back(cur);
      used[cur] = 1;
    }
    long long kk = 0;
    while (path[kk] != v[path.back()]) ++kk;
    k = max(k, kk);
    ans = nok(ans, path.size() - kk);
  }
}
int main() {
  int n;
  cin >> n;
  v.resize(n + 1);
  for (int i = 1; i <= n; ++i) cin >> v[i];
  dfs();
  long long ans2 = ans;
  while (ans2 < k) ans2 += ans;
  cout << ans2;
}
