#include <bits/stdc++.h>
int N, arr[5000 + 5];
int fA, fB, fC;
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) scanf("%d", arr + i);
  for (int i = 1; i <= N; ++i) {
    fA = arr[i];
    fB = arr[fA];
    fC = arr[fB];
    if (fC == i) {
      printf("YES\n");
      return 0;
    }
  }
  printf("NO\n");
  return 0;
}
