n = int(eval(input()))

A = list(map(int, input().split()))

A = [a-1 for a in A]

ans = 0

for i in range(n):

    if A[A[i]] == i:

        ans += 1

print((ans//2))

n = int(eval(input()))

A = list(map(int, input().split()))

ans = 0

for i in range(n):

    if A[A[i]-1] == i+1:

        ans += 1

print((ans//2))

n = int(eval(input()))
A = list(map(int, input().split()))
A = [a - 1 for a in A]
ans = 0
for i in range(n):
    if A[A[i]] == i:
        ans += 1
print((ans // 2))

n = int(eval(input()))
A = list(map(int, input().split()))
ans = 0
for i in range(n):
    if A[A[i] - 1] == i + 1:
        ans += 1
print((ans // 2))

-A = [a - 1 for a in A] -    if A[A[i]] == i: +    if A[A[i] - 1] == i + 1: