from collections import deque



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

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

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



G = [[] for i in range(N)]



for i in range(len(L)) :

    u,v = L[i]

    u -= 1

    v -= 1

    

    G[u].append(v)

    G[v].append(u)



explored = [0 for i in range(N)]



ans = 0



for i in range(N) :

    

    if explored[i] :

        continue

    

    explored[i] = 1

    q = deque([i])

    

    A = []

    B = []

    while q :

        now = q.popleft()

        

        A.append(now)

        B.append(P[now]-1)

        

        for to in G[now] :

            if explored[to] :

                continue

            

            q.append(to)

            explored[to] = 1

    

    C = set(A) & set(B)

    ans += len(C)

    



print(ans)

  
class UnionFind:

    def __init__(self, num):

        self.rank = [0] * num

        self.par = [i for i in range(num)]

        self.n = num



    def find_root(self, node):

        if self.par[node] == node:

            return node

        else:

            self.par[node] = self.find_root(self.par[node])

            return self.par[node]



    def same_root(self, x, y):

        return self.find_root(x) == self.find_root(y)



    def union(self, x, y):

        x = self.find_root(x)

        y = self.find_root(y)

        if x == y:

            return

        if self.rank[x] > self.rank[y]:

            self.par[y] = x

        else:

            self.par[x] = y

            if self.rank[x] == self.rank[y]:

                self.rank[y] += 1





def main():

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

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

    UF = UnionFind(N)

    

    for i in range(M) :

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

        A -= 1

        B -= 1

        UF.union(A, B)

    

    ans = 0

    

    for i, v in enumerate(L) :

        ans += (UF.same_root(i, v-1))

    

    print(ans)

    

        



import sys

input = sys.stdin.readline



if __name__ == "__main__":

    main()

from collections import deque

N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
L = [list(map(int, input().split())) for i in range(M)]
G = [[] for i in range(N)]
for i in range(len(L)):
    u, v = L[i]
    u -= 1
    v -= 1
    G[u].append(v)
    G[v].append(u)
explored = [0 for i in range(N)]
ans = 0
for i in range(N):
    if explored[i]:
        continue
    explored[i] = 1
    q = deque([i])
    A = []
    B = []
    while q:
        now = q.popleft()
        A.append(now)
        B.append(P[now] - 1)
        for to in G[now]:
            if explored[to]:
                continue
            q.append(to)
            explored[to] = 1
    C = set(A) & set(B)
    ans += len(C)
print(ans)

class UnionFind:
    def __init__(self, num):
        self.rank = [0] * num
        self.par = [i for i in range(num)]
        self.n = num

    def find_root(self, node):
        if self.par[node] == node:
            return node
        else:
            self.par[node] = self.find_root(self.par[node])
            return self.par[node]

    def same_root(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def union(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        if self.rank[x] > self.rank[y]:
            self.par[y] = x
        else:
            self.par[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1


def main():
    N, M = list(map(int, input().split()))
    L = list(map(int, input().split()))
    UF = UnionFind(N)
    for i in range(M):
        A, B = list(map(int, input().split()))
        A -= 1
        B -= 1
        UF.union(A, B)
    ans = 0
    for i, v in enumerate(L):
        ans += UF.same_root(i, v - 1)
    print(ans)


import sys

input = sys.stdin.readline
if __name__ == "__main__":
    main()

-from collections import deque +class UnionFind: +    def __init__(self, num): +        self.rank = [0] * num +        self.par = [i for i in range(num)] +        self.n = num -N, M = list(map(int, input().split())) -P = list(map(int, input().split())) -L = [list(map(int, input().split())) for i in range(M)] -G = [[] for i in range(N)] -for i in range(len(L)): -    u, v = L[i] -    u -= 1 -    v -= 1 -    G[u].append(v) -    G[v].append(u) -explored = [0 for i in range(N)] -ans = 0 -for i in range(N): -    if explored[i]: -        continue -    explored[i] = 1 -    q = deque([i]) -    A = [] -    B = [] -    while q: -        now = q.popleft() -        A.append(now) -        B.append(P[now] - 1) -        for to in G[now]: -            if explored[to]: -                continue -            q.append(to) -            explored[to] = 1 -    C = set(A) & set(B) -    ans += len(C) -print(ans) +    def find_root(self, node): +        if self.par[node] == node: +            return node +        else: +            self.par[node] = self.find_root(self.par[node]) +            return self.par[node] + +    def same_root(self, x, y): +        return self.find_root(x) == self.find_root(y) + +    def union(self, x, y): +        x = self.find_root(x) +        y = self.find_root(y) +        if x == y: +            return +        if self.rank[x] > self.rank[y]: +            self.par[y] = x +        else: +            self.par[x] = y +            if self.rank[x] == self.rank[y]: +                self.rank[y] += 1 + + +def main(): +    N, M = list(map(int, input().split())) +    L = list(map(int, input().split())) +    UF = UnionFind(N) +    for i in range(M): +        A, B = list(map(int, input().split())) +        A -= 1 +        B -= 1 +        UF.union(A, B) +    ans = 0 +    for i, v in enumerate(L): +        ans += UF.same_root(i, v - 1) +    print(ans) + + +import sys + +input = sys.stdin.readline +if __name__ == "__main__": +    main()