#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010;
int n;
bool flag, vis[maxn];
vector<int> ve[maxn];
void dfs(int pos, int pre, int cnt) {
  vis[pos] = true;
  if (flag) return;
  for (int i = 0; i < ve[pos].size(); i++) {
    int v = ve[pos][i];
    if (cnt == 3 && vis[v] && v != pre) flag = true;
    if (cnt == 3 && (!vis[v] || v == pre)) continue;
    dfs(v, pos, cnt + 1);
  }
  return;
}
void check() {
  for (int i = 1; i <= n; i++) {
    memset(vis, 0, sizeof(vis));
    dfs(i, -1, 1);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    ve[i].push_back(temp);
  }
  check();
  if (flag)
    printf("YES");
  else
    printf("NO");
  return 0;
}
