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

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

P = list(enumerate(P, start=1))





class UnionFind:

    def __init__(self, n):

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

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

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



    # 検索

    def find(self, x):

        if self.par[x] == x:

            return x

        else:

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

            return self.par[x]



    # 併合

    def union(self, x, y):

        x = self.find(x)

        y = self.find(y)



        if x == y:

            return



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

            self.par[x] = y

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

            self.size[x] = 0

        else:

            self.par[y] = x

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

            self.size[y] = 0

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

                self.rank[x] += 1



    # 同じ集合に属するか判定

    def same_check(self, x, y):

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





uf = UnionFind(N)

for m in range(M):

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

    uf.union(x, y)



ans = 0

for now, value in P:

    if uf.same_check(now, value):

        ans += 1

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

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





class UnionFind:

    def __init__(self, n):

        self.par = [i for i in range(n)]

        self.rank = [0] * n

        self.size = [1] * n



    # 検索

    def find(self, x):

        if self.par[x] == x:

            return x

        else:

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

            return self.par[x]



    # 併合

    def union(self, x, y):

        x = self.find(x)

        y = self.find(y)



        if x == y:

            return



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

            self.par[x] = y

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

            self.size[x] = 0

        else:

            self.par[y] = x

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

            self.size[y] = 0

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

                self.rank[x] += 1



    # 同じ集合に属するか判定

    def same(self, x, y):

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



    # すべての頂点に対して親を検索する

    def all_find(self):

        for n in range(len(self.par)):

            self.find(n)





UF = UnionFind(N)

for i in range(M):

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

    x, y = x-1, y-1

    UF.union(x, y)





UF.all_find()



ans = 0

for index, number in P:

    ans += UF.same(index, number - 1)



print(ans)

N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
P = list(enumerate(P, start=1))


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

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
            self.size[x] = 0
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            self.size[y] = 0
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


uf = UnionFind(N)
for m in range(M):
    x, y = list(map(int, input().split()))
    uf.union(x, y)
ans = 0
for now, value in P:
    if uf.same_check(now, value):
        ans += 1
print(ans)

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


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

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
            self.size[x] = 0
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            self.size[y] = 0
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same(self, x, y):
        return self.find(x) == self.find(y)

    # すべての頂点に対して親を検索する
    def all_find(self):
        for n in range(len(self.par)):
            self.find(n)


UF = UnionFind(N)
for i in range(M):
    x, y = list(map(int, input().split()))
    x, y = x - 1, y - 1
    UF.union(x, y)
UF.all_find()
ans = 0
for index, number in P:
    ans += UF.same(index, number - 1)
print(ans)

-P = list(map(int, input().split())) -P = list(enumerate(P, start=1)) +P = list(enumerate(map(int, input().split()))) -        self.par = [i for i in range(n + 1)] -        self.rank = [0] * (n + 1) -        self.size = [1] * (n + 1) +        self.par = [i for i in range(n)] +        self.rank = [0] * n +        self.size = [1] * n -    def same_check(self, x, y): +    def same(self, x, y): +    # すべての頂点に対して親を検索する +    def all_find(self): +        for n in range(len(self.par)): +            self.find(n) -uf = UnionFind(N) -for m in range(M): + +UF = UnionFind(N) +for i in range(M): -    uf.union(x, y) +    x, y = x - 1, y - 1 +    UF.union(x, y) +UF.all_find() -for now, value in P: -    if uf.same_check(now, value): -        ans += 1 +for index, number in P: +    ans += UF.same(index, number - 1)