#include <bits/stdc++.h>
using namespace std;
int n, a;
vector<int> g[1000001];
int main() {
  cin >> n;
  for (int x = 1; x <= n; x++) {
    cin >> a;
    g[x].push_back(a);
  }
  for (int x = 1; x <= n; x++) {
    for (int y = 0; y < g[x].size(); y++) {
      for (int z = 0; z < g[g[x][y]].size(); z++) {
        for (int k = 0; k < g[g[g[x][y]][z]].size(); k++) {
          if (g[g[g[x][y]][z]][k] == x) {
            cout << "YES";
            return 0;
          }
        }
      }
    }
  }
  cout << "NO";
}
