#include <bits/stdc++.h>
using namespace std;
bool f[10000007];
vector<int> prime;
void sieve() {
  f[0] = f[1] = 1;
  for (int i = 2; i < 10000007; ++i) {
    if (f[i]) continue;
    prime.push_back(i);
    for (int j = i * i; i <= 10000 && j < 10000007; j += i) f[j] = 1;
  }
}
vector<int> factor;
void getFactor(int n) {
  for (int i = 2; i <= n / i; ++i) {
    while (n % i == 0) {
      factor.push_back(i);
      n /= i;
    }
  }
  if (n != 1) factor.push_back(n);
}
long long MaxSubarray(long long arr[], int n) {
  long long best = -1e18, sum = 0;
  for (int i = 0; i < n; ++i) {
    sum = max(arr[i], arr[i] + sum);
    best = max(best, sum);
  }
  return best;
}
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
long long fast_power(long long b, long long p) {
  long long res = 1;
  while (p) {
    if (p % 2) res = (res * b) % 1000000007;
    b = (b * b) % 1000000007;
    p /= 2;
  }
  return res;
}
int factorialTrailingZeros(int n) {
  int cnt = 0;
  for (int i = 5; n >= i; i *= 5) cnt += n / i;
  return cnt;
}
long long fac(int st, int en) {
  if (st > en) return -1;
  long long ret = 1;
  if (st == 0) ++st;
  for (int i = st; i <= en; ++i) ret *= i;
  return ret;
}
long long P(int n, int k) {
  if (n < k) return -1;
  if (n == 0) return 0;
  return fac(n - k + 1, n);
}
long long C(int n, int k) {
  if (n < k) return -1;
  int k1 = n - k;
  return fac(max(k1 + 1, k + 1), n) / fac(0, min(k1, k));
}
long long C_over(int n, int k) {
  if (n < k) return -1;
  long long ret = 1;
  int k1 = n - k;
  vector<int> a, b;
  for (int i = max(k1 + 1, k + 1); i <= n; ++i) a.push_back(i);
  for (int i = 2; i <= min(k1, k); ++i) b.push_back(i);
  for (int i = 0; i < a.size(); ++i) {
    for (int j = 0; j < b.size(); ++j) {
      int x = GCD(a[i], b[j]);
      a[i] /= x, b[j] /= x;
    }
  }
  for (int i = 0; i < a.size(); ++i) ret *= a[i];
  return ret;
}
template <typename T>
void printPQ(T& q) {
  while (!q.empty()) {
    std::cout << q.top() << " ";
    q.pop();
  }
  std::cout << '\n';
}
int n, arr[5005];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", arr + i);
  for (int i = 1; i <= n; ++i) {
    if (arr[arr[arr[i]]] == i) exit(puts("YES"));
  }
  puts("NO");
  return 0;
}
