#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
const long long inf = 1e18;
int a[5050];
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int f = 0;
  for (int i = 1; i <= n; i++) {
    if (a[a[a[i]]] == i) {
      f = 1;
      break;
    }
  }
  if (f)
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}
