#Union Findで連結を判定

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

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

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



class UnionFind:

    def __init__(self,N):

        self.Parent = [-1]*N

    def get_Parent(self,n):

        if self.Parent[n] == -1:return n

        p = self.get_Parent(self.Parent[n])

        self.Parent[n] = p

        return p

    def merge(self,x,y):

        x = self.get_Parent(x)

        y = self.get_Parent(y)

        if x!=y: self.Parent[y] = x

        return

    def is_united(self,x,y):

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



U = UnionFind(N)

for x,y in XY:

    x-=1

    y-=1

    U.merge(x,y)

print((sum(U.is_united(i,p-1) for i,p in enumerate(P))))
class UnionFind():

    def __init__(self,n):

        self.n=n

        self.root=[-1]*n

        self.rank=[-1]*n



    def Find_Root(self,x):

        if self.root[x]<0:return x

        else:

            self.root[x]=self.Find_Root(self.root[x])

            return self.root[x]



    def Unite(self,x,y):

        x=self.Find_Root(x)

        y=self.Find_Root(y)



        if x==y:return

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

            self.root[x] +=self.root[y]

            self.root[y]=x

        else:

            self.root[y] +=self.root[x]

            self.root[x]=y

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

                self.rank[y] +=1



    def isSameGroup(self,x,y):

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



    def Count(self,x):

        return -self.root[self.Find_Root(x)]



    def members(self,x):

        root=self.Find_Root(x)

        return [i for i in range(self.n) if self.Find_Root(i)==root]



    def address(self):

        return[i for i,j in enumerate(self.root) if j<0]



    def group_members(self):

        return {i: self.members(i) for i in self.address()}



    def size(self,x):

        return -self.root[self.Find_Root(x)]





n,m=list(map(int,input().split()))

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

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

UnionFind=UnionFind(n)



for x,y in XY:

    UnionFind.Unite(x-1,y-1)



print((sum([UnionFind.isSameGroup(i,j-1) for i,j in enumerate(P)])))
# Union Findで連結を判定
N, M = list(map(int, input().split()))
(*P,) = list(map(int, input().split()))
XY = [list(map(int, input().split())) for _ in range(M)]


class UnionFind:
    def __init__(self, N):
        self.Parent = [-1] * N

    def get_Parent(self, n):
        if self.Parent[n] == -1:
            return n
        p = self.get_Parent(self.Parent[n])
        self.Parent[n] = p
        return p

    def merge(self, x, y):
        x = self.get_Parent(x)
        y = self.get_Parent(y)
        if x != y:
            self.Parent[y] = x
        return

    def is_united(self, x, y):
        return self.get_Parent(x) == self.get_Parent(y)


U = UnionFind(N)
for x, y in XY:
    x -= 1
    y -= 1
    U.merge(x, y)
print((sum(U.is_united(i, p - 1) for i, p in enumerate(P))))

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.root = [-1] * n
        self.rank = [-1] * n

    def Find_Root(self, x):
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]

    def Unite(self, x, y):
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if x == y:
            return
        elif self.rank[x] > self.rank[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    def Count(self, x):
        return -self.root[self.Find_Root(x)]

    def members(self, x):
        root = self.Find_Root(x)
        return [i for i in range(self.n) if self.Find_Root(i) == root]

    def address(self):
        return [i for i, j in enumerate(self.root) if j < 0]

    def group_members(self):
        return {i: self.members(i) for i in self.address()}

    def size(self, x):
        return -self.root[self.Find_Root(x)]


n, m = list(map(int, input().split()))
P = list(map(int, input().split()))
XY = [list(map(int, input().split())) for i in range(m)]
UnionFind = UnionFind(n)
for x, y in XY:
    UnionFind.Unite(x - 1, y - 1)
print((sum([UnionFind.isSameGroup(i, j - 1) for i, j in enumerate(P)])))

-# Union Findで連結を判定 -N, M = list(map(int, input().split())) -(*P,) = list(map(int, input().split())) -XY = [list(map(int, input().split())) for _ in range(M)] +class UnionFind: +    def __init__(self, n): +        self.n = n +        self.root = [-1] * n +        self.rank = [-1] * n + +    def Find_Root(self, x): +        if self.root[x] < 0: +            return x +        else: +            self.root[x] = self.Find_Root(self.root[x]) +            return self.root[x] + +    def Unite(self, x, y): +        x = self.Find_Root(x) +        y = self.Find_Root(y) +        if x == y: +            return +        elif self.rank[x] > self.rank[y]: +            self.root[x] += self.root[y] +            self.root[y] = x +        else: +            self.root[y] += self.root[x] +            self.root[x] = y +            if self.rank[x] == self.rank[y]: +                self.rank[y] += 1 + +    def isSameGroup(self, x, y): +        return self.Find_Root(x) == self.Find_Root(y) + +    def Count(self, x): +        return -self.root[self.Find_Root(x)] + +    def members(self, x): +        root = self.Find_Root(x) +        return [i for i in range(self.n) if self.Find_Root(i) == root] + +    def address(self): +        return [i for i, j in enumerate(self.root) if j < 0] + +    def group_members(self): +        return {i: self.members(i) for i in self.address()} + +    def size(self, x): +        return -self.root[self.Find_Root(x)] -class UnionFind: -    def __init__(self, N): -        self.Parent = [-1] * N - -    def get_Parent(self, n): -        if self.Parent[n] == -1: -            return n -        p = self.get_Parent(self.Parent[n]) -        self.Parent[n] = p -        return p - -    def merge(self, x, y): -        x = self.get_Parent(x) -        y = self.get_Parent(y) -        if x != y: -            self.Parent[y] = x -        return - -    def is_united(self, x, y): -        return self.get_Parent(x) == self.get_Parent(y) - - -U = UnionFind(N) +n, m = list(map(int, input().split())) +P = list(map(int, input().split())) +XY = [list(map(int, input().split())) for i in range(m)] +UnionFind = UnionFind(n) -    x -= 1 -    y -= 1 -    U.merge(x, y) -print((sum(U.is_united(i, p - 1) for i, p in enumerate(P)))) +    UnionFind.Unite(x - 1, y - 1) +print((sum([UnionFind.isSameGroup(i, j - 1) for i, j in enumerate(P)])))