class UF_tree:

    def __init__(self, n):

        self.root = [-1] * (n + 1)  # -1ならそのノードが根,で絶対値が木の要素数

        self.rank = [0] * (n + 1)



    def find(self, x):  # xの根となる要素番号を返す

        if self.root[x] < 0:

            return x

        else:

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

            return self.root[x]



    def isSame(self, x, y):

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



    def unite(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if x == y:

            return

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

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

            self.root[x] = y

        else:

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

            self.root[y] = x

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

                self.rank[x] += 1



    def getNodeLen(self, x):

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





if __name__ == "__main__":

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

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

    uf = UF_tree(2*N)

    for i, p in enumerate(P, start=1):

        uf.unite(i, p + N)



    for _ in range(M):

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

        uf.unite(x + N, y + N)



    ans = 0

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

        if uf.isSame(i, i + N):

            ans += 1

    print(ans)

import sys

input = sys.stdin.buffer.readline

sys.setrecursionlimit(10 ** 7)





class UF_tree:

    def __init__(self, n):

        self.root = [-1] * (n + 1)  # -1ならそのノードが根,で絶対値が木の要素数

        self.rank = [0] * (n + 1)



    def find(self, x):  # xの根となる要素番号を返す

        if self.root[x] < 0:

            return x

        else:

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

            return self.root[x]



    def isSame(self, x, y):

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



    def unite(self, x, y):

        x = self.find(x)

        y = self.find(y)

        if x == y:

            return

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

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

            self.root[x] = y

        else:

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

            self.root[y] = x

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

                self.rank[x] += 1



    def getNodeLen(self, x):

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





if __name__ == "__main__":

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

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

    uf = UF_tree(N + 10)

    for _ in range(M):

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

        uf.unite(x, y)



    ans = sum(uf.isSame(i, p) for i, p in enumerate(P, start=1))

    print(ans)
class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)  # -1ならそのノードが根,で絶対値が木の要素数
        self.rank = [0] * (n + 1)

    def find(self, x):  # xの根となる要素番号を返す
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def getNodeLen(self, x):
        return -self.root[self.find(x)]


if __name__ == "__main__":
    N, M = list(map(int, input().split()))
    P = list(map(int, input().split()))
    uf = UF_tree(2 * N)
    for i, p in enumerate(P, start=1):
        uf.unite(i, p + N)
    for _ in range(M):
        x, y = list(map(int, input().split()))
        uf.unite(x + N, y + N)
    ans = 0
    for i in range(1, N + 1):
        if uf.isSame(i, i + N):
            ans += 1
    print(ans)

import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)


class UF_tree:
    def __init__(self, n):
        self.root = [-1] * (n + 1)  # -1ならそのノードが根,で絶対値が木の要素数
        self.rank = [0] * (n + 1)

    def find(self, x):  # xの根となる要素番号を返す
        if self.root[x] < 0:
            return x
        else:
            self.root[x] = self.find(self.root[x])
            return self.root[x]

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] < self.rank[y]:
            self.root[y] += self.root[x]
            self.root[x] = y
        else:
            self.root[x] += self.root[y]
            self.root[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def getNodeLen(self, x):
        return -self.root[self.find(x)]


if __name__ == "__main__":
    N, M = list(map(int, input().split()))
    P = list(map(int, input().split()))
    uf = UF_tree(N + 10)
    for _ in range(M):
        x, y = list(map(int, input().split()))
        uf.unite(x, y)
    ans = sum(uf.isSame(i, p) for i, p in enumerate(P, start=1))
    print(ans)

+import sys + +input = sys.stdin.buffer.readline +sys.setrecursionlimit(10**7) + + -    uf = UF_tree(2 * N) -    for i, p in enumerate(P, start=1): -        uf.unite(i, p + N) +    uf = UF_tree(N + 10) -        uf.unite(x + N, y + N) -    ans = 0 -    for i in range(1, N + 1): -        if uf.isSame(i, i + N): -            ans += 1 +        uf.unite(x, y) +    ans = sum(uf.isSame(i, p) for i, p in enumerate(P, start=1))