N, M = list(map(int, input().split()))

p = [int(i) for i in input().split()]

graph = [[] for _ in range(N+1)]

group = [0]*(N+1)

ans = 0

for _ in range(M):

    x, y = (int(i) for i in input().split())

    graph[x].append(y)

    graph[y].append(x)

for i, num in enumerate(p, 1):

    space = [i]

    for point in space:

        if group[point]:

            continue

        group[point] = i

        space.extend(graph[point])

    ans += (group[num] == group[i])

print(ans)
N, M = list(map(int, input().split()))

p = [int(i) for i in input().split()]

graph = [[] for _ in range(N+1)]

group = [0]*(N+1)

ans = 0

for _ in range(M):

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

    graph[x].append(y)

    graph[y].append(x)

for i, num in enumerate(p, 1):

    space = [i]

    for point in space:

        if not group[point]:

            group[point] = i

            space.extend(graph[point])

    ans += (group[num] == group[i])

print(ans)
N, M = list(map(int, input().split()))
p = [int(i) for i in input().split()]
graph = [[] for _ in range(N + 1)]
group = [0] * (N + 1)
ans = 0
for _ in range(M):
    x, y = (int(i) for i in input().split())
    graph[x].append(y)
    graph[y].append(x)
for i, num in enumerate(p, 1):
    space = [i]
    for point in space:
        if group[point]:
            continue
        group[point] = i
        space.extend(graph[point])
    ans += group[num] == group[i]
print(ans)

N, M = list(map(int, input().split()))
p = [int(i) for i in input().split()]
graph = [[] for _ in range(N + 1)]
group = [0] * (N + 1)
ans = 0
for _ in range(M):
    x, y = list(map(int, input().split()))
    graph[x].append(y)
    graph[y].append(x)
for i, num in enumerate(p, 1):
    space = [i]
    for point in space:
        if not group[point]:
            group[point] = i
            space.extend(graph[point])
    ans += group[num] == group[i]
print(ans)

-    x, y = (int(i) for i in input().split()) +    x, y = list(map(int, input().split())) -        if group[point]: -            continue -        group[point] = i -        space.extend(graph[point]) +        if not group[point]: +            group[point] = i +            space.extend(graph[point])