As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number f_{i}, where 1 ≤ f_{i} ≤ n and f_{i} ≠ i.

We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.


-----Input-----

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes.

The second line contains n integers f_1, f_2, ..., f_{n} (1 ≤ f_{i} ≤ n, f_{i} ≠ i), meaning that the i-th plane likes the f_{i}-th.


-----Output-----

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO».

You can output any letter in lower case or in upper case.


-----Examples-----
Input
5
2 4 5 1 3

Output
YES

Input
5
5 5 5 5 1

Output
NO



-----Note-----

In first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.

In second example there are no love triangles.

Difficulty: EASY
Tags: Graph algorithms

Starter code:

Solutions:
n = int(input())
a = list(map(int, input().split()))
f = 'NO'
for i in range(n):
	if a[a[a[i] - 1] - 1] - 1 == i:
		f = 'YES'
print(f)


R = lambda : list(map(int, input().split()))
n = int(input())
f = list(R())
d = {}
for i in range(n):
	d[i + 1] = f[i]

def solve(d, n):
	for i in range(n):
		b = d[i + 1]
		c = d[b]
		a = d[c]
		if i + 1 == a:
			return 'YES'
	return 'NO'
print(solve(d, n))


from typing import List
n: int = int(input())
arr: List = list(map(int, input().split()))
arr[:] = [x - 1 for x in arr]
f = 0
for i in range(n):
	j = arr[i]
	k = arr[j]
	l = arr[k]
	if i == l:
		f = 1
		break
if f == 1:
	print('YES')
else:
	print('NO')


input()
a = list(map(int, input().split()))
print('YNEOS'[1 - any([i == a[a[a[i] - 1] - 1] - 1 for i in range(len(a))])::2])


n = int(input())
F = list(map(int, input().split()))
F = [f - 1 for f in F]
for (a, b) in enumerate(F):
	c = F[b]
	if F[c] == a:
		print('YES')
		exit()
else:
	print('NO')


n = int(input())
f = [1, *map(int, input().split())]
print('YNEOS'[all((f[f[f[x]]] != x for x in f))::2])


n = int(input())
x = list(map(int, input().split()))
for i in range(n):
	if x[x[x[x[i] - 1] - 1] - 1] == x[i]:
		print('YES')
		exit()
	elif i == n - 1:
		print('NO')


n = int(input())
arr = list(map(int, input().split()))
for i in arr:
	a = arr[i - 1]
	b = arr[a - 1]
	if arr[b - 1] == i:
		print('YES')
		break
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
s = 'NO'
for i in range(1, n + 1):
	i1 = f[i - 1]
	i2 = f[i1 - 1]
	if i == f[i2 - 1]:
		s = 'YES'
		break
print(s)


n = int(input())
l = list(map(int, input().split()))
f = 0
for x in range(n):
	a = l[x]
	b = l[a - 1]
	c = l[b - 1]
	if c == x + 1 and a != b and (b != c):
		f = 1
		print('YES')
		break
if f == 0:
	print('NO')


import sys
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	b = a[i]
	c = a[b - 1]
	if a[c - 1] == i + 1:
		print('YES')
		exit()
print('NO')


R = lambda : map(int, input().split())
n = int(input())
edges = [0] + list(R())
for i in range(1, n + 1):
	nxt = edges[i]
	nnxt = edges[nxt]
	if edges[nnxt] == i:
		print('YES')
		exit(0)
print('NO')


from sys import stdin, stdout

def main():
	line = stdin.readline()
	line2 = stdin.readline()
	parts = line2.split()
	rta = False
	for i in range(0, len(parts)):
		b = int(parts[i]) - 1
		c = int(parts[b]) - 1
		if int(parts[c]) == i + 1:
			rta = True
			break
	if rta == True:
		stdout.write('YES')
	else:
		stdout.write('NO')
main()


n = int(input())
l = list(map(int, input().split()))
count = 0
for i in range(len(l)):
	l1 = l[i]
	l2 = l[l1 - 1]
	l3 = l[l2 - 1]
	if l1 != l2 and l1 != l3 and (l2 != l3) and (l1 == l[l3 - 1]):
		count += 1
		print('YES')
		break
if count == 0:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
flag = 0
for i in range(n):
	if a[a[a[i] - 1] - 1] == i + 1:
		print('YES')
		flag = 1
		break
if flag == 0:
	print('NO')


N = int(input())
arr = input().split()
arr = [int(c) for c in arr]
arr = [0] + arr
flag = False
for i in range(1, N + 1):
	if arr[arr[arr[i]]] == i:
		flag = True
if flag:
	print('YES')
else:
	print('NO')


input()
flag = False
l = [0] + list(map(int, input().split()))
for (i, j) in enumerate(l):
	x = l[j]
	y = l[x]
	if y == i and y != j:
		flag = True
		break
print({True: 'YES', False: 'NO'}[flag])


import sys, math
f = sys.stdin
n = int(f.readline())
l = map(int, f.readline().split())
l = [x - 1 for x in l]
found = False
for i in range(n):
	if l[i] != i and l[l[i]] != i and (l[l[i]] != l[i]) and (l[l[l[i]]] == i):
		found = True
		print('YES')
		break
if not found:
	print('NO')


V = int(input())
f = list(map(int, input().split()))
exist = False
for node in range(1, V + 1):
	if f[f[f[node - 1] - 1] - 1] == node:
		exist = True
if exist:
	print('YES')
else:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
d = {}
for i in range(n):
	d[i] = l[i] - 1
flag = 0
for i in range(n):
	if d[d[d[i]]] == i:
		flag = 1
		break
print('YES' if flag else 'NO')


def f(p):
	for i in range(len(p)):
		if p[p[p[i]]] == i:
			return True
	return False
input()
p = [int(x) - 1 for x in input().split(' ')]
if f(p):
	print('YES')
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
result = 'no'
if n > 2:
	for i in range(n - 2):
		if f[f[f[i] - 1] - 1] == i + 1:
			result = 'yes'
			break
print(result)


def main():
	n = int(input().strip())
	f = [0] * (n + 1)
	f[1:] = list(map(int, input().strip().split()))
	for i in range(1, n + 1):
		if i == f[f[f[i]]]:
			print('YES')
			return
	print('NO')
	return
main()


n = int(input())
l = list(map(int, input().split()))
for (i, a) in enumerate(l):
	b = l[a - 1]
	if b == i + 1:
		continue
	c = l[b - 1]
	if c == i + 1:
		print('YES')
		break
else:
	print('NO')


a = int(input())
x = list(map(int, input().split()))
sum = 0
for i in range(a):
	if x[x[x[i] - 1] - 1] == i + 1:
		print('YES')
		sum = 1
		break
if sum == 0:
	print('NO')


n = int(input())
i = list(map(int, input().split()))
a = {j + 1: i[j] for j in range(n)}
for p in a:
	t = a[p]
	y = a[t]
	if p == a[y]:
		print('YES')
		break
else:
	print('NO')


import sys
n = int(sys.stdin.readline())
line = sys.stdin.readline().split()
nums = list(map(int, line))
yes = False
for i in range(n):
	if nums[nums[nums[i] - 1] - 1] == i + 1:
		yes = True
if yes:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	x = a[i]
	y = a[x - 1]
	if a[y - 1] == i + 1:
		ans += 1
		break
if ans > 0:
	print('YES')
else:
	print('NO')


n = int(input())
planes = list(map(lambda x: int(x) - 1, input().split()))

def solve(n, planes):
	for (i, p) in enumerate(planes):
		np = planes[p]
		if planes[np] == i:
			return True
	return False
if solve(n, planes):
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
d = {}
l = {}
for i in range(n):
	d[i + 1] = a[i]
	if a[i] in d and d[a[i]] in d and (d[d[a[i]]] == i + 1):
		exit(print('YES'))
print('NO')


n = int(input())
s = input()
s = [int(x) for x in s.split(' ')]
dic = {}
for i in range(1, n + 1):
	dic[i] = s[i - 1]
flag = 0
for i in dic:
	if i == dic[dic[dic[i]]]:
		flag = 1
		break
if flag:
	print('YES')
else:
	print('NO')


import sys
input = sys.stdin.readline

def inpit():
	return int(input())

def inplt():
	return list(map(int, input().split()))

def inps():
	s = input()
	return list(s[:len(s) - 1])

def inpbit():
	return map(int, input().split())
n = inpit()
f = inplt()
p = True
for i in range(n):
	A = f[i]
	B = f[A - 1]
	C = f[B - 1]
	if i == C - 1:
		p = False
		print('YES')
		break
if p:
	print('No')


def rec(v, f, start, path_len=0):
	if path_len == 3:
		return start == v
	return rec(f[v], f, start, path_len + 1)

def solve(f):
	for v in range(len(f)):
		if rec(v, f, v):
			return 'YES'
	return 'NO'
input()
f = list(map(lambda x: int(x) - 1, input().split()))
print(solve(f))


n = int(input())
arr1 = []
can = False
arr1 = list(map(int, input().split()))
arr = []
arr.append(0)
for i in range(0, n):
	arr.append(arr1[i])
for i in range(1, n + 1):
	if arr[arr[arr[i]]] == i:
		can = True
		break
if can == True:
	print('YES')
else:
	print('NO')


n = int(input())
lovers = input().split(' ')
lovers = [int(i) for i in lovers]
graph = {}
vis = []
for i in range(1, n + 1):
	graph[i] = lovers[i - 1]
	vis.append(False)

def find_Tri(start, count, origin):
	if vis[start - 1] and count == 3 and (start == origin):
		return True
	elif vis[start - 1] and count != 3:
		return False
	count += 1
	vis[start - 1] = True
	d = find_Tri(graph[start], count, origin)
	vis[start - 1] = False
	return d
if n < 3:
	print('No')
else:
	out = 'No'
	for i in range(1, n + 1):
		if find_Tri(i, 0, i):
			out = 'Yes'
			break
	print(out)


def linp():
	return list(map(int, input().split(' ')))

def slinp():
	return sorted(linp())

def inp():
	return int(input())

def fnp():
	return float(input())

def dinp():
	s = linp()
	return (s[0], s[1])

def tinp():
	s = linp()
	return (s[0], s[1], s[2])

def main():
	n = inp()
	s = linp()
	s.insert(0, 0)
	for i in range(1, n + 1):
		if s[s[s[i]]] == i:
			print('YES')
			return
	print('NO')
t = 1
for i in range(t):
	main()


n = int(input())
f = list(map(int, input().split()))
f.insert(0, 0)
flag = 0
for i in range(1, n + 1):
	if f[f[f[i]]] == i:
		flag = 1
		break
if flag:
	print('YES')
else:
	print('NO')


n = int(input())
f = list([int(x) - 1 for x in input().split()])
for i in range(len(f)):
	if f[f[f[i]]] == i and f[f[i]] != i:
		print('YES')
		return
print('NO')


a = int(input())
b = list(map(int, input().split()))
for i in range(a):
	if b[b[b[i - 1] - 1] - 1] == i:
		print('YES')
		break
	if i == a - 1:
		print('NO')


n = int(input())
a = [0] * n
a = input().split()
for i in range(0, n):
	a[i] = int(a[i]) - 1
for i in range(0, n):
	if a[a[a[i]]] == i:
		print('YES')
		exit(0)
print('NO')


n = int(input())
vals = [0] + list(map(int, input().strip().split(' ')))
for i in range(1, n + 1):
	if vals[vals[vals[i]]] == i:
		print('YES')
		break
else:
	print('NO')


a = int(input())
b = [int(a) for a in input().split()]
(r, s) = (1, 0)
while r <= a:
	if b[b[b[r - 1] - 1] - 1] == r:
		s = s + 1
		break
	r = r + 1
if s > 0:
	print('YES')
else:
	print('NO')


n = int(input())
v = list(map(int, input().split()))
for i in range(n):
	v[i] -= 1
for i in range(n):
	if v[v[v[i]]] == i:
		print('YES')
		exit(0)
print('NO')


n = int(input())
a = [int(x) for x in input().split(' ')]
for i in range(n):
	if a[a[a[i] - 1] - 1] == i + 1:
		print('YES')
		break
else:
	print('NO')


n = int(input())
dic = {}
done = False
lst = list(map(int, input().split()))
for i in range(len(lst)):
	lst[i] = lst[i] - 1
for i in lst:
	if i == lst[lst[lst[i]]]:
		done = True
		print('YES')
		break
if done == False:
	print('NO')


def solve():
	global n, f
	for i in range(n):
		v = f[i] - 1
		for j in range(2):
			v = f[v] - 1
		if v == i:
			return 'YES'
	return 'NO'

def read_input():
	global n, f
	n = int(input())
	f = list(map(int, input().split()))

def main():
	read_input()
	print(solve())
main()


n = int(input())
(*a,) = map(int, input().split())
print('NYOE S'[any((i == a[a[a[i] - 1] - 1] - 1 for i in range(n)))::2])


n = int(input())
l = list(map(int, input().split()))
f = 0
for i in range(n):
	if l[l[l[i] - 1] - 1] == i + 1:
		f = 1
		print('YES')
		break
if f == 0:
	print('NO')


n = int(input())
nl = list(map(int, input().split()))
dic = {}
for i in range(n):
	dic[i + 1] = nl[i]
flag = False
for i in range(n):
	if dic.get(i + 1, 0) != 0:
		x = dic.get(i + 1)
		if dic.get(x, 0) != 0:
			y = dic.get(x)
			if dic.get(y, 0) != 0:
				if dic.get(y) == i + 1:
					print('YES')
					flag = True
					break
if not flag:
	print('NO')


from sys import stdin, stdout

def rint():
	return map(int, stdin.readline().split())
n = int(input())
f = list(rint())
f = [-1] + f
for i in range(1, n + 1):
	a = i
	b = f[a]
	c = f[b]
	d = f[c]
	if a == d:
		print('YES')
		return
print('NO')


n = int(input())
arr = list(map(int, input().split(' ')))
isTriangle = False
for i in range(n):
	triangleEnd = arr[arr[arr[i] - 1] - 1] - 1
	if triangleEnd == i:
		isTriangle = True
		break
if isTriangle:
	print('YES')
else:
	print('NO')


n = int(input())
planes = list(map(int, input().split()))
found = False
for i in range(n):
	a = i
	b = planes[a] - 1
	c = planes[b] - 1
	if a == planes[c] - 1:
		found = True
print('YES' if found == True else 'NO')


n = int(input())
a = [int(x) for x in input().split()]
d = {}
for i in range(n):
	d[i + 1] = a[i]
flag = 0
for i in range(1, n + 1):
	if i == d[d[d[i]]]:
		flag = 1
if flag:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
fl = True
for i in range(n):
	if i + 1 == a[a[a[i] - 1] - 1]:
		print('YES')
		fl = False
		break
if fl:
	print('NO')


import atexit
import io
import sys
import os
inf = float('inf')
inf_neg = float('-inf')
range_5 = int(100000.0 + 1)
range_6 = int(1000000.0 + 1)
range_7 = int(10000000.0 + 1)
range_8 = int(100000000.0 + 1)
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def main():
	n = int(input())
	arr = [0] + list(map(int, input().split()))
	for i in range(1, n):
		j = arr[i]
		k = arr[j]
		l = arr[k]
		if i != j and j != k and (k != i) and (l == i):
			print('YES')
			return
	print('NO')
main()


def solve(line, out):
	gi = lambda : list(map(int, line().split()))
	(n,) = gi()
	L = [0] + gi()
	out('YNEOS'[True not in [L[L[L[k]]] == k and k != L[k] and (L[k] != L[L[k]]) for k in L]::2])
solve(input, print)


t = int(input())
a = list(map(int, input().split()))
for i in range(t):
	x = i + 1
	y = a[x - 1]
	z = a[y - 1]
	if a[z - 1] == x:
		print('YES')
		exit(0)
print('NO')


input()
a = [1] + list(map(int, input().split()))
print('YES' if any((a[a[x]] == a.index(x) for x in a)) else 'NO')


n = int(input())
array = [0] * n
array = input().split()
for i in range(n):
	array[i] = int(array[i])
isanswer = False
for i in range(n):
	a = array[i]
	b = array[a - 1]
	if array[b - 1] == i + 1:
		isanswer = True
if isanswer == True:
	print('YES')
else:
	print('NO')


def inp():
	ls = list(map(int, input().split()))
	return ls
n = int(input())
ls = inp()
flag = True
for i in range(n):
	if ls[ls[ls[ls[i] - 1] - 1] - 1] == ls[i] and len(set([ls[i], ls[ls[i] - 1], ls[ls[ls[i] - 1] - 1]])) == 3:
		print('YES')
		flag = False
		break
if flag:
	print('NO')


numPlanes = int(input())
planes = list(map(int, input().split()))
currentPosition = 0
flag = True
for i in range(0, numPlanes):
	currIndexValue = planes[i]
	nextIndexValue = planes[currIndexValue - 1]
	lastIndexValue = planes[nextIndexValue - 1]
	if lastIndexValue - 1 == i:
		print('YES')
		flag = False
		break
if flag:
	print('NO')


from sys import stdin

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()
n = ii()
a = li()
a = [0] + a
for i in range(1, n + 1):
	if a[a[a[i]]] == i:
		print('YES')
		exit()
print('NO')


n = int(input())
count = 0
arr = list(map(int, input().split()))
for k in range(len(arr)):
	arr[k] = arr[k] - 1
for i in range(len(arr)):
	if arr[arr[arr[i]]] == i:
		count += 1
	else:
		continue
if count >= 1 or count == 1:
	print('YES')
else:
	print('NO')


def find(init, num, nums, counter, n):
	next_num = int(nums[num - 1])
	if next_num - 1 >= 0 and next_num - 1 <= n - 1:
		if counter == 3 and next_num == init:
			return True
		elif counter > 3:
			return False
		return find(init, next_num, nums, counter + 1, n)
	else:
		return False
n = int(input())
line = input()
nums = line.split()
flag = False
for num in nums:
	if find(int(num), int(num), nums, 1, n):
		flag = True
		break
	else:
		pass
if flag:
	print('YES')
else:
	print('NO')


n = int(input())
s = input()
k = s.split()
l = list(map(int, k))
for i in range(0, n):
	x = i + 1
	y = l[i]
	z = l[y - 1]
	if l[z - 1] == x:
		print('yes')
		break
	elif i == n - 1:
		print('no')


x = int(input())
s = [int(n) - 1 for n in input().split()]
for n in range(x):
	if s[s[s[n]]] == n:
		print('YES')
		exit(0)
print('NO')


n = int(input())
planes = list(input().split())
planes = list(map(lambda x: int(x), planes))
planes.insert(0, -1)
result = False
for i in range(1, n + 1):
	a = i
	b = planes[a]
	c = planes[b]
	if a == planes[c]:
		result = True
		break
print('YES') if result else print('NO')


input()
a = list(map(int, input().split()))
for i in range(0, len(a)):
	tmp1 = a[i]
	tmp2 = a[tmp1 - 1]
	tmp3 = a[tmp2 - 1]
	if a[tmp3 - 1] == tmp1:
		print('YES')
		exit()
print('NO')


n = input()
array = [int(i) for i in input().split()]
for i in range(0, len(array)):
	if array[array[array[i - 1] - 1] - 1] == i:
		print('YES')
		exit()
print('NO')


n = int(input())
a = [int(a) for a in input().split()]
d = dict()
for i in range(n):
	d[i] = a[i] - 1

def istriangle():
	for i in range(n):
		if d[d[d[i]]] == i:
			return True
			break
	return False
if istriangle() == True:
	print('Yes')
else:
	print('No')


b = int(input(''))
a = [int(x) for x in input().split()]
i = 1
c = 0
while i < b + 1:
	v = a[i - 1]
	x = a[v - 1]
	z = a[x - 1]
	if v != x and i == z:
		print('YES')
		c = 1
		break
	i += 1
if c == 0:
	print('NO')


def solve():
	n = int(input())
	A = list(map(int, input().split(' ')))
	for i in range(n):
		if i + 1 == A[A[A[i] - 1] - 1]:
			return 'YES'
	return 'NO'
print(solve())


n = int(input())
f = list(map(lambda _: int(_) - 1, input().split()))
print('YES' if any((f[f[f[i]]] == i for i in range(n))) else 'NO')


n = int(input())
f = [0]
for x in map(int, input().split()):
	f.append(x)
for i in range(1, n + 1):
	if f[f[f[i]]] == i:
		print('YES')
		exit()
print('NO')


n = int(input())
arr = [int(i) for i in input().split()]
isTriangle = False
for f in range(n):
	idx = f
	s = set()
	s.add(idx + 1)
	for count in range(3):
		nextOne = arr[idx]
		if nextOne in s:
			break
		s.add(nextOne)
		idx = nextOne - 1
	if nextOne == f + 1 and len(s) == 3:
		isTriangle = True
		break
if isTriangle:
	print('YES')
else:
	print('NO')


graph = {}
n = int(input())
L = list(map(int, input().split()))
for i in range(n):
	graph[i + 1] = L[i]
co = False
for each_key in graph:
	if graph[graph[graph[each_key]]] == each_key:
		co = True
		break
if co:
	print('YES')
else:
	print('NO')


n = int(input())
m = list(map(int, input().split()))
k = 0
for i in m:
	if m[m[m[i - 1] - 1] - 1] == i:
		k = 1
		break
if k:
	print('YES')
else:
	print('NO')


def solve(n, ar):
	for i in range(1, n + 1):
		a = i
		b = ar[i]
		c = ar[b]
		if a == ar[c]:
			print('YES')
			return
	print('NO')
	return
n = int(input())
ar = list(map(int, input().split()))
ar.insert(0, 0)
solve(n, ar)


n = int(input())
ar = list(map(int, input().split()))
for i in range(n):
	if ar[ar[ar[i] - 1] - 1] == i + 1:
		print('YES')
		break
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
ans = False
for i in f:
	ans = ans or f[f[f[i - 1] - 1] - 1] == i
print('YES' if ans else 'NO')


n = int(input())
x = list(map(int, input().split()))
for i in range(n):
	a = i + 1
	b = x[a - 1]
	c = x[b - 1]
	if x[c - 1] == a:
		print('YES')
		exit(0)
print('NO')


(a, a1, a2, a3) = ([], [], [], [])
sign = False
num = int(input())
a = [int(x) for x in input().split()]
for i in range(0, num):
	n1 = a[i]
	n2 = a[n1 - 1]
	n3 = a[n2 - 1]
	n4 = a[n3 - 1]
	if n1 == n4:
		sign = True
		break
if sign == True:
	print('YES')
else:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
(d, flag) = ({}, False)
for i in range(1, n + 1):
	d.update({i: l[i - 1]})
for i in range(1, n + 1):
	x = d[i]
	y = d[x]
	z = d[y]
	if i == z:
		flag = True
		break
if flag:
	print('YES')
else:
	print('NO')


try:
	n = int(input())
	arr = [int(i) for i in input().split()]
	flg = 1
	for i in range(n):
		if arr[arr[arr[i] - 1] - 1] == i + 1:
			flg = 0
			print('Yes')
			break
	if flg:
		print('NO')
except EOFError as e:
	print(e)


(n, a) = (int(input()), list(map(lambda x: int(x) - 1, input().split())))
for i in range(n):
	x = a[i]
	y = a[x]
	z = a[y]
	xx = a[z]
	if x == xx and x != y and (y != z):
		print('YES')
		exit(0)
print('NO')


def is_love_triangle(n, d):
	for i in range(n):
		e1 = (i + 1, d[i + 1])
		e2 = (e1[1], d[e1[1]])
		e3 = (e2[1], d[e2[1]])
		if e1[0] != e2[1] and e1[0] == e3[1]:
			return True
	return False
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	d[i + 1] = a[i]
if is_love_triangle(n, d):
	print('YES')
else:
	print('NO')


n = int(input())
g = list(map(lambda x: int(x) - 1, input().split()))
uv = set(range(n))
while uv:
	node = next(iter(uv))
	dist = 0
	rank = {node: dist}
	while True:
		dist += 1
		node = g[node]
		if node not in uv:
			break
		if node not in rank:
			rank[node] = dist
		elif dist - rank[node] == 3:
			print('YES')
			exit(0)
		else:
			break
	uv.difference_update(rank.keys())
print('NO')


import math
n = int(input())
f = list(map(int, input().split()))
for i in range(n):
	if f[f[f[i] - 1] - 1] == i + 1:
		print('YES')
		break
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
for i in range(n):
	cnt = 0
	j = i
	while cnt < 3:
		j = f[j] - 1
		cnt += 1
	if i == j:
		print('YES')
		exit()
print('NO')


import sys, math, operator, itertools, collections, heapq, bisect

class Solution:

	def __init__(self):
		pass

	def solve(self, *Input):
		(n, planes) = Input
		for (i, plane) in enumerate(planes):
			if planes[planes[plane - 1] - 1] == i + 1:
				return 'YES'
		return 'NO'

	def create(self, *Input):
		return None

	def check(self, *Input):
		pass

	def sieve_classic(self, a, b):
		self.primes = [True] * (b + 1)
		self.primes[0] = self.primes[1] = False
		for i in range(2, b + 1):
			if self.primes[i] and i * i <= b:
				for j in range(i * i, b + 1, i):
					self.primes[j] = False
		self.primes = [x for (x, y) in enumerate(self.primes) if y]
		ind = bisect.bisect_left(self.primes, a)
		self.primes[:ind] = []
		return None
solution = Solution()
inputs = iter(sys.stdin.readlines())
n = int(next(inputs))
planes = list(map(int, next(inputs).split()))
ans = solution.solve(n, planes)
print(ans)


def main():
	n = int(input())
	l = [0, *map(int, input().split())]
	for (i, a) in enumerate(l):
		if a != i == l[l[a]]:
			print('YES')
			return
	print('NO')
main()


N = int(input())
planes = dict(enumerate(list(map(int, input().split())), 1))
for i in planes.keys():
	if planes[planes[planes[i]]] == i:
		print('yes')
		exit(0)
print('no')


import sys
from sys import stdin, stdout
import bisect
import math
import heapq

def modinv(n, p):
	return pow(n, p - 2, p)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
import math

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def main():
	n = inin()
	a = ain()
	ans = 'NO'
	for i in range(n):
		if a[a[a[i] - 1] - 1] == i + 1:
			ans = 'YES'
	print(ans)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
	conv = ord if py2 else lambda x: x
	A = []
	numb = zero
	sign = 1
	i = 0
	s = sys.stdin.buffer.read()
	try:
		while True:
			if s[i] >= b'0'[0]:
				numb = 10 * numb + conv(s[i]) - 48
			elif s[i] == b'-'[0]:
				sign = -1
			elif s[i] != b'\r'[0]:
				A.append(sign * numb)
				numb = zero
				sign = 1
			i += 1
	except:
		pass
	if s and s[-1] >= b'0'[0]:
		A.append(sign * numb)
	return A
main()


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	j = a[i] - 1
	k = a[j] - 1
	if a[k] == i + 1:
		print('YES')
		exit(0)
print('NO')


n = int(input())
f = list(map(int, input().split()))
count = 0
for x in f:
	count += 1
	y = f.index(x)
	y += 1
	z = f[x - 1]
	w = f[z - 1]
	love_tri = {y: x, x: z, z: w}
	if w == y and x != y and (y != z) and (z != x):
		print('YES')
		break
if count == n:
	print('NO')


n = int(input())
arr = [int(x) for x in input().split()]
for x in range(len(arr)):
	if arr[x] == arr[arr[arr[arr[x] - 1] - 1] - 1]:
		print('YES')
		exit()
print('NO')


a = int(input())
b = [int(q) for q in input().strip().split()]
t = 0
for k in range(len(b)):
	m = k
	for r in range(3):
		n = b[m]
		m = n - 1
	if m == k:
		print('YES')
		t = 1
		break
if t == 0:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
count = [0, -1, -1]
flag = 'NO'
for i in range(n):
	while count[-1] == -1:
		count[1] = l[count[0]] - 1
		count[2] = l[count[1]] - 1
	if l[count[-1]] - 1 == count[0]:
		flag = 'YES'
		break
	else:
		count[0] += 1
		count[1] = -1
		count[2] = -1
print(flag)


n = int(input())
L = [int(x) for x in input().split()]
Has3cycle = False
for i in range(n):
	if i == L[L[L[i] - 1] - 1] - 1:
		Has3cycle = True
		break
print('YES' if Has3cycle else 'NO')


def checkLoveTriangle(graph):
	for i in range(len(graph)):
		if graph[graph[graph[i] - 1] - 1] - 1 == i:
			return True
	return False
n = int(input())
graph = list(map(int, input().split()))
if checkLoveTriangle(graph):
	print('YES')
else:
	print('NO')


a = input()
n = list(map(int, input().split()))
flag = False
for i in range(len(n)):
	if n[n[n[i] - 1] - 1] == i + 1:
		flag = True
		print('YES')
		break
if not flag:
	print('NO')


from collections import defaultdict
d = defaultdict(lambda : 0)
d = dict()
n = int(input())
l = list(map(int, input().split()))
flag = 1
for i in range(0, n):
	d[i + 1] = l[i]
for i in range(0, n):
	if d[d[d[i + 1]]] == i + 1:
		print('YES')
		flag = 0
		break
if flag == 1:
	print('NO')


import sys

def b():
	_input = sys.stdin.readlines()
	num = int(_input[0].strip())
	if num < 3:
		print('NO')
		return
	a = _input[1].strip().split(' ')
	a = [int(v) for v in a]
	for (i, val) in enumerate(a):
		if i + 1 == a[a[val - 1] - 1]:
			print('YES')
			return
	print('NO')
b()


n = input()
n = int(n)
s = {}
x = input()
x = x.split()
for i in range(n):
	t = int(x[i])
	s[i + 1] = t
t = 0
for i in range(1, n + 1):
	if i == s[s[s[i]]]:
		print('YES')
		t = -1
		break
	elif i == n and t != -1:
		print('NO')
		break


import sys
import os
import math
import copy
from math import gcd
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations

def Int():
	return int(sys.stdin.readline())

def Mint():
	return map(int, sys.stdin.readline().split())

def Lstr():
	return list(sys.stdin.readline().strip())

def Str():
	return sys.stdin.readline().strip()

def Mstr():
	return map(str, sys.stdin.readline().strip().split())

def List():
	return list(map(int, sys.stdin.readline().split()))

def Hash():
	return dict()

def Mod():
	return 1000000007

def Ncr(n, r, p):
	return fact[n] * (ifact[r] * ifact[n - r] % p) % p

def Most_frequent(list):
	return max(set(list), key=list.count)

def Mat2x2(n):
	return [List() for _ in range(n)]

def Divisors(n):
	l = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n // i == i:
				l.append(i)
			else:
				l.append(i)
				l.append(n // i)
	return l

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def minimum_integer_not_in_list(a):
	b = max(a)
	if b < 1:
		return 1
	A = set(a)
	B = set(range(1, b + 1))
	D = B - A
	if len(D) == 0:
		return b + 1
	else:
		return min(D)

def solution():
	n = Int()
	a = List()
	for i in range(n):
		if a[a[a[i] - 1] - 1] == i + 1:
			print('YES')
			sys.exit(0)
	print('NO')
solution()


import sys
input = sys.stdin.buffer.readline
n = int(input())
d = {}
arr = list(map(int, input().split()))
for i in range(1, n + 1):
	d[i] = arr[i - 1]
for i in range(1, n + 1):
	if d[d[d[i]]] == i:
		print('Yes')
		exit()
print('No')


num = int(input())
inp = input().split(' ')
graph = []
for i in inp:
	graph.append(int(i))
visited = []
exists = False
for i in range(num):
	if i in visited:
		continue
	visited.append(i)
	n = graph[i] - 1
	visited.append(n)
	n = graph[n] - 1
	visited.append(n)
	n = graph[n] - 1
	if n == i:
		exists = True
if exists:
	print('YES')
else:
	print('NO')


n = int(input())
A = [int(i) for i in input().split(' ')]
for i in range(n):
	A[i] -= 1
c = 0
for i in range(n):
	if i == A[A[A[i]]]:
		c = 1
if c:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().strip().split()))[:n]
for i in range(0, n):
	a[i] -= 1
f = False
for i in range(0, n):
	if a[a[a[a[i]]]] == a[i]:
		f = True
		break
if f:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(lambda x: int(x) - 1, input().split()))
ans = False
for i in range(n):
	if a[a[a[i]]] == i:
		ans = True
		break
print('YES' if ans else 'NO')


(n, a, b) = (int(input()), [int(x) - 1 for x in input().split()], 0)
for i in range(n):
	if a[a[a[i]]] == i:
		b = 1
		break
print('NYOE S'[b::2])


n = int(input())
planes = list(map(int, input().split()))
i = count = 0
while i < n:
	a = planes[i]
	b = planes[a - 1]
	c = planes[b - 1]
	d = planes[c - 1]
	if d == a:
		count = 1
		break
	else:
		i += 1
if count:
	print('YES')
else:
	print('NO')


def Tri_Love(arr):
	Tri_Found = 0
	for element in arr:
		element_1 = arr[element - 1]
		element_2 = arr[element_1 - 1]
		element_3 = arr[element_2 - 1]
		element_4 = arr[element_3 - 1]
		if element_1 == element_4:
			Tri_Found = 0
			break
		else:
			Tri_Found = 1
	if Tri_Found == 0:
		print('YES')
	else:
		print('NO')
i = int(input())
arr = arr = [int(i) for i in input().split()]
Tri_Love(arr)


n = int(input())
f = input().split(' ')
flag = False
for i in range(n):
	a = int(f[i])
	b = int(f[a - 1])
	c = int(f[b - 1])
	if i == c - 1:
		flag = True
		break
print('YES' if flag else 'NO')


import math
import queue
from collections import defaultdict

def pin():
	return int(input())

def mpin():
	return map(int, input().split())

def mpfl():
	return map(float, input().split())

def mpst():
	return map(str, input().split())

def lmpin():
	return list(map(int, input().split()))

def lmpfl():
	list(map(float, input().split()))

def lmpst():
	list(map(str, input().split()))

def mkg(edges):
	g = defaultdict(list)
	for edge in edges:
		(a, b) = (edge[0], edge[1])
		g[a].append(b)
		g[b].append(a)
	return g

def find_path(g, v, u, p=[]):
	p += [v]
	if v == u:
		return p
	if v not in g:
		return
	for node in g[v]:
		if node not in p:
			newp = find_path(g, node, u, p)
			if newp:
				return p
	return

def mpath(g, v, u):
	dis = {v: [v]}
	q = list([v])
	if v == u:
		return dis[v]
	while q:
		at = q[0]
		del q[0]
		for node in g[at]:
			if node not in dis:
				dis[node] = [*dis[v], node]
				q.append(node)
	return dis[u]
n = pin()
l = lmpin()
g = defaultdict(list)
a = 0
for i in range(n):
	g[i + 1].append(l[i])
	g[l[i]].append(i + 1)
for node in g:
	if len(g[node]) >= 2:
		for nodeo in g[node]:
			for nodet in g[nodeo]:
				if node in g[nodet]:
					print('YES')
					a = 1
					break
			if a == 1:
				break
		if a == 1:
			break
if a == 0:
	print('NO')


def read2():
	ent = input()
	line = input().split()
	return list(map(int, line))
A = [1, 2, 3, 4, 5]

def a(A):
	for i in range(len(A)):
		A[i] -= 1
	for i in range(len(A)):
		if A[A[A[i]]] == i:
			return 'YES'
	return 'NO'
A = [2, 4, 5, 1, 3]
print(a(read2()))


n = int(input())
data = list(map(int, input().split()))
ans = False
for i in data:
	try:
		if data[data[data[i - 1] - 1] - 1] == i:
			ans = True
	except Exception:
		pass
print('YES' if ans else 'NO')


n = int(input())
loves_list = list(map(int, input().split()))
loves = {i: loves_list[i - 1] for i in range(1, n + 1)}
exist = False
for i in range(1, n + 1):
	if loves[loves[loves[i]]] == i:
		exist = True
		break
print('YES' if exist else 'NO')


(nrplanes, planes, string) = (int(input(' ')), list(map(int, input().split(' '))), 'NO')
for i in range(nrplanes):
	if i + 1 == planes[planes[planes[i] - 1] - 1]:
		string = 'YES'
print(string)


def triangle(n, planes):
	for (i, v) in enumerate(planes):
		i2 = planes[v - 1] - 1
		v2 = planes[i2]
		if i + 1 == v2:
			return 'YES'
	return 'NO'
n = int(input())
planes = list(map(int, input().split()))
print(triangle(n, planes))


n = int(input())
k = list(map(int, input().split()))
l = []
for i in range(n):
	l.append([i, k[i] - 1])
f = 0
i = 0
while i < n and f != 1:
	if l[l[l[i][1]][1]][1] == i:
		f = 1
	i += 1
if f == 1:
	print('YES')
else:
	print('NO')


t = int(input())
flag = 0
l = list(map(int, input().split(' ')))
for i in range(1, t + 1):
	if l[l[l[i - 1] - 1] - 1] == i:
		print('YES')
		flag = 1
		break
if flag == 0:
	print('NO')


n = int(input())
lst = [int(x) for x in input().split()]
FOUND = False
for i in range(n):
	ferst = i
	second = lst[i] - 1
	therte = lst[second] - 1
	begin = lst[therte] - 1
	if begin == ferst and second != ferst and (begin != therte) and (second != therte):
		print('YES')
		FOUND = True
		break
if FOUND == False:
	print('NO')


n = int(input())
arr = [int(x) for x in input().split()]
temp = 0
for j in range(0, len(arr), 1):
	x = arr[j]
	i = x - 1
	count = 1
	while count != 3:
		y = arr[i]
		count += 1
		i = arr[i] - 1
	if arr[i] == x:
		print('YES')
		temp = 1
		break
if temp != 1:
	print('NO')


n = int(input())
p = list(map(int, input().split()))
r = 0
for i in range(n):
	if p[p[p[i] - 1] - 1] - 1 == i and p[p[i] - 1] - 1 != i:
		r = 1
		print('YES')
		break
if r == 0:
	print('NO')


(n, flag) = (int(input()), 0)
nums = list(map(int, input().split()))
nums = [nums[i] - 1 for i in range(n)]
for i in range(n):
	if nums[nums[nums[i]]] == i:
		flag = 1
print('YES' if flag else 'No')


import sys, os.path
from collections import *
from copy import *
import math
mod = 10 ** 9 + 7
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
n = int(input())
f = list(map(int, input().split()))
c = 0
i = 0
j = 0
flag = 0
for i in range(n):
	if f[f[f[i] - 1] - 1] == i + 1:
		flag = 1
		break
if flag == 1:
	print('YES')
else:
	print('NO')


n = int(input())
l = [int(i) for i in input().split()]
f = False
for i in range(n):
	if l[i] != l[l[i] - 1] and l[l[i] - 1] != l[l[l[i] - 1] - 1] and (l[i] == l[l[l[l[i] - 1] - 1] - 1]) and (l[i] != l[l[l[i] - 1] - 1]):
		f = True
		break
if f:
	print('YES')
else:
	print('NO')


n = int(input())
lista_amores = [int(a) for a in input().split()]
nodos = [i for i in range(1, n + 1)]
nodo_forma_triangulo = False
for nodo in nodos:
	nodo2 = lista_amores[nodo - 1]
	nodo3 = lista_amores[nodo2 - 1]
	nodo4 = lista_amores[nodo3 - 1]
	if nodo4 == nodo:
		nodo_forma_triangulo = True
		break
if nodo_forma_triangulo == True:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
for i in range(len(a)):
	if a[a[i] - 1] - 1 != i and a[a[a[i] - 1] - 1] - 1 == i:
		print('YES')
		return
print('NO')


def main():
	n = int(input())
	a = [int(x) - 1 for x in input().split()]
	for i in range(n):
		if a[a[a[i]]] == i:
			print('YES')
			return
	print('NO')
main()


n = int(input())
x = list(map(lambda x: int(x) - 1, input().split()))
if len(set(x)) <= 2:
	print('NO')
	exit()
flag = None

def dfs(i, lis=[]):
	global flag
	if x[i] in lis:
		if len(lis) == 2 and x[i] == lis[0]:
			flag = True
		return
	dfs(x[i], lis + [i])
for i in range(len(x)):
	dfs(i)
	if flag:
		print('YES')
		exit()
print('NO')


n = int(input())
f = list(map(int, input().split()))
exists = False
for i in range(len(f)):
	if f[f[f[i] - 1] - 1] == i + 1:
		exists = True
		print('YES')
		break
if not exists:
	print('NO')


n = int(input())
m = [int(s) for s in input().split()]
for i in range(n):
	if i == m[m[m[i] - 1] - 1] - 1:
		print('YES')
		exit()
print('NO')


l = int(input())
tab = list(map(int, input().split()))
rep = 'NO'
for i in range(l):
	if tab[tab[tab[i] - 1] - 1] == i + 1:
		rep = 'YES'
		break
print(rep)


n = int(input())
fs = map(int, input().split())
g = {}
for (i, fi) in enumerate(fs):
	g[i + 1] = fi
for i in g.keys():
	fi = g[i]
	ffi = g[fi]
	fffi = g[ffi]
	if fffi == i:
		print('YES')
		exit()
print('NO')


def ans():
	d = {}
	n = int(input())
	l = [int(x) for x in input().split()]
	for i in range(n):
		d[i + 1] = l[i]
	for i in d:
		if i == d[d[d[i]]]:
			print('YES')
			return
	print('NO')
	return
ans()


cases = int(input())
arr = list(map(int, input().split()))
arr = [i - 1 for i in arr]
for i in arr:
	one = arr[i]
	two = arr[one]
	three = arr[two]
	if three == i:
		print('YES')
		exit()
print('NO')


n = int(input())
x = input().split()
for i in range(n):
	x[i] = int(x[i])
y = [0]
y.extend(x)
c = 0
for i in range(1, n + 1):
	if y[y[y[i]]] == i:
		c += 1
if c > 0:
	print('YES')
else:
	print('NO')


n = int(input())
mas = list(map(int, input().split()))
good = False
for i in range(n):
	A = i + 1
	B = mas[i]
	C = mas[B - 1]
	D = mas[C - 1]
	if A == D and A != B != C:
		good = True
		break
if good:
	print('YES')
else:
	print('NO')


n = int(input())
fi = [0] + list(map(int, input().split(' ')))
ans = 0
for i in range(1, n + 1):
	if i == fi[fi[fi[i]]]:
		ans = 1
if ans:
	print('YES')
else:
	print('NO')


def solution(n, fs):
	for a in range(n):
		b = fs[a] - 1
		c = fs[b] - 1
		if fs[c] - 1 == a:
			return True
	return False
n = int(input())
fs = list(map(int, input().split()))
print('YES' if solution(n, fs) else 'NO')


getInputList = lambda : list(input().split())
getInputIntList = lambda : list(map(int, input().split()))
n = int(input())
a = getInputIntList()
t = True
for i in range(n):
	test = set([])
	test.add(i)
	test.add(a[i] - 1)
	test.add(a[a[i] - 1] - 1)
	if a[i] == a[a[a[a[i] - 1] - 1] - 1] and len(test) == 3:
		print('YES')
		t = False
		break
if t:
	print('NO')


def tri(ar, n):
	for i in range(n):
		if ar[ar[ar[i]]] == i:
			return 'YES'
	return 'NO'
love = []
u = int(input())
t = input().split(' ')
for i in range(u):
	love.append(int(t[i]) - 1)
print(tri(love, u))


import sys, math
n = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))
flag = False
for i in range(n):
	if a[a[a[i] - 1] - 1] == i + 1:
		flag = True
		break
if flag:
	print('YES')
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
for i in range(n):
	a = i
	b = f[i] - 1
	c = f[b] - 1
	d = f[c] - 1
	if d == a and a != b and (b != c) and (c != d):
		print('YES')
		exit()
print('NO')


n = int(input())
f = [int(k) - 1 for k in input().split(' ') if k]
for i in range(n):
	if f[i] != i and f[f[i]] != i and (f[f[f[i]]] == i):
		print('YES')
		exit()
print('NO')


N = int(input())
F = list(map(lambda x: int(x) - 1, input().split()))
ok = True
for (i, f) in enumerate(F):
	cur = i
	for j in range(3):
		cur = F[cur]
	if cur == i:
		ok = False
		break
print('yes' if not ok else 'no')


def read_nums():
	return list(map(int, input().strip().split()))

def read_int():
	return read_nums()[0]

def solve(nums):
	for (i, v) in enumerate(nums):
		if i == 0:
			continue
		if nums[nums[v]] == i:
			return True
	return False
t = read_int()
nums = read_nums()
if solve([-1] + nums):
	print('YES')
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
tmp1 = 0
tmp2 = 0
for i in range(0, n):
	tmp1 = f[i]
	tmp2 = f[tmp1 - 1]
	if i + 1 == f[tmp2 - 1]:
		print('YES')
		exit()
print('NO')


def love(f, n, l):
	global ar
	if l > 3:
		return 0
	if l == 3 and f == n:
		return 1
	return love(f, ar[n], l + 1)
a = int(input())
ar = [0] + list(map(int, input().split()))
f = 0
for i in range(1, a + 1):
	if love(i, ar[ar[i]], 2):
		f = 1
		break
print('YES' if f else 'NO')


n = int(input())
L = [0] + list(map(int, input().split()))
if any((L[L[L[x]]] == x for x in L[1:])):
	print('YES')
else:
	print('NO')


n = int(input())
f = {}
for (i, a) in enumerate(map(int, input().split())):
	f[i + 1] = a
for i in f.keys():
	a = f[i]
	b = f[a]
	c = f[b]
	if i == c:
		print('YES')
		break
else:
	print('NO')


import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = [int(i) - 1 for i in input().split()]
flag = 0
for i in range(n):
	x = i
	y = a[i]
	z = a[y]
	zz = a[z]
	if x == zz:
		print('YES')
		flag = 1
		break
if flag == 0:
	print('NO')


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
from math import ceil, floor, sqrt, log2, gcd
from heapq import heappush, heappop
abc = 'abcdefghijklmnopqrstuvwxyz'
n = int(input())
arr = list(map(int, input().split()))
i = 1
d = defaultdict(int)
for val in arr:
	d[i] = val
	i += 1
flag = 0
for i in arr:
	if d[d[d[i]]] == i:
		flag = 1
		break
if flag == 1:
	print('YES')
else:
	print('NO')


a = int(input())
l = list(map(int, input().split()))
for i in range(a):
	if l[l[l[i] - 1] - 1] == i + 1:
		print('YES')
		exit()
print('NO')


n = int(input().strip())
f = list(map(int, input().strip().split()))
assert len(f) == n
f = dict(zip(range(1, n + 1), f))
count = 0
for i in range(1, n + 1):
	if i == f[f[f[i]]]:
		print('YES')
		count = 1
		break
if count == 0:
	print('NO')


n = int(input())
A = list(map(int, input().split()))
f = False
i = 0
while i < n and (not f):
	if i == A[A[A[i] - 1] - 1] - 1:
		f = True
	i += 1
if f:
	print('YES')
else:
	print('NO')


n = int(input())
lst = [int(x) for x in input().split()]
s = 0
for i in range(len(lst) - 1):
	index1 = i + 1
	znach1 = lst[i]
	index2 = znach1
	znach2 = lst[znach1 - 1]
	index3 = znach2
	znach3 = lst[znach2 - 1]
	if index1 == znach3 and index2 == znach1 and (znach2 == index3):
		print('YES')
		s += 1
		break
if s == 0:
	print('NO')


input()
a = [int(i) for i in input().split()]
a.insert(0, 0)
for i in range(1, len(a)):
	if a[a[a[i]]] == i:
		print('YES')
		exit(0)
print('NO')


def loveTriangle():
	pN = int(input())
	fL = list(map(int, input().split()))
	for i in range(pN):
		tempL = []
		tempL.append(fL[i])
		x1 = fL[fL[i] - 1]
		tempL.append(fL[fL[i] - 1])
		x2 = fL[x1 - 1]
		tempL.append(x2)
		x3 = fL[x2 - 1]
		tempL.append(x3)
		if len(set(tempL)) == 3 and tempL[0] == tempL[3]:
			return 'YES'
	return 'NO'
ans = loveTriangle()
print(ans)


def run():
	input()
	s = [int(x) for x in input().split()]
	for i in range(len(s)):
		s1 = s[i]
		s2 = s[s1 - 1]
		s3 = s[s2 - 1]
		if s3 - 1 == i:
			print('YES')
			return
	print('NO')
run()


n = int(input())
love = [int(c) for c in input().split()]
for i in range(1, n + 1):
	a = love[i - 1]
	b = love[a - 1]
	c = love[b - 1]
	last = love[c - 1]
	if a != c and last == a:
		print('YES')
		break
else:
	print('NO')


from collections import defaultdict
graph = defaultdict(list)
n = int(input())
arr = list(map(int, input().split()))
for i in range(1, n + 1):
	graph[i] = arr[i - 1]
flag = False
for i in range(1, n + 1):
	if graph[graph[graph[i]]] == i:
		print('YES')
		flag = True
		break
if not flag:
	print('NO')


s = int(input())
a = [int(i) for i in input().split()]
for (idx, num) in enumerate(a):
	if a[a[num - 1] - 1] == idx + 1:
		print('YES')
		exit()
print('NO')


n = int(input())
f = list(map(int, input().split()))
d = 0
for i in range(n):
	a = f[i]
	b = f[a - 1]
	c = f[b - 1]
	if c == i + 1:
		d += 1
		break
if d == 0:
	print('NO')
else:
	print('YES')


def readn():
	return int(input())

def readlist():
	return list(map(int, input().split()))

def readnos():
	return map(int, input().split())
n = readn()
a = readlist()
a = [0] + a
flag = 0
for i in range(1, n + 1):
	y = a[i]
	z = a[y]
	w = a[z]
	if w == i:
		flag = 1
		break
if flag == 1:
	print('YES')
else:
	print('NO')


n = input()
seq = input()
flag = 0
numbers = seq.split()
for (i, num) in enumerate(numbers):
	v1 = int(numbers[i])
	v2 = int(numbers[v1 - 1])
	v3 = int(numbers[v2 - 1])
	if int(numbers[v3 - 1]) == v1:
		flag = 1
		break
print('YES' if flag == 1 else 'NO')


n = input()
l = list(map(lambda x: int(x), input().split(' ')))
triangle = False
for (idx, i) in enumerate(l):
	f1 = l[i - 1]
	f2 = l[f1 - 1]
	if idx == f2 - 1:
		triangle = True
		break
if triangle:
	print('YES')
else:
	print('NO')


n = int(input())
A = list(map(int, input().split()))
ans = 'NO'
for i in range(n):
	if A[A[A[i] - 1] - 1] == i + 1:
		ans = 'YES'
		break
print(ans)


n = int(input())
ln = [int(i) for i in input().split(' ')]
i = 1
sa = []
lt = False
wds = ['NO', 'YES']
for i in range(1, len(ln) + 1):
	while True:
		sa.append(i)
		i = ln[i - 1]
		if i in sa:
			if len(sa) - sa.index(i) == 3:
				lt = True
			sa = []
			i = 1
			break
print(wds[lt])


n = int(input())
i = list(map(int, input().split()))
for idx in range(1, len(i) + 1):
	if idx != i[idx - 1] or i[idx - 1] != i[i[idx - 1] - 1] or idx != i[i[idx - 1] - 1]:
		if i[i[i[idx - 1] - 1] - 1] == idx:
			print('YES')
			break
		continue
else:
	print('NO')


n = int(input())
li = list(map(int, input().split()))
c = 0
for i in range(len(li)):
	li[i] -= 1
for i in range(len(li)):
	if li[li[li[i]]] == i:
		c += 1
		break
if c == 0:
	print('NO')
else:
	print('YES')


n = int(input())
inp = [int(i) for i in input().split()]
for (idx, val) in enumerate(inp):
	if inp[inp[inp[val - 1] - 1] - 1] == val:
		print('YES')
		break
else:
	print('NO')


(n, F) = (int(input()), list(map(int, input().split())))
print('YES' if any((F[F[F[i] - 1] - 1] == i + 1 for i in range(n))) else 'NO')


from collections import defaultdict
n = int(input())
f = list(map(int, input().split()))
flag = False
for i in range(n):
	if f[f[f[i] - 1] - 1] == i + 1:
		flag = True
		break
if flag:
	print('YES')
else:
	print('NO')


t = int(input())
x = [int(q) for q in input().split()]
y = 0
for i in range(len(x)):
	a = x[i]
	b = x[a - 1]
	c = x[b - 1]
	d = x[c - 1]
	if a == d:
		y += 1
if y > 0:
	print('YES')
else:
	print('NO')


n = int(input())
planes = list(map(int, input().split()))
yep = 0
for i in range(n - 1):
	point1 = planes[i]
	point2 = planes[point1 - 1]
	if planes[point2 - 1] != i:
		point3 = planes[point2 - 1]
		if point3 - 1 == i:
			yep += 1
if yep >= 1:
	print('YES')
else:
	print('NO')


(n,) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
flag = 0
for i in range(n):
	j = i
	st = set()
	while j not in st:
		st.add(j)
		j = a[j] - 1
		cur = j
	if len(st) == 3 and cur == i:
		print('YES')
		flag = 1
		break
if flag == 0:
	print('NO')


n = int(input())
ls = list(map(int, input().split(' ')))
flag = []
for i in range(n):
	if ls[ls[ls[i] - 1] - 1] == i + 1:
		flag.append(1)
	else:
		flag.append(0)
if flag.count(1) != 0:
	print('YES')
else:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
done = 0
for i in range(n):
	l1 = l[i]
	l2 = l[l1 - 1]
	l3 = l[l2 - 1]
	l4 = l[l3 - 1]
	if l1 == l4:
		print('YES')
		done = 1
		break
if done == 0:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
d = dict()
for i in range(len(l)):
	d[i + 1] = l[i]
i = 0
flag = 1
while i <= n:
	i += 1
	try:
		if d[d[d[i]]] == i:
			print('YES')
			flag = 0
			break
	except:
		pass
if flag:
	print('NO')


n = int(input())
f = list(map(int, input().strip().split()))
f = list(map(lambda x: x - 1, f))
for a in range(n):
	b = f[a]
	c = f[b]
	x = f[c]
	if a == x:
		print('YES')
		exit(0)
print('NO')


def solve(test):
	n = int(input())
	a = list(map(int, input().split()))
	ok = False
	if n > 2:
		for i in range(n):
			if a[a[a[i] - 1] - 1] - 1 == i:
				ok = True
	print('YES' if ok else 'NO')
t = 1
for _ in range(t):
	solve(_ + 1)


n = int(input())
t = [int(w) for w in input().split()]
q = 0
for i in range(n):
	x = i
	a = t[x] - 1
	y = a
	b = t[y] - 1
	z = b
	c = t[z] - 1
	if c == x:
		print('yes'.upper())
		q = 1
		break
if q == 0:
	print('NO')


___ = int(input())
_ = list(map(int, input().split()))
for __ in range(___):
	if __ + 1 == _[_[_[__] - 1] - 1]:
		print('YES')
		exit()
else:
	print('NO')


n = int(input())
arr = [int(x) for x in input().split()]
for (i, val) in enumerate(arr):
	if arr[arr[val - 1] - 1] - 1 == i:
		print('YES')
		break
else:
	print('NO')


def main():
	N = int(input())
	A = list(map(int, input().split()))
	A = [0] + A
	flag = False
	for i in range(1, N + 1):
		if A[A[A[i]]] == i:
			flag = True
			break
	if flag:
		print('YES')
	else:
		print('NO')
main()


length = int(input())
numbers = input()
numbers = numbers.split()
x = False
for i in range(len(numbers)):
	numbers[i] = int(numbers[i]) - 1
for i in range(length):
	if numbers[numbers[numbers[i]]] == i:
		x = True
if x:
	print('YES')
else:
	print('NO')


n = int(input())
crush = list(map(int, input().split()))
for i in range(0, n):
	c1 = crush[i]
	c2 = crush[c1 - 1]
	if crush[c2 - 1] - 1 == i:
		print('YES')
		exit(0)
print('NO')


n = int(input())
a = list(map(int, input().split()))
for i in range(len(a)):
	if a[a[a[a[i] - 1] - 1] - 1] == a[i]:
		print('YES')
		exit()
else:
	print('NO')


n = int(input())
g = list(map(int, input().split()))
for j in range(n):
	look = j
	for i in range(3):
		look = g[look] - 1
	if look == j:
		print('YES')
		break
else:
	print('NO')


n = int(input())
data = input().split(' ', n)
f = []
for i in range(n):
	t = int(data[i])
	f.append(t)
flag = True
for i in range(n):
	if f[f[f[i] - 1] - 1] == i + 1:
		print('YES\n')
		flag = False
		break
if flag:
	print('NO\n')


N = int(input())
like = [int(x) for x in input().split(' ')]
flag = False
for (i, x) in enumerate(like):
	if like[like[x - 1] - 1] - 1 == i:
		print('Yes')
		flag = True
		break
if not flag:
	print('No')


import sys

def solve(n, adj_list):
	visited = [-1] * n
	parent = [-1] * n

	def _dfs(node):
		visited[node] = 1
		neighbor = adj_list[node] - 1
		if visited[neighbor] == -1:
			parent[neighbor] = node
			if _dfs(neighbor):
				return True
		elif neighbor == parent[parent[node]]:
			return True
		return False
	for i in range(n):
		if visited[i] == -1:
			if _dfs(i):
				print('YES')
				return
	print('NO')
	return
n = int(input())
neighbors = list(map(int, input().split(' ')))
solve(n, neighbors)


n = int(input())
list1 = [int(i) for i in input().split()]
flag = False
for x in range(n):
	if list1[list1[list1[x] - 1] - 1] - 1 == x:
		flag = True
		break
if flag:
	print('YES')
else:
	print('NO')


t = int(input())
l = list(map(int, input().split()))
count = 0
for i in range(t):
	j = l[i]
	k = l[j - 1]
	h = l[k - 1]
	if i + 1 == h:
		print('YES')
		count = 1
		break
if count == 0:
	print('NO')


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
n = Int()
a = array()
d = {}
over = set()
for i in range(n):
	d[i + 1] = a[i]
for i in a:
	if d[d[d[i]]] == i:
		print('YES')
		exit()
print('NO')


from collections import defaultdict

def int_lst_input():
	return [int(val) for val in input().split()]

def int_input():
	return int(input())

def print_lst(lst):
	print(' '.join([str(val) for val in lst]))

def solve():
	n = int_input()
	a = int_lst_input()
	found = False
	for i in range(1, n + 1):
		origin = a[i - 1]
		origin_index = i
		seen = set()
		j = i
		cycle_len = 0
		while a[j - 1] not in seen:
			seen.add(a[j - 1])
			j = a[j - 1]
			cycle_len += 1
		if j == origin_index and cycle_len == 3:
			print('YES')
			found = True
			break
	if not found:
		print('NO')
solve()


n = int(input())
fs = [int(i) - 1 for i in input().split()]
for i in range(n):
	if fs[fs[fs[i]]] == i:
		print('YES')
		break
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
res = False
i = 0
while i < n and (not res):
	b = a[i]
	c = a[b - 1]
	if a[c - 1] == i + 1:
		res = True
	i += 1
if res:
	print('yes')
else:
	print('no')


n = int(input())
arr = list(map(int, input().strip().split(' ')))
arr = [0] + arr
flag = 0
for i in range(1, n + 1):
	a = arr[i]
	b = arr[a]
	if i == arr[b]:
		flag = 1
		break
if flag:
	print('YES')
else:
	print('NO')


x = int(input())
y = list(map(int, input().split()))

def calc(x, y):
	for i in range(x):
		if y[y[y[i] - 1] - 1] == i + 1:
			return 'YES'
	return 'NO'
print(calc(x, y))


n = int(input())
s = [0]
s.extend(list(map(int, input().split())))
for i in range(1, len(s)):
	x = i
	if s[s[s[x]]] == x:
		print('YES')
		exit()
print('NO')


n = int(input())
planes = [1] + list(map(int, input().split()))
for i in planes:
	if planes[planes[i]] == planes.index(i):
		print('YES')
		exit(0)
print('NO')


n = int(input())
line = input()
nums = [int(item) for item in line.strip().split()]
nums.insert(0, 0)
exist = False
for i in range(1, n + 1):
	num1 = i
	num2 = nums[num1]
	if num1 == num2:
		continue
	num3 = nums[num2]
	if num1 == nums[num3]:
		exist = True
		break
if exist:
	print('YES')
else:
	print('NO')


n = int(input())
a = 0
array = list(map(int, input().split()))
for i in range(n):
	if array[array[i] - 1] != i + 1:
		if array[array[array[i] - 1] - 1] == i + 1:
			a = 1
if a == 1:
	print('yes')
else:
	print('no')


n = int(input())
s = input().split()
st = False
for i in s:
	if i != s[int(i) - 1] and s[int(i) - 1] != s[int(s[int(i) - 1]) - 1] and (i != s[int(s[int(i) - 1]) - 1]) and (s[int(s[int(s[int(i) - 1]) - 1]) - 1] == i):
		st = True
		break
if st:
	print('YES')
else:
	print('NO')


n = int(input())
arr = list(map(int, input().split()))
x = False
for i in range(n):
	a = i + 1
	b = arr[i]
	c = arr[b - 1]
	if arr[c - 1] == a:
		x = True
		break
if x:
	print('YES')
else:
	print('NO')


def love_triangle():
	n = int(input())
	a = [i - 1 for i in list(map(int, input().split()))]
	for i in range(n):
		if a[a[a[i]]] == i:
			return 'YES'
	return 'NO'
print(love_triangle())


(z, zz, dgraphs, mod) = (input, lambda : list(map(int, z().split())), {}, 10 ** 9 + 7)
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
n = int(z())
l = zz()
ll = []
for (i, j) in enumerate(l):
	i = i + 1
	try:
		dgraphs[i]
	except:
		dgraphs[i] = []
	try:
		dgraphs[j]
	except:
		dgraphs[j] = []
	dgraphs[i].append(j)
lx = list(dgraphs)

def s(i, vis, k, c):
	vis[i] = True
	c += 1
	for j in dgraphs[i]:
		if not vis[j]:
			s(j, vis, k, c)
		if vis[j] and j == k and (c == 3):
			print('YES')
			exit()
		if c > 3:
			break
	return False
for i in lx:
	v = [False] * (n + 1)
	if s(i, v, i, 0) == 1:
		print('YES')
		exit()
print('NO')


n = int(input())
planes_loves = list(map(int, input().split()))
result = 'NO'
for index in range(len(planes_loves)):
	current_element = index + 1
	first_love = planes_loves[index]
	two_love = planes_loves[first_love - 1]
	three_love = planes_loves[two_love - 1]
	if first_love != two_love and two_love != three_love and (three_love == current_element):
		result = 'YES'
		break
print(result)


n = int(input())
L = list(map(int, input().split()))
f = False
for i in range(n):
	j = L[i]
	k = L[j - 1]
	m = L[k - 1]
	if i == m - 1:
		f = True
		break
print('YES' if f else 'NO')


n = int(input())
f = list(map(int, input().split()))
ans = []
for i in range(n):
	ans.append(sorted([i + 1, f[i], f[f[i] - 1]]))
y = []
for i in range(len(ans)):
	y.append(ans.count(ans[i]))
if 3 in y:
	print('YES')
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
f = [0] + f
for i in range(1, n + 1):
	if f[f[f[i]]] == i:
		print('Yes')
		break
else:
	print('No')


a = int(input())
b = list(map(int, input().split()))
for i in b:
	if b[b[b[i - 1] - 1] - 1] == i:
		print('YES')
		exit()
print('NO')


n = int(input())
planes = tuple(map(int, input().split()))
for i in range(n):
	if planes[planes[planes[i] - 1] - 1] == i + 1:
		print('YES')
		break
else:
	print('NO')


n = int(input())
mat = list(map(int, input().split()))
for i in range(len(mat)):
	my_love = mat[i] - 1
	if mat[mat[my_love] - 1] == i + 1:
		print('YES')
		break
else:
	print('NO')


n = int(input())
a = [int(x) for x in input().split()]
flag = 0
for i in range(len(a)):
	j = a[i] - 1
	k = a[j] - 1
	if a[k] - 1 == i:
		flag = 1
		break
if flag == 1:
	print('YES')
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
print('NO' if all([f[f[f[x - 1] - 1] - 1] != x for x in f]) else 'YES')


n = int(input())
l = list(map(int, input().split()))
c = 0
for i in l:
	x = l[i - 1]
	y = l[x - 1]
	if l[y - 1] == i:
		c = 1
if c == 1:
	print('YES')
else:
	print('NO')


n = int(input())
array = [1] * (n + 1)
isAnswer = False

def dfs(v, dep, start):
	global isAnswer
	if dep == 2:
		if array[v] == start:
			isAnswer = True
			return True
		else:
			return False
	dfs(array[v], dep + 1, start)
array = [0]
array = array + input().split()
for i in range(n + 1):
	array[i] = int(array[i])
for i in range(1, n + 1):
	dfs(i, 0, i)
if isAnswer == True:
	print('YES')
else:
	print('NO')


n = int(input())
arr = list(map(int, input().split()))
for i in range(n):
	a = arr[i]
	b = arr[a - 1]
	c = arr[b - 1]
	if c - 1 == i and len(set([a, b, c])) == 3:
		print('YES')
		exit()
print('NO')


n = int(input())
mas = list(map(int, input().split()))
for i in range(n):
	A = mas[i]
	B = mas[A - 1]
	C = mas[B - 1]
	if mas[C - 1] == A:
		print('YES')
		exit(0)
print('NO')


n = input()
planes = list(map(int, input().split(' ')))

def check(planes):
	for j in range(len(planes)):
		i = j + 1
		for k in range(3):
			tmp = i
			i = planes[i - 1]
			if i == tmp:
				return False
		if i == j + 1:
			return True
	return False
if check(planes):
	print('YES')
else:
	print('NO')


_ = input()
likes = list(map(int, input().split()))

def yes_or_no(likes):
	for i in range(len(likes)):
		likes[i] -= 1
	for i in range(len(likes)):
		if likes[likes[likes[i]]] == i:
			return 'YES'
	return 'NO'
print(yes_or_no(likes))


n = int(input())
l = list(map(int, input().split()))
if len([i for i in range(n) if l[l[l[i] - 1] - 1] - 1 == i]) > 0:
	print('YES')
else:
	print('NO')


x = int(input())
a = list(map(int, input().strip().split(' ')))
for i in range(x):
	a[i] = a[i] - 1
flag = 0
if x == 2:
	print('NO')
	exit
if x > 2:
	for i in range(x):
		j = a[i]
		k = a[j]
		l = a[k]
		if i == l:
			flag = 1
			break
	if flag == 1:
		print('YES')
	else:
		print('NO')


n = int(input())
v = list(map(int, input().split()))
b = False
for i in range(n):
	if v[v[v[i] - 1] - 1] - 1 == i:
		b = True
print('YES' if b else 'NO')


def DFS2(graph, sv, n, visited, A):
	visited[sv] = True
	A.append(sv)
	for i in graph[sv]:
		if not visited[i]:
			DFS2(graph, i, n, visited, A)

def DFS(graph, sv, n, visited, stack):
	visited[sv] = True
	for i in graph[sv]:
		if not visited[i]:
			DFS(graph, i, n, visited, stack)
	stack.append(sv)

def getSSC(graph, n):
	visited = [False for _ in range(n)]
	stack = []
	for i in range(n):
		if not visited[i]:
			DFS(graph, i, n, visited, stack)
	visited = [False for _ in range(n)]
	while stack:
		front = stack.pop(0)
		if not visited[front]:
			A = []
			DFS2(graph, front, n, visited, A)
			if len(A) == 3:
				return 'YES'
	return 'NO'
n = int(input())
Arr = list(map(int, input().split()))
graph = [[] for _ in range(n)]
graphT = [[] for _ in range(n)]
for i in range(n):
	graph[i].append(Arr[i] - 1)
print(getSSC(graph, n))


a = int(input())
b = list(map(int, input().split()))
c = 0
d = False
for r in b:
	c = r
	l = b[c - 1]
	k = b[l - 1]
	m = b[k - 1]
	if m == c:
		d = True
if d:
	print('YES')
else:
	print('NO')


n = int(input())
v = input().split(' ')
likes = {}
triangle = False
for i in range(0, n):
	likes[i + 1] = int(v[i])
if len(v) > 2:
	for key in likes.keys():
		triangle |= likes[likes[likes[key]]] == key
if triangle:
	print('YES')
else:
	print('NO')


n = int(input())
import sys
arr = [0] + list(map(int, input().split()))
for i in range(1, n + 1):
	if arr[arr[arr[i]]] == i:
		print('Yes')
		sys.exit()
print('No')


n = int(input())
f = list(map(int, input().split()))
for (i, el) in enumerate(f, 1):
	if f[f[f[i - 1] - 1] - 1] == i:
		print('YES')
		exit()
print('NO')


n = int(input())
flag = 'NO'
l = list(map(int, input().split()))
for i in range(n):
	if l[l[l[i] - 1] - 1] == i + 1:
		flag = 'YES'
print(flag)


t = int(input())
l = list(map(int, input().split()))
y = 0
for i in range(t):
	a = l[i]
	b = l[a - 1]
	if l[b - 1] == i + 1:
		print('YES')
		y = 1
		break
if y == 0:
	print('NO')


n = int(input())
num = [i for i in map(int, input().split())]
num.insert(0, 0)
test = False
for i in range(1, n + 1):
	if num[num[num[i]]] == i:
		print('YES')
		test = True
		break
if test == False:
	print('NO')


from sys import stdin
n = int(stdin.readline())
likes = [int(i) for i in stdin.readline().split(' ')]
triangle = False
for i in range(0, n):
	if likes[likes[likes[i] - 1] - 1] - 1 == i:
		triangle = True
		break
if triangle:
	print('YES')
else:
	print('NO')


n = int(input())
ns = list(map(lambda x: x - 1, map(int, input().split())))

def find_triangle(start, arr):
	if ns[start] not in arr:
		arr.append(ns[start])
		find_triangle(ns[start], arr)
	return arr
for i in range(n):
	res = find_triangle(i, [])
	if len(res) == 3 and res[-1] == i:
		print('YES')
		break
else:
	print('NO')


def answer(ary):
	v_list = [False] * len(ary)
	for i in range(1, len(ary), 1):
		loop_size = 0
		while v_list[i] == False:
			v_list[i] = True
			i = ary[i]
			loop_size += 1
		if loop_size == 3:
			return 'YES'
	return 'NO'

def answer2(ary):
	for i in range(1, len(ary), 1):
		if ary[ary[ary[i]]] == i:
			return 'YES'
	return 'NO'

def main():
	n = int(input())
	ary = [int(i) for i in input().split()]
	ary.insert(0, 0)
	print(answer2(ary))
main()


n = int(input())
L = [None] + list(map(int, input().split()))
f = False
for a in range(1, len(L)):
	if L[L[L[a]]] == a:
		f = True
		print('YES')
		break
if f == False:
	print('NO')


n = input()
a = list(map(int, input().split()))
key = False
for i in range(len(a)):
	a[i] -= 1
for i in range(len(a)):
	if a[a[a[i]]] == i:
		print('YES')
		key = True
		break
if key == False:
	print('no')


import sys
lines = sys.stdin.readlines()
sequence = [int(x) for x in lines[1].split()]
tr = False
for (idx, x) in enumerate(sequence):
	idx = idx + 1
	triangle = sequence[sequence[x - 1] - 1]
	if triangle == idx:
		tr = True
		break
if tr:
	print('YES')
else:
	print('NO')


N = int(input())
values = []
for i in input().split():
	values.append(int(i))
c = 0
for i in range(N):
	if values[values[values[i] - 1] - 1] - 1 == i:
		c = 1
if c:
	print('yes')
else:
	print('no')


def solve(r):
	for key in r:
		if r[r[r[key]]] == key:
			return True
	return False
r = {}
n = int(input())
for (i, item) in enumerate(input().split()):
	r[i + 1] = int(item)
print('yes' if solve(r) else 'no')


f = False
n = int(input())
a = list()
a = [0]
x = [int(j) for j in input().split() if int(j) <= n]
for i in range(len(x)):
	a.append(x[i])
del x
for i in range(1, len(a)):
	l = a[i]
	m = a[l]
	n = a[m]
	if i == n:
		print('YES')
		f = True
		break
if f == False:
	print('NO')


def loveTriangle(numPlanes, planes):
	for i in range(numPlanes):
		a = planes[i]
		b = planes[a - 1]
		c = planes[b - 1]
		if c == i + 1:
			return 'YES'
	return 'NO'
numPlanes = int(input())
planes = list(map(int, input().split(' ')))
print(loveTriangle(numPlanes, planes))


import sys
input = sys.stdin.readlines()
planes = input[1].split(' ')
planes = list(map(int, planes))
for (idx, planeA) in enumerate(planes):
	planeB = planes[planeA - 1]
	planeC = planes[planeB - 1]
	if planeC - 1 == idx:
		print('YES')
		break
else:
	print('NO')


R = lambda : map(int, input().split())
n = int(input())
a = list(R())
a[:] = [x - 1 for x in a]

def solve1():
	for i in range(n):
		if a[a[a[i]]] == i and a[i] != i and (a[a[i]] != a[i]) and (a[a[i]] != i):
			print('YES')
			return
	print('NO')
solve1()


n = int(input())
l = list(map(int, input().split()))
l = [0] + l
if n < 3:
	print('NO')
else:
	flag = 0
	for i in range(1, n + 1):
		val1 = l[i]
		val2 = l[val1]
		val3 = l[val2]
		if val3 == i:
			print('YES')
			flag = 1
			break
	if flag == 0:
		print('NO')


n = int(input())
lista = input().split(' ')
cont = 0
for i in lista:
	lista[cont] = int(i)
	cont += 1
lista.insert(0, 0)
res = False
for i in range(1, n + 1):
	if lista[lista[lista[i]]] == i:
		res = True
		break
if res:
	print('YES')
else:
	print('NO')


from collections import defaultdict as dd
n = int(input())
arr = [int(item) for item in input().split()]
d = dd(int)
for i in range(n):
	d[i + 1] = arr[i]
flag = 0
for i in range(1, n + 1):
	a = d[i]
	b = d[a]
	c = d[b]
	if c == i:
		flag = 1
		break
if flag == 0:
	print('No')
else:
	print('Yes')


n = int(input())
l = input().split()
counter = 1
y = 0
for i in range(n):
	if y != 1:
		nxt = int(l[i])
		nxt2 = int(l[nxt - 1])
		nxt3 = int(l[nxt2 - 1])
		if nxt3 == i + 1:
			print('YES')
			y = 1
			break
if y != 1:
	print('NO')


n = input()
n = int(n)
s = input()
a = []
a = s.split()
f = False
for i in range(n):
	if int(a[int(a[int(a[i]) - 1]) - 1]) == i + 1:
		f = True
		break
if f:
	print('Yes')
else:
	print('No')


x = 0
s = []

def dfs(a, i, visited):
	global x, s
	s.append(i)
	x += 1
	visited[i] = True
	if not visited[a[i]]:
		dfs(a, a[i], visited)

def main():
	global x, s
	n = int(input())
	a = list(map(int, input().split()))
	a = [-1] + a
	k = 0
	for i in range(1, n + 1):
		if a[a[a[i]]] == i:
			k = 1
			break
		else:
			k = 0
	if k == 1:
		print('Yes')
	else:
		print('No')
main()


a = int(input())
l = list(map(int, input().split()))
for i in range(a):
	e = l[i]
	c = l[e - 1]
	d = l[c - 1]
	if i + 1 == d:
		print('YES')
		break
	if i == a - 1:
		print('NO')


def solve(a):
	for i in range(len(a)):
		if a[a[a[i] - 1] - 1] - 1 == i:
			return True
	return False
n = int(input())
a = [int(x) for x in input().split()]
if solve(a):
	print('YES')
else:
	print('NO')


def czy(i, lista):
	ktory = i - 1
	for x in range(3):
		ktory = lista[ktory] - 1
	if ktory == i - 1:
		return True
	return False

def samoloty(lista):
	for i in lista:
		if czy(i, lista):
			return 'YES'
	return 'NO'
input()
lista = [int(x) for x in input().split()]
print(samoloty(lista))


def main():
	n_1 = int(input()) + 1
	loves = [0] + [int(_) for _ in input().split()]
	found = False
	for i in range(1, n_1):
		x = loves[i]
		y = loves[x]
		if y != i and loves[y] == i:
			found = True
			break
	print('YES' if found else 'NO')
main()


n = int(input())
f = list(map(int, input().strip().split()))
for i in range(n):
	if f[f[f[i] - 1] - 1] == i + 1:
		print('YES')
		exit()
print('NO')


n = int(input())
check = 0
lstPlanes = list(map(int, input().split()))
lstPlanes = [lstPlanes[i] - 1 for i in range(n)]
for i in range(n):
	if lstPlanes[lstPlanes[lstPlanes[i]]] == i:
		check = 1
print('YES' if check else 'NO')


from sys import stdin
n = int(stdin.readline())
nums = list(map(int, stdin.readline().rstrip().split(' ')))
for i in range(len(nums)):
	if i + 1 == nums[nums[nums[i] - 1] - 1]:
		print('YES')
		break
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
if n == 2:
	print('NO')
else:
	t = 0
	for i in range(len(a)):
		if a[a[a[i] - 1] - 1] - 1 == i:
			t += 1
		else:
			pass
	if t == 0:
		print('NO')
	else:
		print('YES')


def main():
	N = int(input())
	F = tuple(map(int, input().split()))
	for (i, b) in enumerate(F):
		try:
			c = F.index(i + 1) + 1
		except ValueError:
			continue
		if F[b - 1] == c:
			print('YES')
			break
	else:
		print('NO')
main()


n = int(input())
z = list(map(int, input().split()))
z.insert(0, 0)
for x in range(1, n + 1):
	if z[z[z[x]]] == x:
		print('YES')
		exit()
print('NO')


n = int(input())
a = list(map(int, input().split()))
answer = False
for i in range(n):
	if a[a[a[i] - 1] - 1] - 1 == i:
		answer = True
print('YES' if answer else 'NO')


n = int(input())
l = []
l.append(-1)
inp = list(map(int, input().split()))
for i in inp:
	l.append(i)
for i in range(1, n + 1):
	x = l[i]
	y = l[x]
	if i == l[y]:
		print('YES')
		break
else:
	print('NO')


def sol():
	n = int(input())
	A = [int(i) for i in input().split()]
	r = 'NO'
	for i in range(n):
		p = i
		q = i
		c = 0
		r = 'NO'
		while True:
			if c == 2:
				if A[p] - 1 == q:
					r = 'YES'
				break
			if A[p] - 1 != q:
				p = A[p] - 1
				c += 1
			else:
				break
		if r == 'YES':
			break
	print(r)
sol()


def triangle(l, n):
	for i in range(1, len(l) + 1):
		if l[l[l[i - 1] - 1] - 1] == i:
			return True
	return False
n = int(input())
l = list(map(int, input().split()))
if triangle(l, n):
	print('YES')
else:
	print('NO')


def func(v):
	n = len(v)
	for i in range(n):
		if v[v[v[i]]] == i:
			return True
	return False
n = int(input())
v = list(map(int, input().split()))
for i in range(len(v)):
	v[i] -= 1
if func(v):
	print('YES')
else:
	print('NO')


input()
a = [1] + list(map(int, input().split()))
print('YES' if any(map(lambda x: a[a[x]] == a.index(x), a)) else 'NO')


import sys
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
n = ri()
f = rl()
ans = 'NO'

def g(k):
	return f[k] - 1
for i in range(n):
	if g(g(g(i))) == i:
		ans = 'YES'
		break
print(ans)


n = int(input())
lst = []
for x in input().split():
	lst.append(int(x) - 1)
flag = False
for x in range(len(lst)):
	if x == lst[lst[lst[x]]]:
		flag = True
		break
if flag:
	print('YES')
else:
	print('NO')


t = int(input())
arr = list(map(int, input().split()))
f = 0
for i in range(0, t):
	a = arr[i]
	b = arr[a - 1]
	c = arr[b - 1]
	if c == i + 1:
		f = 1
		break
if f == 1:
	print('YES')
else:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
for x in l:
	if x == l[l[l[x - 1] - 1] - 1]:
		print('YES')
		break
else:
	print('NO')


(f1, f2) = (False, True)
n = int(input())
l = list(map(int, input().split()))
for i in range(n):
	if l[i] == l[l[l[l[i] - 1] - 1] - 1]:
		f1 = True
for i in range(n):
	if l[i] == i + 1:
		f2 = False
if f1 and f2:
	print('YES')
else:
	print('NO')


GI = lambda : int(input())
GIS = lambda : map(int, input().split())

def main():
	n = GI()
	ps = list(GIS())
	ps.insert(0, 0)
	seen = set()
	for i in range(1, n + 1):
		if ps[ps[ps[i]]] == i:
			return True
	return False
print('YES' if main() else 'NO')


input()
pls = list(map(int, input().split()))
for (n, pl) in enumerate(pls):
	if pl == pls[pls[pls[pl - 1] - 1] - 1] and pl != pls[pl - 1]:
		print('YES')
		break
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
c = []
for i in range(n - 2):
	if i + 1 in a:
		if a[a[i] - 1] == a.index(i + 1) + 1:
			c.append('Yes')
			break
if len(c) == 1:
	print('YES')
else:
	print('NO')


n = input()
a = list(map(int, input().split(' ')))
b = [0]
for i in a:
	b.append(i)
ans = 0
for i in range(1, len(b)):
	if i == b[b[b[i]]]:
		print('YES')
		ans = 1
		break
if ans == 0:
	print('NO')


n = int(input())
a = list(map(int, input().split(' ')))
m = 1
for i in range(n):
	b = a[a[i] - 1]
	c = a[b - 1]
	if c == i + 1:
		print('YES')
		m = 0
		break
if m == 1:
	print('NO')


n = int(input())
f = [int(i) for i in input().split()]
f.insert(0, 0)
y = False
for i in range(1, n + 1):
	if i == f[f[f[i]]]:
		print('YES')
		y = True
		break
if not y:
	print('NO')


n = int(input())
s = input().split()
pd = 0
for i in range(1, n + 1):
	if str(s[int(s[int(s[i - 1]) - 1]) - 1]) == str(i):
		print('YES')
		pd = 1
		break
if pd == 0:
	print('NO')


n = int(input())
a = list(map(lambda x: int(x) - 1, input().split()))
print('YES' if [i for i in range(n) if a[a[a[i]]] == i] else 'NO')


num_planes = int(input())
planes = [0]
planes.extend(list(map(int, input().split())))
triangle = False
for plane_a in range(1, num_planes):
	plane_b = planes[plane_a]
	plane_c = planes[plane_b]
	if plane_a == planes[plane_c]:
		triangle = True
		break
if triangle:
	print('YES')
else:
	print('NO')


n = int(input())
a = [0] + [int(x) for x in input().split()]
ans = 'NO'
for i in range(1, n + 1):
	if a[i] == a[a[a[a[i]]]]:
		ans = 'YES'
print(ans)


noe = int(input())
arr = [int(x) - 1 for x in input().split()]
if any((arr[arr[arr[i]]] == i for i in range(noe))):
	print('YES')
else:
	print('NO')


n = int(input())
x = [int(z) for z in input().split()]
count = 0
for i in x:
	if x[x[x[i - 1] - 1] - 1] == i:
		print('YES')
		count = 1
		break
if count == 0:
	print('NO')


def fun(p):
	for i in range(1, len(p)):
		t = p[i]
		d = p[t]
		if p[d] == i:
			return 1
n = int(input())
m = [int(x) for x in input().split()]
a = [0] * (n + 1)
for i in range(0, n):
	a[i + 1] = m[i]
if fun(a):
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
a = [0] + a
flag = 0
for i in range(1, n + 1):
	start = i
	k = 2
	z = a[i]
	while k > 0:
		z = a[z]
		k -= 1
	if z == start:
		flag = 1
		break
if flag == 1:
	print('YES')
else:
	print('NO')


from sys import stdin
from collections import deque, Counter, defaultdict
import sys
import math, os
import operator
import random
from fractions import Fraction
import functools
import bisect
import itertools
from heapq import *
import time
import random
import bisect
n = int(input())
arr = list(map(int, input().split()))
d = {i + 1: None for i in range(10)}
for i in range(1, n + 1):
	d[i] = arr[i - 1]
for i in range(1, n + 1):
	if d[d[d[i]]] == i:
		print('YES')
		sys.exit()
print('NO')


n = int(input())
a = input().split()
true = False
for i in range(n):
	if int(a[int(a[int(a[i]) - 1]) - 1]) - 1 == i:
		true = True
		break
if true:
	print('YES')
else:
	print('NO')


n = int(input())
fs = input().split(' ')
fs = list(map(int, fs))
fxx = list(map(lambda x: x - 1, fs))
f1 = [fxx[fxx[fxx[i]]] for i in range(len(fxx))]
if any((i == f1[i] for i in range(len(f1)))):
	print('YES')
else:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
for i in range(n):
	m = l[i]
	o = l[m - 1]
	r = l[o - 1]
	flag = 0
	if r == i + 1:
		print('YES')
		flag = 1
		break
if flag == 0:
	print('NO')


n = int(input())
plane = list(input().split())
plane1 = [0]
a = 0
for number in plane:
	plane1.append(int(number))
for i in range(1, len(plane1)):
	if plane1[plane1[plane1[i]]] == i:
		print('YES')
		a = 1
		break
if a == 0:
	print('NO')


n = int(input())
m = list(map(int, input().split()))
(l, k) = ([], [])
for i in range(n):
	l.append(m[i] - 1)
for i in range(n):
	if l[l[l[i]]] == i:
		k.append(1)
if sum(k) != 0:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
count = 1
if len(set(a)) > 2:
	for i in range(len(a)):
		if i + 1 == a[a[a[i] - 1] - 1]:
			count = 0
			break
	if count == 0:
		print('YES')
	else:
		print('NO')
else:
	print('NO')


n = input()
a = [0] + list(map(int, input().split()))
for (i, x) in enumerate(a):
	if i == x:
		continue
	if a[a[x]] == i:
		print('YES')
		exit()
print('NO')


n = int(input())
a = [int(i) for i in input().split()]
s = 'NO'
for i in range(n):
	if a[a[a[i] - 1] - 1] - 1 == i:
		s = 'YES'
		break
print(s)


n = int(input())
if n < 3:
	print('NO')
	exit(0)
a = [int(i) - 1 for i in input().split()]
for (i, x) in enumerate(a):
	if i == a[a[x]]:
		print('YES')
		break
else:
	print('NO')


import sys
lines = []
for Line in sys.stdin:
	lines.append(Line.rstrip('\n'))
n = int(lines[0])
del lines[0]
p = list(map(int, lines[0].split()))
d = dict()
for i in range(n):
	d[i] = p[i] - 1
i = 0
for (k, v) in d.items():
	i += 1
	if d[d[v]] == k:
		print('YES')
		exit(0)
print('NO')


n = int(input())
s = list(map(int, input().split()))
isYes = False
for i in range(n):
	a = i
	b = s[a] - 1
	c = s[b] - 1
	if a != b and b != c and (a == s[c] - 1):
		print('YES')
		isYes = True
		break
if not isYes:
	print('NO')


class Fly:

	def __init__(self, pos, like):
		self.pos = pos + 1
		self.like = like

def if_triangle(a, vse):
	b = a.like
	c = vse[b - 1].like
	if vse[c - 1].like == a.pos:
		return True
	else:
		return False

def fd70000000():
	n = int(input())
	smts = list(map(int, input().split()))
	SS = []
	for i in range(len(smts)):
		SS.append(Fly(i, smts[i]))
	for i in range(n):
		if if_triangle(SS[i], SS):
			return print('YES')
	return print('NO')
fd70000000()


n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(n):
	if a[a[a[i] - 1] - 1] - 1 == i:
		count += 1
print('YES' if count > 0 else 'NO')


N = int(input().strip())
child = [int(x) for x in input().strip().split(' ')]
Nodes = set(range(1, N + 1))

def isLoTr(x):
	par = x
	gpar = -1
	ch = child[x - 1]
	visited = set([x])
	while ch not in visited and child[ch - 1] != gpar:
		visited.add(ch)
		gpar = par
		par = ch
		ch = child[ch - 1]
	return child[ch - 1] == gpar
flg = True
for i in Nodes:
	if isLoTr(i):
		print('YES')
		flg = False
		break
if flg:
	print('NO')


import math
from collections import *
import sys
from functools import reduce

def get_ints():
	return map(int, input().strip().split())

def get_list():
	return list(get_ints())

def get_string():
	return list(input().strip().split())

def printxsp(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')
DIRECTIONS = [[0, 1], [0, -1], [1, 0], [1, -1]]
NEIGHBOURS = [(i, j) for i in range(-1, 2) for j in range(-1, 2) if i != 0 or j != 0]
OrdUnicode_a = ord('a')
OrdUnicode_A = ord('A')
CAPS_ALPHABETS = {chr(i + OrdUnicode_A): i for i in range(26)}
SMOL_ALPHABETS = {chr(i + OrdUnicode_a): i for i in range(26)}
UGLYMOD = int(1000000000.0) + 7
SEXYMOD = 998244353
MAXN = int(100000.0) + 1
INFINITY = float('inf')
n = int(input())
li = get_list()
li.insert(0, 0)
flag = False
for i in range(1, n):
	if i == li[li[li[i]]]:
		flag = True
print('YES' if flag else 'NO')


import sys
n = int(input())
data = [int(x) for x in input().split()]
for i in range(len(data)):
	if data[data[data[i] - 1] - 1] == i + 1:
		print('YES')
		sys.exit()
print('NO')


n = int(input())
li = [0] + [int(i) for i in input().split()]
for i in range(1, n + 1):
	if i == li[li[li[i]]]:
		print('YES')
		break
else:
	print('NO')


input()
a = list(map(int, input().split()))
ans = 'NO'
for i in range(len(a)):
	if a[a[a[i] - 1] - 1] == i + 1:
		ans = 'YES'
		break
print(ans)


a = int(input())
x = [int(x) for x in input().split()]
if a < 3:
	print('NO')
	exit()
for i in range(a):
	if x[x[x[i] - 1] - 1] - 1 == i:
		print('YES')
		exit()
print('NO')


x = int(input())
l = list(map(int, input().split()))
j = 0
for i in range(1, x + 1):
	if l[l[l[i - 1] - 1] - 1] == i:
		j = 1
		break
if j == 1:
	print('YES')
else:
	print('NO')


n = int(input())
arr = list(map(int, input().split()))
i = 0
flag = 0
while i < len(arr):
	plane1 = i + 1
	plane1_lover = arr[i]
	plane2 = arr[arr[i] - 1]
	plane3 = arr[plane2 - 1]
	if plane1 == plane3:
		print('YES')
		flag = 1
		break
	else:
		i += 1
if flag == 0:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
f.insert(0, 0)
res = 'NO'
if n > 2:
	for i in range(1, n + 1):
		if f[i] != f[f[i]] and f[f[i]] != f[f[f[i]]] and (i == f[f[f[i]]]):
			res = 'YES'
			break
print(res, end='')


n = int(input())
g = input()
A = g.split()
for i in range(n):
	A[i] = int(A[i])
for a in range(n):
	al = A[a]
	b = al - 1
	bl = A[b]
	c = bl - 1
	cl = A[c]
	if a + 1 == cl:
		m = 'YES'
		break
	else:
		m = 'NO'
print(m)


n = input()
num = input().split()
flag = 0
for i in range(0, int(n)):
	a = int(num[i])
	b = int(num[a - 1])
	c = int(num[b - 1])
	if i == c - 1:
		flag = 1
		break
if flag == 0:
	print('NO\n')
else:
	print('YES\n')


def love(ls):
	for i in range(len(ls)):
		a = i + 1
		b = int(ls[i])
		c = int(ls[b - 1])
		k = int(ls[c - 1])
		if a == k:
			return True
	return False
n = int(input())
li = input()
ai = li.strip().split()
if love(ai):
	print('YES')
else:
	print('NO')


num = int(input())
f = list(map(int, input().split()))
flag = 0
for i in range(num):
	a = i + 1
	b = f[i]
	c = f[b - 1]
	if a == f[c - 1]:
		print('YES')
		flag = 1
		break
if not flag:
	print('NO')


n = int(input())
l = input().split()
li = [int(i) for i in l]
hashi = dict()
for i in range(n):
	hashi[i + 1] = li[i]
done = 0
for i in range(1, n + 1):
	if hashi[hashi[hashi[i]]] == i:
		print('YES')
		done = 1
		break
if done == 0:
	print('NO')


n = int(input())
arr = [int(i) for i in input().split()]
f = 0
arr.insert(0, 0)
for i in range(1, n + 1):
	a = arr[i]
	b = arr[a]
	c = arr[b]
	if c == i:
		f = 1
		break
if f == 1:
	print('YES')
else:
	print('NO')


def f(L):
	for i in range(0, len(L)):
		if L[L[L[i] - 1] - 1] - 1 == i:
			return 'YES'
	return 'NO'
n = int(input())
L = list(map(int, input().split()))
print(f(L))


k = int(input())
l = [int(x) for x in input().split()]
for i in range(k):
	if l[l[l[i] - 1] - 1] - 1 == i:
		print('YES')
		break
else:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
flag = 0
for i in range(n):
	first = l[i]
	second = l[first - 1]
	third = l[second - 1]
	if l[third - 1] == first:
		flag = 1
		break
if flag:
	print('YES')
else:
	print('NO')


import sys
n = int(input())
res = {}
li = list(map(int, input().split()))
for i in range(n):
	if i + 1 not in res:
		res[i + 1] = li[i]
for i in res:
	if res[res[res[i]]] == i:
		print('YES')
		sys.exit()
print('NO')


n = int(input())
fi = input().split()
fi = list(map(int, fi))
tempArray = []
tempArray.append(0)
for i in range(n):
	tempArray.append(0)
for j in range(1, n + 1):
	tempArray[j] = fi[j - 1]
	h = 0
	tempIndex = j
	flag = 0
	while h <= 2:
		if tempArray[tempArray[tempIndex]] != 0:
			tempIndex = tempArray[tempIndex]
			h += 1
		else:
			flag = 1
			break
	flag1 = 0
	if flag == 0:
		if tempIndex == j:
			print('YES')
			flag1 = 1
			break
if flag1 == 0:
	print('NO')


graph = {}
n = int(input())
numList = list(map(int, input().split(' ')))
for n in numList:
	graph[n] = numList[n - 1]
answer = 0
for i in graph:
	if graph[graph[graph[i]]] == i:
		answer = 1
		break
if answer == 1:
	print('yes')
else:
	print('no')


n = int(input())
lst = [int(i) for i in input().split()]
dic = {}
for i in range(1, n + 1):
	dic[i] = lst[i - 1]
is_yes = False
for key in dic:
	if dic[dic[dic[key]]] == key:
		is_yes = True
		break
if is_yes:
	print('YES')
else:
	print('NO')


n = int(input())
res = [int(i) for i in input().split(' ')]
s = 'NO'
for i in range(len(res)):
	if res[res[res[i] - 1] - 1] - 1 == i:
		s = 'YES'
		break
print(s)


import sys
import math
get_string = lambda : sys.stdin.readline().strip()
get_list = lambda : list(map(int, sys.stdin.readline().strip().split()))
get_intList_from_str = lambda : list(map(int, list(sys.stdin.readline().strip())))
get_charList_from_str = lambda : list(sys.stdin.readline().strip())
get_int = lambda : int(sys.stdin.readline().strip())
pt = lambda x: sys.stdout.write(str(x))
n = get_int()
planes = get_list()
triangel = []
valid = True
for i in range(n):
	first_like = planes[i]
	second_like = planes[first_like - 1]
	third_like = planes[second_like - 1]
	if third_like == i + 1:
		print('YES')
		valid = False
		break
if valid:
	print('NO')


input()
p = [int(x) - 1 for x in input().split()]
for i in p:
	if p[p[p[i]]] == i:
		exit(print('YES'))
print('NO')


n = int(input())
A = list([int(x) for x in input().split()])
i = 0
while i < len(A):
	num1 = A[i]
	num2 = A[num1 - 1]
	num3 = A[num2 - 1]
	if A[num3 - 1] == num1:
		print('Yes')
		exit()
	i += 1
else:
	print('No')


def love_tri(arr):
	i = 1
	d = {}
	for j in arr:
		d.update({i: j})
		i += 1
	for i in d:
		k = d[i]
		m = d[k]
		l = d[m]
		if l == i:
			return 'YES'
	else:
		return 'NO'
n = int(input())
arr = list(map(int, input().split()))
print(love_tri(arr))


n = map(int, input().split())
a = list(map(int, input().split()))
for (i, l) in enumerate(a):
	s = l
	t = a[l - 1]
	x = a[t - 1]
	if i + 1 == x:
		print('YES')
		exit()
print('NO')


n = int(input())
l = list(map(int, input().split()))
for i in l:
	a = i
	b = l[a - 1]
	c = l[b - 1]
	if a == l[c - 1] and a != b and (b != c) and (c != a):
		print('YES')
		return
print('NO')


n = int(input())
a = list(map(int, input().split()))
for i in a:
	if a[a[a[i - 1] - 1] - 1] == i:
		print('yes')
		exit()
print('no')


def main():
	n = int(input())
	edges = dict(((i, dict()) for i in range(n)))
	for (i, f_i) in enumerate(map(int, input().split())):
		edges[i][f_i - 1] = 1
		edges[f_i - 1][i] = 1
	for vertice in edges:
		for vertice2 in edges[vertice]:
			for vertice3 in edges[vertice2]:
				for vertice4 in edges[vertice3]:
					if vertice == vertice4:
						return 'YES'
	return 'NO'
print(main())


n = int(input())
x = list(map(int, input().split()))
c = 0
for i in range(n):
	if x[x[x[i] - 1] - 1] - 1 == i:
		print('YES')
		c = -1
		break
if c == 0:
	print('NO')


n = int(input())
arr = input().split()
result = 'NO'
for i in range(len(arr)):
	arr[i] = int(arr[i]) - 1
for i in range(len(arr)):
	if arr[arr[arr[i]]] == i:
		result = 'YES'
		break
print(result)


n = int(input())
l = list(map(int, input().split()))
flag = False
for i in range(n):
	if l[l[l[i] - 1] - 1] - 1 == i:
		flag = True
		break
if flag:
	print('YES')
else:
	print('NO')


n = int(input())
m = [0] + list(map(int, input().split())) * 2
for i in range(1, len(m)):
	if m[m[m[i]]] == i:
		print('YES')
		break
else:
	print('NO')


n = int(input())
l = [int(x) for x in input().split()]
l = [-1] + l
for i in range(1, len(l)):
	if l[l[l[i]]] == i:
		print('YES')
		break
else:
	print('NO')


def readInts():
	return map(int, input().split())

def readInt():
	return int(input())

def readIntsindex0():
	return list(map(lambda x: int(x) - 1, input().split()))
n = readInt()
a = readIntsindex0()
ans = False
for i in range(n):
	if a[a[a[i]]] == i:
		ans = True
		break
print('YES' if ans else 'NO')


n = int(input())
f = [0] + list(map(int, input().split()))
for i in f[1:]:
	fi = f[i]
	if fi != i:
		fii = f[fi]
		if fii != fi:
			if f[fii] == i:
				print('YES')
				quit()
print('NO')


n = int(input())
love = list(map(int, input().split()))
sol = False
for i in range(n):
	if love[love[love[i] - 1] - 1] - 1 == i:
		sol = True
if sol:
	print('YES')
else:
	print('NO')


__author__ = 'Dilshod'
n = int(input())
m = list(map(int, input().split()))
for i in range(n):
	if m[m[m[i] - 1] - 1] == i + 1:
		print('YES')
		exit()
print('NO')


n = int(input())
arr = list(map(int, input().split()))
index_ = []
for i in arr:
	check_with = arr.index(i) + 1
	temp = arr[i - 1]
	check_for = arr[temp - 1]
	if check_for == check_with:
		print('YES')
		break
	else:
		continue
else:
	print('NO')


n = int(input())
arr = [int(x) for x in input().split()]
for i in range(0, n):
	if arr[arr[arr[i] - 1] - 1] == i + 1:
		print('YES')
		exit()
print('NO')


n = int(input())
plains = list(map(int, input().split()))
plains = [i - 1 for i in plains]
visited = [False for i in range(n)]

def search(i, tmp):
	visited[i] = True
	tmp.append([i, plains[i]])
	if len(tmp) > 3:
		tmp = tmp[1:]
	if tmp[0][0] == tmp[-1][1]:
		if len(tmp) == 3:
			return True
		else:
			return False
	elif visited[plains[i]]:
		return False
	else:
		return search(plains[i], tmp)
flag = False
while False in visited:
	tt = search(visited.index(False), [])
	if tt:
		flag = True
		break
if flag:
	print('YES')
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
(k, l) = (0, 0)
for i in f:
	k = f[i - 1]
	if k < len(f):
		y = f[k - 1]
		if f[y - 1] == i:
			print('YES')
			l = 1
			break
if l == 0:
	print('NO')


n = int(input())
love = [0] + [int(i) for i in input().split()]
for i in range(1, n + 1):
	if i == love[love[love[i]]]:
		if i != love[i] != love[love[i]]:
			print('YES')
			quit()
print('NO')


import sys

def main():
	sys.stdin.readline()
	a = list(map(int, sys.stdin.readline().split()))
	for i in a:
		if a[a[a[i - 1] - 1] - 1] == i:
			sys.stdout.write('YES')
			return
	else:
		sys.stdout.write('NO')
main()


n = int(input())
f = [0] + [*map(int, input().split())]
s = []
h = 0
for i in range(1, n + 1):
	s = []
	h = 0
	s.append(f[i])
	s.append(i)
	if i in f:
		ff = f.index(i)
	else:
		continue
	if ff in f:
		if ff not in s and f.index(ff) in s:
			h = 1
			print('YES')
			break
if h == 0:
	print('NO')


def loveTriangle(n, seq):
	i = 0
	while i < n:
		if i + 1 == seq[seq[seq[i] - 1] - 1]:
			return 'YES'
		i += 1
	return 'NO'
n = int(input())
seq = [int(x) for x in input().split()]
print(loveTriangle(n, seq))


n = int(input())
nn = input()
nn = nn.split()
k = []
for i in nn:
	k.append(int(i))
l = 0
for i in k:
	l = l + 1
	a1 = i
	a2 = k[a1 - 1]
	a3 = k[a2 - 1]
	if a3 == l:
		print('YES')
		break
else:
	print('NO')


n = int(input())
list1 = list(map(int, input().split()))
flag = 0
for i in range(n):
	a = i + 1
	b = list1[i]
	c = list1[b - 1]
	if a == list1[c - 1]:
		print('YES')
		flag = 1
		break
if not flag:
	print('NO')


def answer():
	n = int(input())
	i = 0
	a = [int(x) for x in input().split()]
	visited = []
	done = 0
	while i < len(a):
		if a[i] == a[a[a[a[i] - 1] - 1] - 1]:
			print('YES')
			done = 1
			break
		i += 1
	if not done:
		print('NO')
answer()


n = int(input())
l = list(map(int, input().split()))
l = [0] + l
g = False
for i in range(1, n + 1):
	k = 0
	i_c = i
	while k <= 4:
		if i_c == i and k == 3:
			g = True
			break
		i_c = l[i_c]
		k += 1
	if g:
		print('YES')
		g = True
		break
if not g:
	print('NO')


def is_love_triangle(n, d):
	for i in range(n):
		if i + 1 == d[d[d[i + 1]]]:
			return True
	return False
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	d[i + 1] = a[i]
if is_love_triangle(n, d):
	print('YES')
else:
	print('NO')


n = int(input())
numbers = [int(i) for i in input().split()]
edges = {}
for n in range(len(numbers)):
	edges[n + 1] = numbers[n]
triange = []
triangle_found = False
for vertex in edges:
	if triangle_found:
		continue
	if edges[edges[edges[vertex]]] == vertex:
		print('Yes')
		triangle_found = True
		break
if triangle_found is False:
	print('NO')


n = int(input())
ls = list(map(int, input().split()))
d = {}
for i in range(1, n + 1):
	d[i] = ls[i - 1]
cnt = 0
for i in range(1, n + 1):
	try:
		if d[d[d[i]]] == i:
			cnt += 1
	except:
		pass
if cnt > 0:
	print('YES')
else:
	print('NO')


from collections import defaultdict as dd
n = int(input())
l = list(map(int, input().split()))
vis = dd(int)
flag = 0
for i in range(1, n + 1):
	if vis[i] != 1:
		l1 = [i, l[i - 1], l[l[i - 1] - 1]]
		if len(set(l1)) == 3 and l[l[l[i - 1] - 1] - 1] == i:
			flag = 1
			break
if flag == 1:
	print('YES')
else:
	print('NO')


n = int(input())
y = list(map(int, input().split()))
j = True
for x in range(n):
	a = x + 1
	b = y[a - 1]
	c = y[b - 1]
	if y[c - 1] == a and j:
		print('YES')
		j = False
if j:
	print('NO')


import sys
n = int(input())
li = [int(i) for i in input().split()]
leng = len(li)
for i in range(leng):
	a = li[i]
	b = li[a - 1]
	c = li[b - 1]
	if b != i + 1 and c == i + 1:
		print('Yes')
		sys.exit(0)
print('No')


n = int(input())
l = [int(i) for i in input().split()]
c = 0
for i in range(n):
	if l[l[l[i] - 1] - 1] == i + 1:
		c = 1
		break
if c == 1:
	print('YES')
else:
	print('NO')


t = int(input())
f = input()
f = f.split()
if len(set(f)) < 3:
	print('NO')
else:
	fin = 0
	for x in range(t):
		tar = x + 1
		ne = f[int(f[x]) - 1]
		if int(f[int(ne) - 1]) == tar:
			print('YES')
			fin = 1
			break
	if fin == 0:
		print('NO')


n = int(input())
a = list(map(int, input().split()))
for (i, x) in enumerate(a):
	b = [1] * n
	b[i] = 0
	c = 1
	t = x - 1
	while b[t]:
		b[t] = 0
		t = a[t] - 1
		c += 1
	if c == 3 and i == t:
		exit(print('YES'))
exit(print('NO'))


def read_nums():
	return [int(x) for x in input().split()]

def is_triangle(nums):
	for (index, num) in enumerate(nums):
		if nums[nums[num - 1] - 1] - 1 == index:
			return 'YES'
	return 'NO'

def main():
	input()
	nums = read_nums()
	print(is_triangle(nums))
main()


n = int(input())
p = input().rstrip().split(' ')
for i in range(0, len(p)):
	a = int(p[i])
	b = int(p[a - 1])
	c = int(p[b - 1])
	d = int(p[c - 1])
	if a == d:
		print('YES')
		break
else:
	print('NO')


a = int(input())
l = list(map(int, input().split()))
dict = {}
li = []
c = 0
for i in range(a):
	dict[i + 1] = l[i]
for i in range(a):
	b = dict[i + 1]
	if dict[b] != i + 1 and dict[dict[b]] == i + 1:
		print('YES')
		exit()
print('NO')


n = int(input())
s = input().split()
d = {}
for i in range(1, n + 1):
	d[i] = int(s[i - 1])
found = False
for i in range(1, n + 1):
	if d[d[d[i]]] == i:
		found = True
		break
print('YES' if found else 'NO')


def I():
	return map(int, input().split())
n = int(input())
f = list(I())
for i in range(n):
	if f[f[f[i] - 1] - 1] - 1 == i:
		print('YES')
		quit()
print('NO')


def plane_love():
	for a in range(1, n + 1):
		b = love[a]
		c = love[b]
		if a == love[c]:
			return 'YES'
	return 'NO'
n = int(input())
love = [0] + [int(x) for x in input().split()]
print(plane_love())


n = int(input())
s = [int(i) - 1 for i in input().split()]
c = [s[s[s[i]]] for i in range(n)]
for i in range(n):
	if c[i] == i:
		print('YES')
		break
else:
	print('NO')


n = int(input())
lst = list(map(int, input().split()))
ans = 'NO'
for i in range(n):
	if lst[lst[lst[i] - 1] - 1] == i + 1:
		ans = 'YES'
print(ans)


n = int(input())
lis = [0] + [int(i) for i in input().split()]
count = 0
for i in range(1, len(lis)):
	a = lis[lis[i]]
	b = lis[a]
	if lis[b] == lis[i]:
		count += 1
if count > 0:
	print('YES')
else:
	print('NO')


n = int(input())
p = [int(num) for num in input().split()]
p.insert(0, 0)
c = 0
for i in range(1, n + 1):
	if p[p[p[i]]] == i:
		c = c + 1
if c != 0:
	print('YES')
else:
	print('NO')


def solve():
	n = input()
	p = [int(x) - 1 for x in input().split()]
	for (i, v) in enumerate(p):
		end = p[p[v]]
		if i == end:
			print('YES')
			return
	print('NO')
solve()


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
from sys import stdin, stdout
from collections import Counter
from math import ceil
from bisect import bisect_left
from bisect import bisect_right
import math
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def lessThanX(a, x):
	i = bisect_left(a, x)
	if i:
		return i - 1
	else:
		return -1

def isprime(n):
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			return False
	return True

def BinarySearchFromRight(a, x):
	i = bisect_right(a, x)
	if i != len(a) + 1 and a[i - 1] == x:
		return i - 1
	else:
		return -1

def BinarySearch(a, x):
	i = bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	else:
		return -1

def main():
	n = ip()
	li = ai()
	d = {}
	for i in range(n):
		d[i + 1] = li[i]
	c = 0
	for (k, v) in d.items():
		try:
			if d[d[d[k]]] == k:
				print('YES')
				c += 1
				break
		except:
			pass
	if c == 0:
		print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
ip = lambda : int(sys.stdin.readline().rstrip('\r\n'))
ai = lambda : list(map(int, sys.stdin.readline().split()))
ei = lambda : map(int, sys.stdin.readline().split())
main()


n = int(input())
l = list(map(int, input().split()))
for i in range(n):
	v1 = l[i] - 1
	v2 = l[v1] - 1
	if l[v2] - 1 == i:
		print('yes')
		exit()
print('no')


n = int(input())
l = ['3']
x = str(input())
x = x.split()
z = 0
for i in x:
	l.append(int(i))
for i in range(1, n + 1):
	if l[l[l[i]]] == i:
		z = 1
if z == 1:
	print('YES')
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
is_yes = False
for el in f:
	if f[f[el - 1] - 1] == f.index(el) + 1:
		is_yes = True
		break
print('YES') if is_yes else print('NO')


from collections import deque, defaultdict
n = int(input())
arr = list(map(int, input().split()))
for i in range(n):
	arr[i] -= 1
naa = 0
for i in range(0, n):
	if arr[arr[arr[i]]] == i:
		print('YES')
		naa = 1
		break
if naa == 0:
	print('NO')


n = int(input())
A = [int(k) - 1 for k in input().split(' ')]

def main(A, n):
	B = [-1] * n
	while True:
		i = 0
		while B[i] == 1:
			i += 1
			if i >= n:
				print('NO')
				return
		l = [i]
		while B[i] == -1:
			B[i] = 1
			if len(l) >= 3:
				if l[-3] == A[i]:
					print('YES')
					return
			l.append(A[i])
			i = A[i]
main(A, n)


n = int(input())
f = input().split()
for i in range(n):
	f[i] = int(f[i])
flag = False
for i in range(n):
	if i == f[f[f[i] - 1] - 1] - 1:
		flag = True
if flag:
	print('YES')
else:
	print('NO')


n = int(input())
f = [1, *map(int, input().split())]
print('NYOE S'[any((f[f[x]] == f.index(x) for x in f))::2])


import sys, math
input = sys.stdin.readline
L = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
n = int(input())
l = L()
l = [0] + l
d = 0
for i in range(1, n + 1):
	if l[l[l[l[i]]]] == l[i]:
		print('YES')
		d = 1
		break
if d == 0:
	print('NO')


n = int(input())
arr = list(map(int, input().split()))
fl = 0
for i in range(n):
	if arr[arr[arr[i] - 1] - 1] == i + 1:
		fl = 1
		break
if fl == 1:
	print('YES')
else:
	print('NO')


n = int(input())
l = list(map(int, input().split()))

def f(n, l):
	for i in range(n):
		k = l[i]
		m = l[k - 1]
		if l[m - 1] == i + 1:
			return 'YES'
	return 'NO'
print(f(n, l))


n = int(input())
arr = list(map(int, input().split()))
for i in range(n):
	j = arr[i]
	p = arr[j - 1]
	q = arr[p - 1]
	if q == i + 1:
		print('YES')
		exit()
print('NO')


def cycle():
	global graph
	for vertex in graph:
		if length:
			break
		dfs_for_cycle(vertex, [])
	print('YES' if length else 'NO')

def dfs_for_cycle(vertex, cycle):
	global length, graph
	if length:
		return
	for neighbour in graph[vertex]:
		if neighbour not in cycle and len(cycle) != 3:
			cycle.append(neighbour)
			dfs_for_cycle(neighbour, cycle)
			cycle.remove(neighbour)
		elif len(cycle) == 3:
			if neighbour == cycle[0]:
				length = True
				break
n = int(input())
planes = [int(i) for i in input().split()]
pairs = [[i + 1, planes[i]] for i in range(n)]
graph = {}
for i in range(n):
	pair = pairs[i]
	if pair[0] in graph:
		graph[pair[0]].add(pair[1])
	else:
		graph[pair[0]] = {pair[1]}
length = False
cycle()


n = int(input())
if n < 3:
	l = [int(x) for x in input().split()]
	print('NO')
else:
	yes = 0
	l = []
	l.append(1)
	l2 = [int(x) for x in input().split()]
	l += l2
	for i in range(1, n + 1):
		first = l[i]
		second = l[first]
		third = l[second]
		if first != second and i != first and (i != second) and (first != third) and (second != third) and (third == i):
			yes += 1
			break
		else:
			yes = 0
			continue
	if yes:
		print('YES')
	else:
		print('NO')


n = int(input())
dict1 = {}
lst = list(map(int, input().split()))
for i in range(1, n + 1):
	dict1[i] = lst[i - 1]
for i in dict1:
	key = i
	cnt = 0
	flag = 0
	while True:
		if key in dict1.keys():
			cnt += 1
			key = dict1[key]
			if cnt == 2:
				if dict1[key] == i:
					flag = 1
					break
			elif cnt > 2:
				break
		else:
			break
	if flag == 1:
		print('YES')
		break
if flag == 0:
	print('NO')


from bisect import bisect_right as br
import sys
from collections import *
from math import *
import re

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	c = 0
	for i in range(2, n):
		if prime[i]:
			c += 1
	return c

def totient(n):
	(res, p) = (n, 2)
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			res -= int(res / p)
		p += 1
	if n > 1:
		res -= int(res / n)
	return res

def iseven(n):
	return [False, True][0 if n % 2 else 1]

def inp_arr():
	return list(map(int, input().split()))

def inp_integers():
	return map(int, input().split())

def inp_strings():
	return input().split()

def lcm(a, b):
	return a * b / gcd(a, b)
max_int = sys.maxsize
mod = 10 ** 9 + 7
flag = False
n = int(input())
arr = inp_arr()
for i in range(n):
	if i + 1 == arr[arr[arr[i] - 1] - 1]:
		flag = True
print(['NO', 'YES'][flag == True])


def isCycle(map_, visited, node):
	stack = [node]
	while stack:
		ele = stack.pop()
		visited[ele] = 1
		neibh = map_[ele]
		if visited[neibh] and map_[neibh] != ele:
			if visited.count(1) == 3:
				return True
		if not visited[neibh]:
			stack.append(neibh)
	return False
x = int(input())
eles = [int(num) for num in input().split(' ')]
map_ = {}
for (i, ele) in enumerate(eles):
	map_[i + 1] = ele
visited = [0] * (len(eles) + 1)
is_cycle = False
for key in map_:
	visited = [0] * (len(eles) + 1)
	is_cycle = isCycle(map_, visited, key)
	if is_cycle:
		break
if is_cycle:
	print('YES')
else:
	print('NO')


number = int(input())
vershini = [-1] + [int(c) for c in input().split(' ')]
a = True
it = 1
while it <= number:
	ab = vershini[it]
	itr = 0
	while itr < 2:
		ab = vershini[ab]
		itr += 1
	if ab == it:
		print('YES')
		a = False
		break
	it += 1
if a == True:
	print('NO')


n = int(input())
l = list()
s = str(input()).split()
c = 0
for k in s:
	l.append(int(k))
for b in range(1, n + 1):
	x = l[b - 1]
	if x <= n:
		y = l[x - 1]
	else:
		continue
	if y <= n:
		z = l[y - 1]
	else:
		continue
	if z <= n:
		w = l[z - 1]
	else:
		continue
	if w == x:
		c = c + 1
if c > 0:
	print('YES')
else:
	print('NO')


n = int(input())
array = [int(i) for i in input().split()]
for i in array:
	if array[array[array[i - 1] - 1] - 1] == i:
		print('YES')
		exit()
print('NO')


import sys
n = int(input())
g = list(map(lambda d: int(d) - 1, input().strip().split()))
for i in range(n):
	a = i
	b = g[i]
	c = g[b]
	if a == g[c]:
		print('YES')
		sys.exit(0)
print('NO')


a = int(input())
b = list(map(int, input().split()))
x = k = int(0)
while x < a:
	c = b[x] - 1
	if b[b[c] - 1] != b[x]:
		d = b[c] - 1
		if b[b[d] - 1] != b[c] and b[b[d] - 1] == b[x]:
			print('YES')
			k = 1
			break
	x = x + 1
if k != 1:
	print('NO')


n = int(input())
a = [int(i) - 1 for i in input().split()]
for i in range(n):
	if a[a[a[i]]] == i:
		print('YES')
		return
print('NO')


def love_is(n, base):
	if n > 1:
		return love_is(n - 1, love[base])
	else:
		return love[base]
n = int(input())
love = list((int(i) - 1 for i in input().split()))
res = 'NO'
for i in range(n):
	if love_is(3, i) == i:
		res = 'YES'
		break
print(res)


n = int(input())
f = [0] + list(map(int, input().split()))
ok = False
for i in range(1, n + 1):
	if f[f[f[i]]] == i:
		ok = True
		print('YES')
		break
if not ok:
	print('NO')


n = int(input())
f = [-1] + list(map(int, input().split()))
mark = False
for i in range(n):
	a = f[i]
	b = f[a]
	c = f[b]
	if f[c] == a:
		mark = True
if mark:
	print('YES')
else:
	print('NO')


n = int(input())
f = input().split()
ans = 0
for i in range(n):
	f[i] = int(f[i]) - 1
for i in range(n):
	if f[f[f[i]]] == i:
		ans = 1
		break
if ans == 1:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
cnt = 0
for i in range(n):
	if i == a[a[a[i] - 1] - 1] - 1:
		cnt += 1
if cnt:
	print('Yes')
else:
	print('No')


n = int(input())
pl = [n]
for i in map(int, input().split()):
	pl.append(i)
flag = False
if n > 2:
	for A in pl:
		if A > n:
			continue
		B = pl[A]
		if B > n:
			continue
		C = pl[B]
		if C > n:
			continue
		if pl[C] == A:
			flag = True
			break
print('YES' if flag else 'NO')


ch = 0
n = int(input())
b = dict()
a = list(map(int, input().split()))
for i in range(1, n + 1):
	b[i] = a[i - 1]
for i in range(1, n + 1):
	if b[b[b[i]]] == i:
		ch = 1
if ch == 1:
	print('YES')
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
for __ in range(n):
	if f[f[f[__] - 1] - 1] - 1 == __:
		print('YES')
		break
else:
	print('NO')


a = int(input())
l = list(map(int, input().split()))
l1 = [i - 1 for i in l]
for i in range(a):
	x = l1[i]
	y = l1[x]
	z = l1[y]
	if i == z and x != y and (y != z) and (x != z) and (i != x) and (i != y):
		print('YES')
		exit(0)
print('NO')


n = int(input().strip())
found = False
arr = [int(i) for i in input().strip().split()]
dic = {}
for i in range(len(arr)):
	dic[i + 1] = arr[i]
for i in dic:
	x = dic[i]
	if x in dic:
		y = dic[x]
		if y in dic:
			if dic[y] != i:
				pass
			else:
				found = True
				break
print('YES' if found else 'NO')


input()
list = list(map(int, input().split()))
found = False
for (i, b) in enumerate(list):
	a = i + 1
	c = list[b - 1]
	if list[c - 1] == a:
		print('YES')
		found = True
		break
if not found:
	print('NO')


num = int(input())
arr = [0] * num
arr = input().split()
for i in range(num):
	arr[i] = int(arr[i])
fifa = False
for i in range(num):
	a = arr[i]
	b = arr[a - 1]
	c = arr[b - 1]
	if c == i + 1:
		fifa = True
if fifa == True:
	print('YES')
else:
	print('NO')


import sys
total = int(input())
love = [int(x) for x in input().split()]
for i in range(len(love)):
	count = 2
	temp = love[i]
	while temp != i + 1 and count != 0:
		count = count - 1
		temp = love[temp - 1]
	if temp == i + 1 and count == 0:
		print('YES')
		sys.exit()
print('NO')


n = int(input())
f = list(map(lambda x: int(x) - 1, input().split()))
res = False
for i in range(n):
	if f[f[f[i]]] == i:
		res = True
		break
	else:
		res = False
if res:
	print('YES')
else:
	print('NO')


import sys

def read_int():
	return int(input())

def read_ints():
	return [int(x) for x in input().split()]
n = read_int()
a = read_ints()
for i in range(n):
	if a[a[a[i] - 1] - 1] == i + 1:
		print('YES')
		return
print('NO')


n = int(input())
f = [0] + [int(x) for x in input().split()]
answer = 'NO'
for i in range(1, n):
	if f[f[f[i]]] == i:
		answer = 'YES'
		break
print(answer)


n = int(input())
t = False
s = input().split()
A = list(map(int, s))
for i in range(n):
	if i + 1 == A[A[A[i] - 1] - 1]:
		t = True
		break
if t:
	print('YES')
else:
	print('NO')


n = int(input())
f = input().split()
lt1 = []
lt2 = []
tf = False
zero = 0
for i in f:
	lt1.append(int(i))
for j in range(n):
	if j in lt2:
		continue
	lt2.append(j)
	zero = lt1[j] - 1
	lt2.append(n)
	zero = lt1[zero] - 1
	lt2.append(n)
	zero = lt1[zero] - 1
	if zero == j:
		tf = True
if tf == True:
	print('YES')
else:
	print('NO')


n = int(input())
s = input()
s = s.split()
s = [0] + s
for i in range(n + 1):
	s[i] = int(s[i])
c = 0
for i in range(1, n + 1):
	if int(s[s[s[i]]]) == i:
		print('Yes')
		c = 1
		break
if c == 0:
	print('No')


n = int(input())
data = list(map(int, input().split()))
found = False
for (i, v) in enumerate(data):
	if data[data[v - 1] - 1] - 1 == i:
		found = True
		break
print(('NO', 'YES')[found])


n = int(input())
f = list(map(int, input().split(' ')))
f = [fi - 1 for fi in f]
exit_loop = False
for (i, fi) in enumerate(f):
	fj = f[fi]
	if fj != i and f[fj] == i:
		print('YES')
		exit_loop = True
		break
if not exit_loop:
	print('NO')


n = int(input())
pl_idx___love = [int(x) - 1 for x in input().split()]
for pl_idx in range(n):
	next_idx = pl_idx___love[pl_idx]
	next_idx = pl_idx___love[next_idx]
	next_idx = pl_idx___love[next_idx]
	if next_idx == pl_idx:
		print('YES')
		exit()
print('NO')


def smanji(f):
	for i in range(len(f)):
		f[i] -= 1

def razliciti(A, B, C):
	s = {A, B, C}
	return len(s) == 3

def trougao(A, f):
	B = f[A]
	C = f[B]
	return razliciti(A, B, C) and A == f[C]
n = int(input())
f = list(map(int, input().split()))
smanji(f)
ima = False
for i in range(n):
	ima = ima or trougao(i, f)
	if ima:
		break
print('YES' if ima else 'NO')


n = int(input())
num = 0
flag = 0
list = [int(inp) for inp in input().split()]
for f in list:
	if list[list[f - 1] - 1] == num + 1:
		print('YES')
		flag = 1
		break
	num += 1
if flag == 0:
	print('NO')


n = int(input())
f = [0, *map(int, input().split())]
print('YNEOS'[all((f[x] == x == f[f[x]] or f[f[f[x]]] != x for x in range(1, n + 1)))::2])


n = int(input())
A = [*map(int, input().split())]
ans = False
for a in range(1, n):
	b = A[a - 1]
	c = A[b - 1]
	d = A[c - 1]
	if d == a:
		ans = True
print('YES' if ans else 'NO')


n = int(input())
k = [int(n) for n in input().split()]
isTrue = False
for i in range(0, n):
	y = k[i]
	z = k[y - 1]
	if k[z - 1] == i + 1:
		isTrue = True
		break
if isTrue:
	print('YES')
else:
	print('NO')


class Node:

	def __init__(self, n):
		self.indicator = n

	def __contains__(self, v):
		self.next = v
		return True

	def detect(self, n=None, depth=0):
		current = self if not n else n
		if int.__eq__(depth, 3):
			return True if n is self else False
		try:
			return self.detect(getattr(current, 'next'), depth + 1)
		except:
			return False
n = int(input())
v = [int(v) - 1 for v in input().split()]
if len(v) != n or not 2 <= n <= 5000:
	raise KeyError('Error')
g = [Node(i) for i in range(len(v))]
f = False
[g[v[c]] in g[c] for c in range(len(v))]
for e in g:
	if e.detect():
		f = not f
		break
print('YES' if f else 'NO')


N = int(input())
l = str(input())
ls = [int(x) - 1 for x in l.split()]

def love(x, times=3):
	a = ls[x]
	if times == 1:
		return a
	else:
		return love(a, times=times - 1)
for i in range(N):
	if love(i) == i:
		print('YES')
		exit()
print('NO')


vertices = int(input())
avioes = list(map(lambda x: int(x) - 1, input().split()))
for vertice in range(vertices):
	if avioes[avioes[avioes[vertice]]] == vertice:
		print('YES')
		exit(0)
print('NO')


n = int(input())
lst = list(map(int, input().split()))
d = {}
for i in range(1, n + 1):
	d[i] = set()
for i in range(n):
	d[i + 1].add(lst[i])
	d[lst[i]].add(i + 1)
visited = [0] * (n + 1)
parent = [0] * (n + 1)
flag = 0
q = []

def bfs(s):
	global visited, parent, d, n, flag, q
	visited[s] = 1
	parent[s] = -1
	q.append(s)
	while q:
		p = q.pop(0)
		for i in d[p]:
			if visited[i] == 0:
				visited[i] = 1
				parent[i] = p
				q.append(i)
			elif parent[i] == parent[p]:
				flag = 1
				return
comp = 0
for i in range(1, n + 1):
	if visited[i] == 0:
		comp += 1
		pp = bfs(i)
if flag == 1:
	print('YES')
else:
	print('NO')


k = int(input())
arr = list(map(int, input().split()))
otv = 'NO'
for i in range(k - 1, -1, -1):
	if i + 1 == arr[arr[arr[i] - 1] - 1]:
		otv = 'YES'
		break
print(otv)


import sys
planes = int(input().rstrip())
likes = list(map(int, input().rstrip().split()))
is_triangle = False
for i in range(planes):
	i_likes = likes[i]
	j_likes = likes[i_likes - 1]
	k_likes = likes[j_likes - 1]
	if k_likes == i + 1:
		is_triangle = True
		break
if is_triangle:
	print('YES')
else:
	print('NO')


gi = lambda : list(map(int, input().split()))
(n,) = gi()
l = [0] + gi()
print('YNEOS'[len([1 for k in range(1, n + 1) if l[l[l[k]]] == k and l[k] != l[l[k]] and (l[l[k]] != k)]) == 0::2])


n = int(input())
a = []
count = 0
a.append(0)
x = list(map(int, input().rstrip().split()))
a = a + x
for i in range(1, n + 1):
	if a[a[a[i]]] == i:
		print('YES')
		count = 1
		break
if count == 0:
	print('NO')


n = int(input())
l = [int(i) for i in input().split()]
ans = 'NO'
for i in range(n):
	if l[l[l[i] - 1] - 1] == i + 1:
		ans = 'YES'
		break
print(ans)


a = input()
a = input().strip().split()
out = 'NO'
for i in range(len(a)):
	a[i] = int(a[i])
for i in range(len(a)):
	if a[a[i] - 1] != i + 1 and a[a[a[i] - 1] - 1] == i + 1:
		out = 'YES'
print(out)


n = input()
arr = list(map(int, input().split(' ')))
result = False
for i in arr:
	if i == arr[arr[arr[i - 1] - 1] - 1]:
		result = True
print('YES' if result else 'NO')


t = 1
while t:
	t -= 1
	n = int(input())
	ok = 0
	f = list(map(int, input().split()))
	for i in range(n):
		if f[f[f[i] - 1] - 1] - 1 == i:
			ok = 1
	if ok:
		print('YES')
	else:
		print('NO')


from collections import defaultdict as dd
n = int(input())
a = [int(i) for i in input().split()]
d = dd(int)
for i in range(len(a)):
	d[i + 1] = a[i]
poss = 0
for i in range(len(a)):
	if i + 1 == d[d[a[i]]]:
		poss = 1
		break
if poss:
	print('YES')
else:
	print('NO')


input()
l = list(map(lambda x: int(x) - 1, input().split()))
for i in l:
	if l[l[l[i]]] == i and i != l[i] and (i != l[l[i]]):
		print('YES')
		exit()
print('NO')


n = int(input())
G = [int(x) for x in input().split()]
G.insert(0, 0)
love = False
for i in range(1, n + 1):
	if i == G[G[G[i]]]:
		love = True
if love:
	print('YES')
else:
	print('NO')


n = int(input())
str = input()
a = str.split(' ')
b = []
for i in a:
	b.append(int(i))
judge = False
for j in range(n):
	if b[b[b[j] - 1] - 1] - 1 == j:
		judge = True
		break
if judge:
	print('YES')
else:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
ok = False
for a in range(n):
	b = f[a] - 1
	c = f[b] - 1
	d = f[c] - 1
	if d == a and b != a and (c != b):
		ok = True
if ok:
	print('YES')
else:
	print('NO')


n = int(input())
found = False
planes = {}
for (i, plane) in enumerate(list(map(int, input().split()))):
	planes[i + 1] = plane
for i in planes:
	if planes[planes[planes[i]]] == i:
		found = True
		break
print('YES' if found else 'NO')


n = int(input())
ar = [0] + list(map(int, input().split()))
visited = [False] * (n + 1)
flag = False

def condition(d, cur):
	ar = set(d.keys())
	ar1 = set(d.values())
	return len(ar) == 3 and ar == ar1
for i in range(1, n + 1):
	cur = i
	d = dict()
	while cur not in d:
		d[cur] = ar[cur]
		cur = ar[cur]
	if condition(d, cur):
		flag = True
		break
print('YES' if flag else 'NO')


n = int(input())
f = [int(x) - 1 for x in input().split()]
print('YES' if len([1 for x in f if f[f[f[x]]] == x]) > 0 else 'NO')


class CodeforcesTask939ASolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.edges = []

	def read_input(self):
		self.n = int(input())
		self.edges = [int(x) for x in input().split(' ')]

	def process_task(self):
		can = False
		for x in range(self.n):
			a = x + 1
			b = self.edges[a - 1]
			c = self.edges[b - 1]
			d = self.edges[c - 1]
			if d == a:
				can = True
				break
		self.result = 'YES' if can else 'NO'

	def get_result(self):
		return self.result
Solution = CodeforcesTask939ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


n = int(input())
a = list(map(int, input().split(' ')))
c = 0
a.insert(0, 0)
for i in range(1, len(a)):
	if a[a[a[i]]] == i:
		c += 1
		break
if c > 0:
	print('yes')
else:
	print('no')


from sys import stdin, stdout
from collections import *
from math import ceil, floor, log, gcd
st = lambda : list(stdin.readline().strip())
li = lambda : list(map(int, stdin.readline().split()))
mp = lambda : map(int, stdin.readline().split())
inp = lambda : int(stdin.readline())
pr = lambda n: stdout.write(str(n) + '\n')
mod = 1000000007
INF = float('inf')

def solve():
	n = inp()
	l = [0] + li()
	for i in range(1, n + 1):
		y = i
		x = l[i]
		z = l[x]
		if l[z] == i:
			pr('YES')
			return
	pr('NO')
for _ in range(1):
	solve()


import sys
from collections import defaultdict
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def print_array(a):
	print(' '.join(map(str, a)))

class Graph:

	def __init__(self):
		self.graph = defaultdict(list)

	def addEdge(self, u, v):
		self.graph[u].append(v)

def loveTriangle(g, n):
	flag = False
	for i in range(1, n + 1):
		if g.graph[g.graph[g.graph[i][0]][0]][0] == i:
			flag = True
			break
	return flag

def main():
	n = int(input())
	g = Graph()
	a = get_array()
	for i in range(1, n + 1):
		g.addEdge(i, a[i - 1])
	print('YES' if loveTriangle(g, n) else 'NO')
main()


class node:

	def __init__(self, index):
		self.index = index
n = int(input())
iarr = list(map(int, input().split()))
arr = []
for i in iarr:
	arr.append(node(i - 1))
for i in range(n):
	a = arr[i].index
	b = arr[a].index
	c = arr[b].index
	if c == i:
		print('YES')
		exit(0)
print('NO')


n = int(input())
a = [int(i) for i in input().split()]
flag = 0
for i in range(0, len(a)):
	l = []
	s = []
	l.append(i + 1)
	s.append(a[i])
	l.append(s[len(s) - 1])
	s.append(a[l[len(l) - 1] - 1])
	l.append(s[len(s) - 1])
	s.append(a[l[len(l) - 1] - 1])
	l = sorted(l)
	s = sorted(s)
	if l == s:
		flag = 1
		break
if flag == 0:
	print('NO')
else:
	print('YES')


n = int(input())
connections = list(map(int, input().split(' ')))
graph = [0]
for i in range(n):
	graph.append(connections[i])

def check():
	for x in range(1, n):
		v = graph[x]
		y = graph[v]
		if graph[y] == x:
			return 'YES'
	return 'NO'
print(check())


n = int(input())
love = [int(c) for c in input().split()]
d = {i: e for (i, e) in enumerate(love, 1)}
for (k, v) in d.items():
	a = k
	b = d[k]
	c = d[b]
	last = d[c]
	if a != c and last == a:
		print('YES')
		break
else:
	print('NO')


def samolety(lst):
	for i in range(1, len(lst) + 1):
		x = i
		for j in range(3):
			x = lst[x - 1]
		if x == i:
			return 'YES'
	return 'NO'
n = int(input())
a = [int(z) for z in input().split()]
print(samolety(a))


n = int(input())
str_lst = input().split()
lst = [int(i) for i in str_lst]
hasPrint = False
for i in range(len(lst)):
	a = str(lst[i])
	b = str(lst[int(a) - 1])
	c = str(lst[int(b) - 1])
	if int(c) == i + 1:
		print('YES')
		hasPrint = True
		break
if not hasPrint:
	print('NO')


vertexN = int(input())
graph = []
l = list(map(int, input().split(' ')))
for i in range(vertexN):
	graph.append([l[i]])
color = []
go = []
for i in range(vertexN):
	color.append(0)
	go.append(0)

def dfs(v, c):
	go[v] = c
	color[v] = 1
	for i in graph[v]:
		if color[i - 1] == 0:
			dfs(i - 1, c + 1)
		if color[i - 1] == 1:
			if abs(go[i - 1] - go[v]) == 2:
				print('YES')
				exit(0)
for i in range(vertexN):
	for j in range(vertexN):
		color[j] = 0
		go[j] = 0
	dfs(i, 1)
print('NO')


a = int(input())
l = input().split()
l.insert(0, 0)
flag = False
for i in range(a):
	if l[i] == l[int(l[int(l[int(l[i])])])] and l[i] != l[int(l[i])] and (l[int(l[int(l[i])])] != l[int(l[i])]):
		print('YES')
		flag = True
		break
if flag == False:
	print('NO')


n = int(input())
c = 1
str1 = input()
listNum = []
listNum[:0] = str1.split(' ')
dictKV = {}
for x in range(1, n + 1):
	dictKV[x] = int(listNum[x - 1])
for (key, value) in dictKV.items():
	x = dictKV[value]
	y = dictKV[x]
	if y == key:
		c = 2
		print('YES')
		exit()
if c == 1:
	print('NO')


n = int(input())
l = list(map(int, input().split(' ')))
k = 0
for a in range(len(l)):
	b = l[l[a] - 1]
	c = l[b - 1]
	if c - 1 == a:
		k = 1
if k == 1:
	print('YES')
else:
	print('NO')


n = int(input())
fs = [int(a) - 1 for a in input().split()]
love = False
for i in range(n):
	if fs[fs[fs[i]]] == i:
		love = True
if love:
	print('YES')
else:
	print('NO')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	n = I()
	l = LI()
	for (i, x) in enumerate(l):
		y = l[x - 1]
		z = l[y - 1]
		if i + 1 == z:
			return 'YES'
	return 'NO'
print(main())


n = int(input())
from collections import defaultdict
l = list(map(int, input().split()))
hash = defaultdict(int)
for i in range(1, n + 1):
	hash[i] = l[i - 1]
for i in hash:
	z = hash[i]
	k = hash[z]
	if hash[k] == i:
		print('YES')
		exit()
print('NO')


n = int(input())
arr = [int(x) for x in input().split()]
for i in range(1, n):
	b = arr[i - 1]
	c = arr[b - 1]
	if arr[c - 1] == i:
		print('YES')
		exit()
print('NO')


n = int(input())
a = [int(i) for i in input().split()]
b = -100
c = -100
for i in range(n):
	k = a[i]
	if k == i + 1:
		pass
	else:
		b = a[k - 1]
		c = a[b - 1]
		if k == a[c - 1] and (c != b and k != b and (k != c)):
			print('YES')
			break
else:
	print('NO')


i = input
n = int(i())
a = [0] + [int(i) for i in i().split()]
for i in range(1, n + 1):
	q = {i, a[i], a[a[i]]}
	w = {a[i], a[a[i]], a[a[a[i]]]}
	if q == w and len(q) == 3:
		print('YES')
		break
else:
	print('NO')


n = int(input())
arr = list(map(int, input().split()))
arr.insert(0, 0)
b = False
for i in range(1, len(arr)):
	if arr[arr[arr[i]]] == i:
		b = True
		break
if b:
	print('YES')
else:
	print('NO')


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(beauty, s, n, count):
	pass
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect

def solve():
	n = scanner()
	f = get_list()
	for (i, x) in enumerate(f):
		if f[f[x - 1] - 1] != x and f[x - 1] != x and (f[f[f[x - 1] - 1] - 1] == x):
			print('YES')
			return
	print('NO')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(1):
	solve()


def fun(*l):
	for i in range(0, n):
		a = l[i]
		b = l[a - 1]
		c = l[b - 1]
		if l[c - 1] == a:
			return 'Yes'
	return 'No'
n = int(input())
l = input()
l = l.split(' ')
for i in range(0, len(l)):
	l[i] = int(l[i])
print(fun(*l))


def read_tokens():
	return input().strip().split(' ')

def read_ints():
	return [int(s) for s in read_tokens()]
n = int(input())
arr = read_ints()
graph = [[] for _ in range(n)]
visited = [0] * n
parent = [-1] * n

def add_edge(u, v):
	global graph
	graph[u].append(v)
for u in range(n):
	v = arr[u]
	v = v - 1
	add_edge(u, v)
end = -1
start = -1

def dfs(u, depth):
	global visited
	global end
	global start
	visited[u] = 1
	for v in graph[u]:
		if visited[v] == 0:
			parent[u] = v
			if dfs(v, depth + 1):
				return True
		elif visited[v] == 1 and depth == 3:
			end = v
			start = u
			return True
	visited[u] = 2
	return False

def solve():
	global visited
	global end
	global start
	cycle = False
	for i in range(n):
		for j in range(n):
			visited[j] = 0
		if visited[i] == 0 and dfs(i, 1):
			cycle = True
			break
	if start == -1 and (not cycle):
		print('NO')
	else:
		ans = []
		ans.append(start)
		while end != start:
			ans.append(end)
			end = parent[end]
		ans.append(start)
		ans.reverse()
		if len(set(ans)) == 3:
			print('YES')
		else:
			print('NO')
solve()


def is_love_triangle(arr):
	like_map = {}
	for i in range(len(arr)):
		if arr[arr[arr[i] - 1] - 1] == i + 1:
			return True
	return False
n = int(input())
print('YES' if is_love_triangle(list(map(int, input().split()))) else 'NO')


n = int(input())
x = [int(x) for x in input().split()]
g = {}
found = False

def love(n):
	return n == g[g[g[n]]]
for n in range(1, n + 1):
	g[n] = x[n - 1]
for i in range(1, n + 1):
	if love(i):
		found = True
		break
if found:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a[i] -= 1
flag = 0
for i in range(n):
	if a[a[a[i]]] == i:
		print('Yes')
		flag = 1
		break
if flag == 0:
	print('No')


n = int(input())
p = [int(i) for i in input().split()]
y = 'NO'
for i in range(n):
	if i == p[p[p[i - 1] - 1] - 1]:
		y = 'YES'
		break
print(y)


n = eval(input())
s = input()
temp_l = [int(i) for i in s.split(' ')]
l = [temp_l[i] for i in range(n)]
flag = 0
for i in range(n):
	temp1 = l[i]
	temp2 = l[temp1 - 1]
	temp3 = l[temp2 - 1]
	if temp3 == i + 1:
		flag = 1
		break
if flag == 1:
	print('YES')
else:
	print('NO')


count = int(input())
vector = [int(x) for x in input().split(' ')]
Boolean = False
for i in range(1, count + 1):
	if vector[vector[vector[i - 1] - 1] - 1] == i:
		print('YES')
		Boolean = True
		break
if Boolean is False:
	print('NO')


n = int(input())
g = list(map(int, input().split()))
for i in range(n):
	if g[g[g[i] - 1] - 1] == i + 1:
		print('YES')
		exit()
print('NO')


n = int(input())
a = input().split()
for i in range(n):
	a[i] = int(a[i]) - 1
for i in range(n):
	j = a[i]
	k = a[j]
	if i != k and a[k] == i:
		break
print(['YES', 'NO'][int(i == n - 1)])


n = int(input())
d = list(map(int, input().split()))
ok = 0
for i in range(len(d)):
	if d[d[i] - 1] != i and d[d[d[i] - 1] - 1] - 1 == i:
		ok = 1
print('YES') if ok else print('NO')


num = int(input())
lst = input().split()
lst = [int(x) - 1 for x in lst]
temp = 0
found = False
for i in range(len(lst)):
	temp = lst[i]
	temp = lst[temp]
	if i == lst[temp]:
		print('YES')
		found = True
		break
if not found:
	print('NO')


import sys
from itertools import permutations
m = 1000000007
n = int(input())
a = list()
a.append(0)
an = list(map(int, input().strip().split(' ')))
for num in an:
	a.append(num)
for i in range(1, n + 1):
	if a[a[a[i]]] == i:
		print('YES')
		break
if i == n:
	print('NO')


n = int(input())
lista = [int(x) for x in input().split()]
v = {i + 1: lista[i] for i in range(n)}
res = 'NO'
for i in range(1, n, 1):
	if v[v[v[i]]] == i:
		res = 'YES'
		break
print(res)


n = int(input())
arr = [int(num) for num in input().split()]
for i in arr:
	if arr.index(i) + 1 == arr[arr[i - 1] - 1]:
		print('YES')
		break
else:
	print('NO')


import sys
from math import sqrt, floor, factorial
from collections import deque, Counter
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def gcd(a, b):
	mini = min(a, b)
	maxi = max(a, b)
	while maxi % mini != 0:
		(maxi, mini) = (mini, maxi % mini)
	return mini

def f(i, arr):
	return i == arr[arr[arr[i - 1] - 1] - 1]

def solve():
	n = int(inp())
	arr = read()
	done = set()
	for i in arr:
		if f(i, arr):
			print('YES')
			exit()
	print('NO')
solve()


from sys import stdin as si
from collections import Counter as c

class Solution:

	def __init__(self):
		self.track = []

	def lovetriangle(self, m, i):
		if len(self.track) == 2:
			if m[i] - 1 == self.track[0]:
				print('YES')
				exit(0)
			else:
				return
		else:
			self.track.append(i)
			self.lovetriangle(m, m[i] - 1)
			self.track.pop(-1)
n = si.readline().strip()
m = list(map(int, si.readline().strip().split()))
S = Solution()
for i in range(len(m)):
	S.lovetriangle(m, i)
else:
	print('NO')


input()
L = list(map(int, input().split()))
L.insert(0, 0)
love = False
visited = ''
i = 1
while i < len(L):
	if i == L[L[L[i]]]:
		print('YES')
		love = True
		break
	i += 1
if not love:
	print('NO')


n = int(input())
f = list(map(int, input().split()))
b = 1
for i in range(n):
	A = i
	B = f[i] - 1
	C = f[B] - 1
	res = f[C] - 1 == A
	if res:
		print('YES')
		b = 0
		break
if b:
	print('NO')


n = int(input())
li = list(map(int, input().split()))
flag = 'NO'
for i in range(n):
	t = li[i]
	x = li[t - 1]
	y = li[x - 1]
	z = li[y - 1]
	if t == z:
		flag = 'YES'
		break
print(flag)


n = int(input())
a = input()
a = a.split()
for k in range(len(a)):
	a[k] = int(a[k])
koft = 'No'
for i in range(n - 1):
	if a[a[a[a[i] - 1] - 1] - 1] == a[i]:
		if a[i] != i:
			koft = 'Yes'
			break
print(koft)


n = int(input())
s = str(input()).split()
l = list()
for i in s:
	l.append(int(i))
d = dict()
for i in range(1, n + 1):
	d[i] = l[i - 1]
f = 0
for i in range(1, n + 1):
	s = i
	a = d[s]
	b = d[a]
	c = d[b]
	if c == i:
		print('YES')
		f = 1
		break
if f == 0:
	print('NO')


n = int(input())
l = [int(x) for x in input().split()]
flag = 0
for i in range(n):
	x = l[i] - 1
	y = l[x] - 1
	z = l[y] - 1
	if l[z] == l[i] and l[y] != l[i]:
		flag = 1
		print('YES')
		break
if flag == 0:
	print('NO')


def is_love(l):
	s = set(l)
	if len(s) < 3:
		print('NO')
	else:
		possible = False
		for i in range(len(l)):
			a = l[i]
			b = l[a - 1]
			c = l[b - 1]
			if a != b and b != c and (c != a) and (l[c - 1] == a):
				print('YES')
				possible = True
				break
		if not possible:
			print('NO')
n = int(input())
l = [int(x) for x in str(input()).split(' ')]
is_love(l)


n = int(input())
f = [0] + [int(i) for i in input().split()]
t = 0
for i in range(1, n + 1):
	if f[f[f[i]]] == i:
		t = 1
if t:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
if n == 2:
	print('NO')
else:
	f = 0
	for i in range(n):
		a[i] -= 1
	for i in range(n):
		if a[a[a[i]]] == i:
			f = 1
			break
	if f:
		print('YES')
	else:
		print('NO')


def main():
	n = int(input())
	ip = [int(item) for item in input().split(' ')]
	for i in range(len(ip)):
		if ip[ip[ip[i] - 1] - 1] - 1 == i:
			print('YES')
			exit()
	print('NO')
main()


n = int(input())
lst = map(int, input().split())
lst = [i - 1 for i in lst]
ans = False
(first, second, third) = ([], [], [])
for i in range(len(lst)):
	first = [i, lst[i]]
	second = [first[1], lst[first[1]]]
	third = [second[1], lst[second[1]]]
	if third[1] == first[0]:
		ans = True
		break
print('YES\n') if ans else print('NO\n')


n = input()
a = [0] + list(map(int, input().split()))
f = False
for i in range(1, len(a)):
	(x, y, z) = (a[i], a[a[i]], a[a[a[i]]])
	if x != y and y != z and (x != z) and (z == i):
		f = True
print('YES' if f else 'NO')


num = int(input())
planes = list(map(int, input().split()))
yes = 0
for i in range(num - 1):
	point1 = planes[i]
	point2 = planes[point1 - 1]
	if planes[point2 - 1] != i:
		point3 = planes[point2 - 1]
		if point3 - 1 == i:
			yes += 1
if yes >= 1:
	print('YES')
else:
	print('NO')


import sys
inf = float('inf')
mod = 1000000007

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())

def input():
	return sys.stdin.readline()

def int1():
	return int(input())
import string
import math
from itertools import combinations

def main():
	t = int(input())
	a = get_array()
	i = 1
	v = 0
	k = 0
	while i <= t:
		r = a[i - 1]
		y = a[r - 1]
		z = a[y - 1]
		if a[z - 1] == a[i - 1]:
			print('YES')
			k = 1
			break
		i = i + 1
	if k == 0:
		print('NO')
main()


import sys
n = int(sys.stdin.readline().split()[0])
airplanes = sys.stdin.readline().split()
loss = True
for i in range(n):
	airplanes[i] = int(airplanes[i])
for i in range(n):
	a = [airplanes[i]]
	for j in range(3):
		a.append(airplanes[a[j] - 1])
	if a[0] == a[3]:
		print('YES')
		loss = False
		break
if loss == True:
	print('NO')


import sys
n = int(input())
l = list(map(int, input().split()))
for i in range(0, n):
	if l[l[l[i] - 1] - 1] == i + 1:
		print('YES')
		sys.exit(0)
print('NO')


import sys
n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	d[i + 1] = a[i]
for i in range(1, n + 1):
	if d[d[d[i]]] == i:
		print('YES')
		sys.exit(0)
	else:
		continue
print('NO')


repeat = int(input())
N = [int(i) - 1 for i in input().split()]
err = False
for i in range(repeat):
	if N[N[N[i]]] == i:
		print('YES')
		err = True
		break
if not err:
	print('NO')


def main():
	n = int(input())
	vals = [int(v) - 1 for v in input().split()]
	data = {}
	for (i, v) in enumerate(vals):
		if v not in data:
			data[v] = []
		data[v].append(i)
	for i in range(n):
		for j in range(n):
			if i == j:
				continue
			if vals[i] == j and i in data and (vals[j] in data[i]):
				print('YES')
				return
	print('NO')
main()


from sys import maxsize
N = int(input())
a = list(map(lambda x: int(x), input().strip().split()))
a.insert(0, maxsize)
for (index, value) in enumerate(a):
	if index > 0:
		if a[a[value]] == index:
			print('YES')
			break
else:
	print('NO')


N = int(input())
n = [int(x) - 1 for x in input().split()]
q = 0
ans = 'h'
for (a, b) in enumerate(n):
	q = n[b]
	if n[q] == a:
		ans = 'YES'
if ans == 'YES':
	print('YES')
else:
	print('NO')


n = int(input())
f = [int(s) for s in input().split(' ')]
answer = 'NO'
for i in range(n):
	if f[f[f[i] - 1] - 1] == i + 1:
		answer = 'YES'
		break
print(answer)


n = int(input())
F = [1] + list(map(int, input().split()))
fl = [1] * (n + 1)
ans = 'NO'
for x in range(1, n + 1):
	if fl[x]:
		y = x
		l = []
		while fl[y]:
			l.append(y)
			fl[y] = 0
			y = F[y]
		if len(l) > 2 and F[l[len(l) - 1]] == l[len(l) - 3]:
			ans = 'YES'
print(ans)


input()
A = list(map(int, input().split()))
found = False
for i in range(len(A)):
	a = A[i]
	b = A[a - 1]
	c = A[b - 1]
	d = A[c - 1]
	if a == d:
		found = True
		break
if found:
	print('YES')
else:
	print('NO')


n = int(input())
l1 = list(map(int, input().split()))
flag = 0
for i in range(len(l1)):
	a = l1[i]
	b = l1[a - 1]
	if l1[b - 1] == i + 1:
		flag = 1
		break
if flag == 1:
	print('YES')
else:
	print('NO')


N = int(input())
k = 3
connections = list(map(int, input().split()))
connections.insert(0, 0)
seen = []
for i in range(N + 5):
	seen.append(False)
for i in range(1, N + 1):
	current = i
	current_path = []
	while not seen[current]:
		seen[current] = True
		current_path.append(current)
		current = connections[current]
	if len(current_path) >= k and current == current_path[len(current_path) - k]:
		print('yes')
		quit()
print('no')


n = input()
d = list(map(lambda x: int(x) - 1, input().split()))
for a in range(len(d)):
	b = d[a]
	c = d[b]
	if a == d[c]:
		print('YES')
		exit(0)
print('NO')


n = int(input())
a = []
asd = 0
a.append(list(map(int, input().split())))
for i in range(n):
	t = i
	zxc = i
	for qwe in range(3):
		t = a[0][t] - 1
	if t == zxc:
		print('YES')
		asd = 1
		break
if asd == 0:
	print('NO')


n = int(input())
if n >= 2 and n <= 5000:
	A = list(map(int, input().split()))
	ls = []
	i = 1
	cnt = 1
	ls.append(1)
	for i in range(n):
		if A[A[A[i] - 1] - 1] - 1 == i:
			print('YES')
			cnt = 0
			break
	if cnt:
		print('NO')


class LoveTriangle:

	def main(self):
		self.num = int(input())
		self.inp = [' ']
		self.inp.extend([int(x) for x in input().split()])
		self.size = len(self.inp)
		for i in range(1, self.size):
			self.tempA = i
			self.tempB = self.inp[self.tempA]
			self.tempC = self.inp[self.tempB]
			self.tempD = self.inp[self.tempC]
			if int(self.tempA) == int(self.tempD):
				print('YES')
				return
		print('NO')
lv = LoveTriangle()
lv.main()


n = int(input())
l = list(map(int, input().split()))
l.insert(0, 0)
flag = 0
for i in range(1, len(l)):
	if l[l[l[i]]] == i:
		flag = 1
		print('YES')
		break
if flag == 0:
	print('NO')


n = int(input())
list = input().split(' ')
for i in range(0, n):
	list[i] = int(list[i])
bool = False
for i in range(0, n):
	if i + 1 == list[list[list[i] - 1] - 1]:
		bool = True
if bool == True:
	print('Yes')
else:
	print('No')


n = int(input())
arr = list(map(int, input().split()))
for i in range(n):
	x = arr[i]
	y = arr[x - 1]
	z = arr[y - 1]
	if i == z - 1:
		print('YES')
		exit(0)
print('NO')


n = int(input())
l = list(map(int, input().split()))
m = {}
for i in range(1, n + 1):
	m[i] = l[i - 1]
f = 0
for i in range(1, n + 1):
	try:
		if m[m[m[i]]] == i:
			print('YES')
			f = 1
			break
	except:
		continue
if f == 0:
	print('NO')


N = int(input())
L = list(map(int, input().split(' ')))
j = 0
for i in range(0, N):
	R = L[i]
	RR = i
	R1 = L[R - 1]
	if R1 != R or R1 - 1 != i:
		R2 = L[R1 - 1]
		if R2 == RR + 1:
			j = 1
			print('YES')
			break
if j == 0:
	print('NO')


n = int(input())
string = input().split(' ')
answer = 'NO'
for i in range(n):
	current = i
	nextplane = int(string[current]) - 1
	for j in range(2):
		current = nextplane
		nextplane = int(string[current]) - 1
	if nextplane == i:
		answer = 'YES'
		break
print(answer)


def I():
	return int(input())

def IL():
	return [int(i) for i in input().split()]

def IM():
	return map(int, input().split())

def IS():
	return input()
n = I()
l = [0] + IL()
for i in range(1, n + 1):
	if l[l[l[i]]] == i:
		print('YES')
		exit()
print('NO')


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	n = I()
	a = LI_()
	for i in range(n):
		b = a[i]
		c = a[b]
		d = a[c]
		if d == i and len(set([b, c, d])) == 3:
			return 'YES'
	return 'NO'
print(main())


num = int(input())
arr = list(map(int, input().strip().split()))[:num]
ans = 0
arr = [x - 1 for x in arr]
(tem, current) = (0, 0)
for x in range(num):
	start = arr[x]
	tem = arr[arr[x]]
	val = arr[tem]
	if tem == val:
		continue
	elif arr[val] == start:
		ans += 1
if ans > 0:
	print('YES')
else:
	print('NO')


n = input()
a = list(input().split(' '))
a = list((int(x) for x in a))
res = False
for i in range(len(a)):
	a[i] -= 1
for i in range(len(a)):
	if a[a[a[i]]] == i:
		res = True
		break
if res == False:
	print('NO')
else:
	print('YES')


def main():
	n = int(input())
	f = list(map(int, input().split()))
	for i in range(n):
		f[i] -= 1
	for i in range(n):
		now = i
		for j in range(3):
			now = f[now]
		if now == i:
			return 'YES'
	return 'NO'
print(main())


from collections import defaultdict

class Graph:

	def __init__(self, vertices):
		self.graph = defaultdict(list)
		self.V = vertices

	def addEdge(self, u, v):
		self.graph[u].append(v)

	def lovTriUtil(self):
		print(self.graph)

	def lovTri(self):
		for node in range(self.V):
			if self.graph[self.graph[self.graph[node + 1][0]][0]][0] == node + 1:
				return True
n = int(input())
f = [int(k) for k in input().split()]
g = Graph(n)
for i in range(1, n + 1):
	g.addEdge(i, f[i - 1])
if g.lovTri():
	print('YES')
else:
	print('NO')


n = int(input())
c = 0
p = [int(x) for x in input().split()]
for i in range(n):
	if p[p[p[i] - 1] - 1] == i + 1:
		c = 1
		print('YES')
		exit()
print('NO')


n = int(input())
ad = ['x']
a = list(map(int, input().strip().split()))[:n]
ad.extend(a)
c = False
for i in range(1, len(ad)):
	if i < n:
		if ad[ad[ad[i]]] == i and ad[i] != 0:
			c = True
			break
if c == True:
	print('YES')
else:
	print('NO')


def LoveTriangle(n, ar):
	for i in range(1, n + 1):
		a = int(i)
		b = ar[a - 1]
		c = ar[b - 1]
		d = ar[c - 1]
		if a != b and b != c and (c != d) and (d == a):
			return 'YES'
	return 'NO'

def hello():
	n = int(input().strip())
	ar = list(map(int, input().strip().split(' ')))
	result = LoveTriangle(n, ar)
	print(result)
hello()


a = int(input())
b = input().split()
f = 0
for i in range(a):
	n = int(b[i])
	n = int(b[n - 1])
	n = int(b[n - 1])
	if i + 1 == n and int(b[int(b[i]) - 1]) == b.index(str(n)) + 1 and (b.index(b[int(b[i]) - 1]) + 1 == int(b[i])):
		f += 1
		break
if f == 0:
	print('NO')
else:
	print('YES')


N = int(input().strip())
ch = [int(x) for x in input().strip().split(' ')]
for i in range(N):
	if ch[ch[ch[i] - 1] - 1] == i + 1:
		print('YES')
		quit(0)
print('NO')


import sys
n = int(input())
if n == 2:
	print('NO')
	sys.exit()
a = list(map(int, input().split()))
for i in range(1, n + 1):
	a1 = a[i - 1]
	a2 = a[a1 - 1]
	a3 = a[a2 - 1]
	if a1 == a[a3 - 1]:
		print('YES')
		sys.exit()
print('NO')


n = int(input())
love = list((int(i) - 1 for i in input().split()))
res = 'NO'
for i in range(n):
	if love[love[love[i]]] == i:
		res = 'YES'
		break
print(res)


from collections import defaultdict
n = int(input())
arr = list(map(int, input().split()))
d = defaultdict(list)

def f(u, visited, graph, vertex):
	q = [u]
	while len(q):
		t = q[-1]
		for i in graph[t]:
			if visited[i] == 0:
				parent = vertex[vertex[t]]
				if parent == i:
					return 1
			elif visited[i] == -1:
				q.append(i)
				visited[i] = 0
				vertex[i] = t
				break
		else:
			p = q.pop()
			visited[p] = 1
for i in range(1, n + 1):
	d[i].append(arr[i - 1])
visited = [-1] * (n + 1)
vertex = {}
for i in range(n):
	vertex[i + 1] = None
for i in range(1, n + 1):
	if visited[i] == -1:
		if f(i, visited, d, vertex):
			print('YES')
			break
else:
	print('NO')


n = int(input())
arr = list(map(int, input().split()))
for i in range(len(arr)):
	love = arr[i]
	if arr[love - 1] != i + 1 and arr[arr[love - 1] - 1] == i + 1:
		print('YES')
		break
else:
	print('NO')


n = int(input())
arr = list(map(int, input().split()))
ans = 'NO'
for (idx, love) in enumerate(arr):
	if arr[arr[love - 1] - 1] - 1 == idx:
		ans = 'YES'
		break
print(ans)


n = int(input())
a = list(map(int, input().split()))
flag = 0
for i in range(n):
	if flag == 1:
		break
	b = 0
	c = 0
	d = 0
	e = 0
	f = 0
	g = 0
	for k in range(1):
		b = i + 1
		c = a[b - 1]
		d = c - 1
		e = a[d]
		f = e - 1
		g = a[f]
		if g == b and c != e:
			flag = 1
			print('YES')
if flag != 1:
	print('NO')


from math import *
import sys, random

def nextInt():
	return int(input())

def nextStrs():
	return input().split()

def nextInts():
	return list(map(int, nextStrs()))

def can(i, j, k):
	if i + j > k:
		return True
	return False

def main():
	t = 1
	while t > 0:
		n = nextInt()
		arr = [0] + nextInts()
		ok = False
		for i in range(1, n + 1):
			i1 = arr[i]
			i2 = arr[i1]
			i3 = arr[i2]
			if i == i3:
				ok = True
		if ok:
			print('YES')
		else:
			print('NO')
		t -= 1
exit(main())


n = int(input())
arr = input()
l = list(map(int, arr.split(' ')))
x = []
for i in range(0, n):
	k = l[i] - 1
	x.append(l[k])
c = 0
for i in range(0, n):
	a = x[i]
	b = x[a - 1]
	if b == l[i]:
		c = c + 1
		print('YES')
		break
if c == 0:
	print('NO')


import sys
inf = float('inf')
(mod, MOD) = (1000000007, 998244353)

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
n = int(input())
Arr = [0] + get_array()
flag = 0
for i in range(1, n):
	x = i
	y = Arr[i]
	z = Arr[Arr[i]]
	if Arr[z] == x:
		flag = 1
		break
if flag == 1:
	print('YES')
else:
	print('NO')


n = int(input())
a = list(map(int, input().split()))
a = [x - 1 for x in a]
result = 'NO'
for i in range(n):
	if a[i] == a[a[a[a[i]]]] and a[i] != a[a[a[i]]]:
		result = 'YES'
		break
print(result)


n = int(input())
fly = [int(i) for i in input().split()]
ok = False
for i in fly:
	a = i
	x = 0
	while x != 3:
		x += 1
		a = fly[a - 1]
	if a == i:
		ok = True
		print('YES')
		break
if ok == False:
	print('NO')


nop = int(input(''))
planes = input('').rsplit(' ')
length = len(planes)
flag = 1
for i in range(0, length):
	if int(planes[int(planes[int(planes[i]) - 1]) - 1]) - 1 == i:
		print('YES')
		flag = 0
		break
if flag:
	print('NO')


useless = input()
arr = [int(x) for x in input().split()]
for i in range(len(arr)):
	if i + 1 == arr[arr[arr[i] - 1] - 1]:
		print('Yes')
		exit()
print('No')


n = int(input())
a = [int(i) for i in input().split()]
for i in range(n):
	a[i] -= 1
r = 'NO'
for i in range(n):
	x = a[i]
	y = a[x]
	z = a[y]
	if z == i:
		r = 'YES'
print(r)


from sys import exit
n = int(input())
f = list(map(lambda x: int(x) - 1, input().split()))
for i in range(len(f)):
	if f[f[f[i]]] == i and f[f[i]] != i:
		print('YES')
		exit(0)
print('NO')


N = int(input())
F = list(map(lambda v: int(v) - 1, input().split()))
ans = 'NO'
for i in range(0, N):
	triangle = [i, F[i], F[F[i]]]
	if i == F[triangle[2]]:
		ans = 'YES'
		break
print(ans)


from math import ceil, sqrt, log, gcd

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def main():
	n = ii()
	m = {}
	a = li()
	f = 0
	for i in range(n):
		m[i + 1] = a[i]
	for i in range(n):
		x = a[i]
		y = m[x]
		z = m[y]
		if y != i + 1 and z == i + 1:
			f = 1
			break
	if f:
		print('YES')
	else:
		print('NO')
main()


n = int(input())
fi = [int(i) for i in input().split(' ')]
check = 0
for i in range(1, n + 1):
	if fi[i - 1] != i and fi[fi[i - 1] - 1] != i and (fi[fi[i - 1] - 1] != fi[i - 1]) and (fi[fi[fi[i - 1] - 1] - 1] == i):
		check = 1
		break
if check == 1:
	print('Yes')
else:
	print('No')


def findLoveTriangle(start, current, count):
	if start == current and count == 2:
		return True
	elif count > 2:
		return False
	else:
		return findLoveTriangle(start, planes[current] - 1, count + 1)
n = int(input())
planes = list(map(int, input().split()))
loveTriangleExists = False
for i in range(0, n):
	if findLoveTriangle(i, planes[i] - 1, 0) == True:
		loveTriangleExists = True
print('YES' if loveTriangleExists else 'NO')


n = int(input())
l = list(map(int, input().split()))
l.insert(0, 0)
for i in range(1, n + 1):
	se = l[i]
	if se == i:
		continue
	th = l[se]
	if th == se or th == i:
		continue
	fo = l[th]
	if fo == se or fo == th:
		continue
	if i == fo:
		print('YES')
		exit(0)
print('NO')


n = int(input())
arr = [int(x) for x in input().split()]
for i in range(n):
	arr[i] -= 1
s = False
for i in range(n):
	pos = arr[i]
	count = 1
	while count < 3:
		count += 1
		pos = arr[pos]
	if pos == i:
		print('YES')
		s = True
		break
if s == False:
	print('NO')


a = int(input())
f = 0
b = list(map(lambda x: int(x), input().split()))
for i in range(a):
	if b[b[b[i] - 1] - 1] - 1 == i:
		print('YES')
		f = 1
		break
if f == 0:
	print('NO')


b = int(input())
a = list(map(int, input().split()))
a.insert(0, 56)
t = 0
for i in range(1, len(a)):
	if a[a[a[i]]] == i:
		print('YES')
		break
	else:
		t += 1
if t == len(a) - 1:
	print('NO')


n = int(input().strip())
ar = [int(i) for i in input().split()]
for i in range(n):
	t = ar[i]
	if ar[t - 1] != t and ar[ar[t - 1] - 1] != ar[t - 1]:
		if ar[ar[t - 1] - 1] == i + 1:
			print('YES')
			break
else:
	print('NO')


cus = []
n = int(input())
numbers = list(map(int, input().split()))
cnt = 0
for (i, num) in enumerate(numbers):
	if numbers[numbers[numbers[num - 1] - 1] - 1] == num:
		print('YES')
		break
else:
	print('NO')


n = int(input())
l = input().split()
l = list(map(int, l))
k = 0
for x in range(n):
	if l[l[l[x] - 1] - 1] == x + 1:
		print('YES')
		k = 1
		break
if k == 0:
	print('NO')


n = int(input())
l = list(map(int, input().split()))
k = [0]
s = []
for j in l:
	k.append(j)
for i in range(1, n + 1):
	if k[k[k[i]]] == i:
		s.append(1)
	else:
		s.append(0)
if 1 in s:
	print('YES')
else:
	print('NO')


n = int(input())
data = input()
data = data.split()
for i in range(len(data)):
	data[i] = int(data[i])
is_triangle = False
for i in range(len(data)):
	next_1 = data[i] - 1
	next_2 = data[next_1] - 1
	next_3 = data[next_2] - 1
	if i == next_3:
		is_triangle = True
if is_triangle == True:
	print('YES')
else:
	print('NO')


found = False
n = int(input().strip())
f = list(map(int, input().split()))
for i in range(n):
	if f[f[f[i - 1] - 1] - 1] == i:
		found = True
		break
print('NO' if not found else 'YES')