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