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