#include <bits/stdc++.h>
int main() {
  int n, x, i, p;
  scanf("%d", &n);
  int ara[n];
  for (i = 0; i < n; i++) {
    scanf("%d", &ara[i]);
  }
  for (x = 0; x < n; x++) {
    p = ara[ara[ara[x] - 1] - 1];
    if (ara[p - 1] == ara[x]) {
      printf("YES\n");
      exit(0);
    }
  }
  printf("NO\n");
}
