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

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



Group=[i for i in range(N+1)]





Swap=[None]*M

for i in range(M):

    Swap[i]=sorted(map(int,input().split()))



def find(x):

    while Group[x] != x:

        x=Group[x]

    return x



for i in range(M):

    if find(Swap[i][1]) != find(Swap[i][0]):

        Group[find(Swap[i][1])]=min(find(Swap[i][1]),find(Swap[i][0]))

        Group[find(Swap[i][0])]=min(find(Swap[i][1]),find(Swap[i][0]))

        Group[Swap[i][1]]=min(find(Swap[i][1]),find(Swap[i][0]))

        Group[Swap[i][1]]=min(find(Swap[i][1]),find(Swap[i][0]))



count=0



for i in range(N):

    if find(P[i])==find(i+1):

        count+=1





print(count)

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

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

S = [list(map(int,input().split())) for i in range(M)]



class Union_find():

    def __init__(self, N):

        self.Group = [i for i in range(N)]

        

    def find(self,x):# find(a)=find(b)のとき同じグループ

        while self.Group[x] != x:

            x = self.Group[x]

        return x



    def Union(self,x,y):  # xとyが同じグループになるよう更新

        if self.find(x) != self.find(y):

            self.Group[self.find(y)] = self.Group[self.find(x)] = min(self.find(y),self.find(x))



A = Union_find(N)



for x,y in S:

    A.Union(x-1,y-1)



ANS = 0



for i in range(N):

    P[i]-=1



for i in range(N):

    if A.find(i) == A.find (P[i]):

        ANS += 1



print(ANS)
N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
Group = [i for i in range(N + 1)]
Swap = [None] * M
for i in range(M):
    Swap[i] = sorted(map(int, input().split()))


def find(x):
    while Group[x] != x:
        x = Group[x]
    return x


for i in range(M):
    if find(Swap[i][1]) != find(Swap[i][0]):
        Group[find(Swap[i][1])] = min(find(Swap[i][1]), find(Swap[i][0]))
        Group[find(Swap[i][0])] = min(find(Swap[i][1]), find(Swap[i][0]))
        Group[Swap[i][1]] = min(find(Swap[i][1]), find(Swap[i][0]))
        Group[Swap[i][1]] = min(find(Swap[i][1]), find(Swap[i][0]))
count = 0
for i in range(N):
    if find(P[i]) == find(i + 1):
        count += 1
print(count)

N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(M)]


class Union_find:
    def __init__(self, N):
        self.Group = [i for i in range(N)]

    def find(self, x):  # find(a)=find(b)のとき同じグループ
        while self.Group[x] != x:
            x = self.Group[x]
        return x

    def Union(self, x, y):  # xとyが同じグループになるよう更新
        if self.find(x) != self.find(y):
            self.Group[self.find(y)] = self.Group[self.find(x)] = min(
                self.find(y), self.find(x)
            )


A = Union_find(N)
for x, y in S:
    A.Union(x - 1, y - 1)
ANS = 0
for i in range(N):
    P[i] -= 1
for i in range(N):
    if A.find(i) == A.find(P[i]):
        ANS += 1
print(ANS)

-Group = [i for i in range(N + 1)] -Swap = [None] * M -for i in range(M): -    Swap[i] = sorted(map(int, input().split())) +S = [list(map(int, input().split())) for i in range(M)] -def find(x): -    while Group[x] != x: -        x = Group[x] -    return x +class Union_find: +    def __init__(self, N): +        self.Group = [i for i in range(N)] + +    def find(self, x):  # find(a)=find(b)のとき同じグループ +        while self.Group[x] != x: +            x = self.Group[x] +        return x + +    def Union(self, x, y):  # xとyが同じグループになるよう更新 +        if self.find(x) != self.find(y): +            self.Group[self.find(y)] = self.Group[self.find(x)] = min( +                self.find(y), self.find(x) +            ) -for i in range(M): -    if find(Swap[i][1]) != find(Swap[i][0]): -        Group[find(Swap[i][1])] = min(find(Swap[i][1]), find(Swap[i][0])) -        Group[find(Swap[i][0])] = min(find(Swap[i][1]), find(Swap[i][0])) -        Group[Swap[i][1]] = min(find(Swap[i][1]), find(Swap[i][0])) -        Group[Swap[i][1]] = min(find(Swap[i][1]), find(Swap[i][0])) -count = 0 +A = Union_find(N) +for x, y in S: +    A.Union(x - 1, y - 1) +ANS = 0 -    if find(P[i]) == find(i + 1): -        count += 1 -print(count) +    P[i] -= 1 +for i in range(N): +    if A.find(i) == A.find(P[i]): +        ANS += 1 +print(ANS)