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