#include <bits/stdc++.h>
using namespace std;
int ai[5001];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int a;
    scanf("%d", &a);
    ai[i] = a;
  }
  for (int i = 1; i <= n; i++) {
    if (ai[ai[ai[i]]] == i) {
      printf("YES\n");
      return 0;
    }
  }
  printf("NO\n");
  return 0;
}
