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

P=[0]+list(map(int, input().split()))

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

for _ in range(M):

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

    E[x].append(y)

    E[y].append(x)



ans=[0]*(N+1)

parent=[0]*(N+1)



for root in range(1,N+1):

    if parent[root]: continue

    parent[root]=root

    stack=[root]

    nodes=set()

    values=set()

    while stack:

        node=stack.pop()

        nodes.add(node)

        values.add(P[node])

        for to in E[node]:

            if parent[to]: continue

            parent[to]=node

            stack.append(to)

    for i in nodes&values:

        ans[i]=1



print((sum(ans)))
import sys

def main():

    input = sys.stdin.readline

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

    P = list([int(x)-1 for x in input().split()])

    

    uf = UnionFindTree(N)

    for _ in range(M):

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

        uf.union(x-1,y-1)



    ind = [-1] * N

    for i in range(N):

        ind[P[i]] = i



    ans = 0

    for i in range(N):

        if P[i] == i: ans += 1

        elif uf.same(i, ind[i]): ans += 1

    print(ans)



class UnionFindTree:

    def __init__(self, n):

        self.parent = [-1] * n



    def find(self, x):

        p = self.parent

        while p[x] >= 0: x, p[x] = p[x], p[p[x]]

        return x



    def union(self, x, y):

        x, y, p = self.find(x), self.find(y), self.parent

        if x == y: return

        if p[x] > p[y]: x, y = y, x

        p[x], p[y] = p[x] + p[y], x



    def same(self, x, y): return self.find(x) == self.find(y)

    def size(self, x): return -self.parent[self.find(x)]





if __name__ == '__main__':

    main()
N, M = list(map(int, input().split()))
P = [0] + list(map(int, input().split()))
E = [[] for _ in range(N + 1)]
for _ in range(M):
    x, y = list(map(int, input().split()))
    E[x].append(y)
    E[y].append(x)
ans = [0] * (N + 1)
parent = [0] * (N + 1)
for root in range(1, N + 1):
    if parent[root]:
        continue
    parent[root] = root
    stack = [root]
    nodes = set()
    values = set()
    while stack:
        node = stack.pop()
        nodes.add(node)
        values.add(P[node])
        for to in E[node]:
            if parent[to]:
                continue
            parent[to] = node
            stack.append(to)
    for i in nodes & values:
        ans[i] = 1
print((sum(ans)))

import sys


def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    P = list([int(x) - 1 for x in input().split()])
    uf = UnionFindTree(N)
    for _ in range(M):
        x, y = list(map(int, input().split()))
        uf.union(x - 1, y - 1)
    ind = [-1] * N
    for i in range(N):
        ind[P[i]] = i
    ans = 0
    for i in range(N):
        if P[i] == i:
            ans += 1
        elif uf.same(i, ind[i]):
            ans += 1
    print(ans)


class UnionFindTree:
    def __init__(self, n):
        self.parent = [-1] * n

    def find(self, x):
        p = self.parent
        while p[x] >= 0:
            x, p[x] = p[x], p[p[x]]
        return x

    def union(self, x, y):
        x, y, p = self.find(x), self.find(y), self.parent
        if x == y:
            return
        if p[x] > p[y]:
            x, y = y, x
        p[x], p[y] = p[x] + p[y], x

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return -self.parent[self.find(x)]


if __name__ == "__main__":
    main()

-N, M = list(map(int, input().split())) -P = [0] + list(map(int, input().split())) -E = [[] for _ in range(N + 1)] -for _ in range(M): -    x, y = list(map(int, input().split())) -    E[x].append(y) -    E[y].append(x) -ans = [0] * (N + 1) -parent = [0] * (N + 1) -for root in range(1, N + 1): -    if parent[root]: -        continue -    parent[root] = root -    stack = [root] -    nodes = set() -    values = set() -    while stack: -        node = stack.pop() -        nodes.add(node) -        values.add(P[node]) -        for to in E[node]: -            if parent[to]: -                continue -            parent[to] = node -            stack.append(to) -    for i in nodes & values: -        ans[i] = 1 -print((sum(ans))) +import sys + + +def main(): +    input = sys.stdin.readline +    N, M = list(map(int, input().split())) +    P = list([int(x) - 1 for x in input().split()]) +    uf = UnionFindTree(N) +    for _ in range(M): +        x, y = list(map(int, input().split())) +        uf.union(x - 1, y - 1) +    ind = [-1] * N +    for i in range(N): +        ind[P[i]] = i +    ans = 0 +    for i in range(N): +        if P[i] == i: +            ans += 1 +        elif uf.same(i, ind[i]): +            ans += 1 +    print(ans) + + +class UnionFindTree: +    def __init__(self, n): +        self.parent = [-1] * n + +    def find(self, x): +        p = self.parent +        while p[x] >= 0: +            x, p[x] = p[x], p[p[x]] +        return x + +    def union(self, x, y): +        x, y, p = self.find(x), self.find(y), self.parent +        if x == y: +            return +        if p[x] > p[y]: +            x, y = y, x +        p[x], p[y] = p[x] + p[y], x + +    def same(self, x, y): +        return self.find(x) == self.find(y) + +    def size(self, x): +        return -self.parent[self.find(x)] + + +if __name__ == "__main__": +    main()