#include <bits/stdc++.h>
using namespace std;
const int N = 5555;
inline void read(int &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = 10 * x + c - '0';
    c = getchar();
  }
  x *= f;
}
int n, a[N], vis[N];
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(a[i]);
  int ok = 0;
  for (int x = 1; x <= n; x++) {
    int y = a[x], z = a[y];
    ok |= (a[z] == x && x != y && x != z && y != z);
  }
  if (ok)
    puts("YES");
  else
    puts("NO");
  return 0;
}
