#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5000;
int n;
int a[MAXN + 1];
void init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
}
bool check(int x) {
  int to = a[a[a[x]]];
  return to == x;
}
void solve() {
  for (int i = 1; i <= n; ++i)
    if (check(i)) {
      puts("YES");
      return;
    }
  puts("NO");
}
int main() {
  init();
  solve();
  return 0;
}
