#include <bits/stdc++.h>
long long gcd(long long a, long long b) {
  for (;;) {
    if (a == 0) return b;
    b %= a;
    if (b == 0) return a;
    a %= b;
  }
}
long long lcm(long long a, long long b) {
  long long temp = gcd(a, b);
  return temp ? (a / temp * b) : 0;
}
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> f(N);
  vector<int> g(N);
  for (int i = 0; i < N; i++) {
    cin >> f[i];
    f[i]--;
  }
  int minv = 0;
  long long cycle = 1;
  int i, j, k;
  for (i = 0; i < N; i++) {
    int s = f[i];
    for (j = 0; j < N; j++) {
      int t = s;
      for (k = 0; k < N; k++) {
        t = f[t];
        if (s == t) {
          minv = max(minv, j);
          cycle = lcm(cycle, k + 1);
          j = N;
          break;
        }
      }
      s = f[s];
    }
  }
  cout << ((minv + cycle) / cycle) * cycle << endl;
}
