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()
import sys

def main():

    input = sys.stdin.readline

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

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

    P=[-1]+P

    uf=UnionFindTree(N+1)

    for _ in range(M):

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

        uf.union(P[x],P[y])

    ans=0

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

        if uf.same(P[i], 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()
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()

import sys


def main():
    input = sys.stdin.readline
    N, M = list(map(int, input().split()))
    (*P,) = list(map(int, input().split()))
    P = [-1] + P
    uf = UnionFindTree(N + 1)
    for _ in range(M):
        x, y = list(map(int, input().split()))
        uf.union(P[x], P[y])
    ans = 0
    for i in range(1, N + 1):
        if uf.same(P[i], 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()

-    P = list([int(x) - 1 for x in input().split()]) -    uf = UnionFindTree(N) +    (*P,) = list(map(int, input().split())) +    P = [-1] + P +    uf = UnionFindTree(N + 1) -        uf.union(x - 1, y - 1) -    ind = [-1] * N -    for i in range(N): -        ind[P[i]] = i +        uf.union(P[x], P[y]) -    for i in range(N): -        if P[i] == i: -            ans += 1 -        elif uf.same(i, ind[i]): +    for i in range(1, N + 1): +        if uf.same(P[i], i):