#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline int sz(const T& x) {
  return (int)x.size();
}
template <typename T>
inline void mn(T& x, const T& y) {
  if (y < x) x = y;
}
template <typename T>
inline void mx(T& x, const T& y) {
  if (x < y) x = y;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
pair<int, int> calcBeforeAndPeriod(int x, const vector<int>& f) {
  map<int, int> order;
  for (int i = 0;; ++i, x = f[x]) {
    auto it = order.find(x);
    if (it != order.end()) {
      return {it->second, i - it->second};
    }
    order[x] = i;
  }
  return {-1, -1};
}
int main() {
  int n;
  scanf("%d", &n);
  vector<int> f(n);
  for (int i = 0; i < (n); ++i) {
    scanf("%d", &f[i]);
    f[i]--;
  }
  int maxBefore = 0;
  long long currLcm = 1;
  for (int i = 0; i < (n); ++i) {
    pair<int, int> beforeAndBeriod = calcBeforeAndPeriod(i, f);
    currLcm = lcm(currLcm, beforeAndBeriod.second);
    maxBefore = max(maxBefore, beforeAndBeriod.first);
  }
  long long ans = currLcm;
  while (ans < maxBefore) {
    ans += currLcm;
  }
  printf("%I64d\n", ans);
  return 0;
}
