#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int n;
int temp[5005];
int main() {
scanf("%d", &n);
for (int i = 1;i <= n;++i) {
scanf("%d", &temp[i]);
}
bool flag = false;
for (int i = 1;i <= n;++i) {
if (temp[temp[temp[i]]] == i) {
flag = true;
break;
}
}
if (flag) {
printf("YES\n");
}
else {
printf("NO\n");
}
return 0;
}