#include <bits/stdc++.h>
using namespace std;
const double pi = 2 * acos(0);
int main() {
  int n;
  int data[5010];
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &data[i + 1]);
  bool notGet = false;
  for (int i = 1; i < n + 1; i++) {
    if (i == data[data[data[i]]]) {
      printf("YES");
      notGet = true;
      break;
    }
  }
  if (notGet == false) printf("NO");
  return 0;
}
