#各pは、自分の今いるインデックスがindex p-1と繋がっていればそこに移動する

import sys

sys.setrecursionlimit(200000)



class UnionFind():

    def __init__(self,n):

        self.n=n

        self.parents = [i for i in range(n+1)]

        self.size = [1]*(n+1)

    def find(self,x):

        if self.parents[x]==x:

            return x

        else:

            self.parents[x]=self.find(self.parents[x])

            return self.parents[x]

    def unite(self,x,y):

        xRoot = self.find(x)

        yRoot = self.find(y)

        if xRoot == yRoot:

            return

        if self.size[xRoot]>self.size[yRoot]:

            self.parents[yRoot] = xRoot

        else:

            self.parents[xRoot] = yRoot

            if self.size[xRoot] == self.size[yRoot]:

                self.size[yRoot]+=1

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

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

tree = UnionFind(N)

for _ in range(M):

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

    tree.unite(x,y)



count = 0

for i in range(N):

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

        count += 1



print(count)
"""

初期状態でpiにいる数が、pi番目にたどり着けるかを判定すればよい。

unionFind木が使える

"""

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

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

class UnionFind():

    def __init__(self,n):

        self.n=n

        self.parents = [i for i in range(n+1)]

        self.size = [1]*(n+1)

    def find(self,x):

        if self.parents[x]==x:

            return x

        else:

            self.parents[x]=self.find(self.parents[x])

            return self.parents[x]

    def unite(self,x,y):

        xRoot = self.find(x)

        yRoot = self.find(y)

        if xRoot == yRoot:

            return

        if self.size[xRoot]>self.size[yRoot]:

            self.parents[yRoot] = xRoot

            self.size[xRoot] += self.size[yRoot]

            self.size[yRoot] = 0

        else:

            self.parents[xRoot] = yRoot

            self.size[yRoot] += self.size[xRoot]

            self.size[xRoot] = 0

tree = UnionFind(N)

for _ in range(M):

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

    tree.unite(x,y)

cnt = 0

for i in range(N):

    p = P[i]

    if tree.find(i+1)==tree.find(p):

        cnt += 1



print(cnt)
# 各pは、自分の今いるインデックスがindex p-1と繋がっていればそこに移動する
import sys

sys.setrecursionlimit(200000)


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [i for i in range(n + 1)]
        self.size = [1] * (n + 1)

    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def unite(self, x, y):
        xRoot = self.find(x)
        yRoot = self.find(y)
        if xRoot == yRoot:
            return
        if self.size[xRoot] > self.size[yRoot]:
            self.parents[yRoot] = xRoot
        else:
            self.parents[xRoot] = yRoot
            if self.size[xRoot] == self.size[yRoot]:
                self.size[yRoot] += 1


N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
tree = UnionFind(N)
for _ in range(M):
    x, y = list(map(int, input().split()))
    tree.unite(x, y)
count = 0
for i in range(N):
    if tree.find(i + 1) == tree.find(P[i]):
        count += 1
print(count)

"""
初期状態でpiにいる数が、pi番目にたどり着けるかを判定すればよい。
unionFind木が使える
"""
N, M = list(map(int, input().split()))
P = list(map(int, input().split()))


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [i for i in range(n + 1)]
        self.size = [1] * (n + 1)

    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def unite(self, x, y):
        xRoot = self.find(x)
        yRoot = self.find(y)
        if xRoot == yRoot:
            return
        if self.size[xRoot] > self.size[yRoot]:
            self.parents[yRoot] = xRoot
            self.size[xRoot] += self.size[yRoot]
            self.size[yRoot] = 0
        else:
            self.parents[xRoot] = yRoot
            self.size[yRoot] += self.size[xRoot]
            self.size[xRoot] = 0


tree = UnionFind(N)
for _ in range(M):
    x, y = list(map(int, input().split()))
    tree.unite(x, y)
cnt = 0
for i in range(N):
    p = P[i]
    if tree.find(i + 1) == tree.find(p):
        cnt += 1
print(cnt)

-# 各pは、自分の今いるインデックスがindex p-1と繋がっていればそこに移動する -import sys - -sys.setrecursionlimit(200000) +""" +初期状態でpiにいる数が、pi番目にたどり着けるかを判定すればよい。 +unionFind木が使える +""" +N, M = list(map(int, input().split())) +P = list(map(int, input().split())) +            self.size[xRoot] += self.size[yRoot] +            self.size[yRoot] = 0 -            if self.size[xRoot] == self.size[yRoot]: -                self.size[yRoot] += 1 +            self.size[yRoot] += self.size[xRoot] +            self.size[xRoot] = 0 -N, M = list(map(int, input().split())) -P = list(map(int, input().split())) -count = 0 +cnt = 0 -    if tree.find(i + 1) == tree.find(P[i]): -        count += 1 -print(count) +    p = P[i] +    if tree.find(i + 1) == tree.find(p): +        cnt += 1 +print(cnt)