#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, u[N], X;
bool f;
void Dfs(int x, int d) {
  if (d == 3) {
    if (x == X) f = 1;
    return;
  }
  Dfs(u[x], d + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &u[i]);
  for (int i = 1; i <= n; i++) X = i, Dfs(i, 0);
  if (f)
    puts("YES");
  else
    puts("NO");
  return 0;
}
