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