#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
vector<int> used;
bool bfs(int x) {
  queue<int> q;
  q.push(x);
  int i = 0;
  while (!q.empty() && i != 3) {
    int v = q.front();
    i++;
    if (i == 3) {
      if (g[v][0] == x) return 1;
    }
    q.pop();
    q.push(g[v][0]);
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  used.resize(n, 0);
  g.resize(n, vector<int>());
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    x--;
    g[i].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (bfs(i)) {
      cout << "YES";
      return 0;
    }
  }
  cout << "NO";
  return 0;
}
