import sys

sys.setrecursionlimit(2000000)

input = sys.stdin.readline

class UnionFind():

    def __init__(self,n):

        self.n=n

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

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



    def find(self,x):

        if self.parents[x]==x:

            return x

        else:

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

            return self.parents[x]



    def union(self,x,y):

        xRoot = self.find(x)

        yRoot = self.find(y)

        if xRoot == yRoot:

            return

        if self.size[xRoot]>self.size[yRoot]:

            self.parents[yRoot] = xRoot

        else:

            self.parents[xRoot] = yRoot

            if self.size[xRoot] == self.size[yRoot]:

                self.size[yRoot]+=1



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

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

dic = {}

dic2 = {}

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

    dic[P[i-1]]=i

    dic2[i]=P[i-1]



tree = UnionFind(N)

for _ in range(M):

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

    tree.union(x,y)



indexGroup = {}

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

    root = tree.find(i)

    if root not in indexGroup:

        indexGroup[root]=set()

    indexGroup[root].add(i)



for p,i in list(dic.items()):

    root = tree.find(i)

    if p in indexGroup[root]:

        n = dic2[p]

        dic[p] = p

        dic2[p] = p

        dic[n] = i

        dic2[i] = n



count = 0

for p,i in list(dic.items()):

    if p==i:

        count += 1

print(count)



import sys

sys.setrecursionlimit(2000000)

input = sys.stdin.readline

class UnionFind():

    def __init__(self,n):

        self.n=n

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

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



    def find(self,x):

        if self.parents[x]==x:

            return x

        else:

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

            return self.parents[x]



    def union(self,x,y):

        xRoot = self.find(x)

        yRoot = self.find(y)

        if xRoot == yRoot:

            return

        if self.size[xRoot]>self.size[yRoot]:

            self.parents[yRoot] = xRoot

        else:

            self.parents[xRoot] = yRoot

            if self.size[xRoot] == self.size[yRoot]:

                self.size[yRoot]+=1



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

tree = UnionFind(N)

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

for _ in range(M):

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

    tree.union(x,y)



indeces = {}

indeces2 = {}

for i in range(N):

    indeces[P[i]]=i+1

    indeces2[i+1]=P[i]



group = {}

group2 = {}

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

    r = tree.find(i)

    if r not in group:

        group[r]=set()

    group[r].add(i)

    group2[i]=r



ans = 0

for n,index in list(indeces.items()):

    if n == index:

        ans += 1

    else:

        groupNum = group2[index]

        if n in group[groupNum]:

            ans += 1

            a = indeces2[n] #n番目に現在君臨している数字

            indeces2[n] = n

            indeces2[index] = a

            indeces[a] = index

            indeces[n] = n



print(ans)





import sys

sys.setrecursionlimit(2000000)
input = sys.stdin.readline


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

    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        xRoot = self.find(x)
        yRoot = self.find(y)
        if xRoot == yRoot:
            return
        if self.size[xRoot] > self.size[yRoot]:
            self.parents[yRoot] = xRoot
        else:
            self.parents[xRoot] = yRoot
            if self.size[xRoot] == self.size[yRoot]:
                self.size[yRoot] += 1


N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
dic = {}
dic2 = {}
for i in range(1, N + 1):
    dic[P[i - 1]] = i
    dic2[i] = P[i - 1]
tree = UnionFind(N)
for _ in range(M):
    x, y = list(map(int, input().split()))
    tree.union(x, y)
indexGroup = {}
for i in range(1, N + 1):
    root = tree.find(i)
    if root not in indexGroup:
        indexGroup[root] = set()
    indexGroup[root].add(i)
for p, i in list(dic.items()):
    root = tree.find(i)
    if p in indexGroup[root]:
        n = dic2[p]
        dic[p] = p
        dic2[p] = p
        dic[n] = i
        dic2[i] = n
count = 0
for p, i in list(dic.items()):
    if p == i:
        count += 1
print(count)

import sys

sys.setrecursionlimit(2000000)
input = sys.stdin.readline


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

    def find(self, x):
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        xRoot = self.find(x)
        yRoot = self.find(y)
        if xRoot == yRoot:
            return
        if self.size[xRoot] > self.size[yRoot]:
            self.parents[yRoot] = xRoot
        else:
            self.parents[xRoot] = yRoot
            if self.size[xRoot] == self.size[yRoot]:
                self.size[yRoot] += 1


N, M = list(map(int, input().split()))
tree = UnionFind(N)
P = list(map(int, input().split()))
for _ in range(M):
    x, y = list(map(int, input().split()))
    tree.union(x, y)
indeces = {}
indeces2 = {}
for i in range(N):
    indeces[P[i]] = i + 1
    indeces2[i + 1] = P[i]
group = {}
group2 = {}
for i in range(1, N + 1):
    r = tree.find(i)
    if r not in group:
        group[r] = set()
    group[r].add(i)
    group2[i] = r
ans = 0
for n, index in list(indeces.items()):
    if n == index:
        ans += 1
    else:
        groupNum = group2[index]
        if n in group[groupNum]:
            ans += 1
            a = indeces2[n]  # n番目に現在君臨している数字
            indeces2[n] = n
            indeces2[index] = a
            indeces[a] = index
            indeces[n] = n
print(ans)

+tree = UnionFind(N) -dic = {} -dic2 = {} -for i in range(1, N + 1): -    dic[P[i - 1]] = i -    dic2[i] = P[i - 1] -tree = UnionFind(N) -indexGroup = {} +indeces = {} +indeces2 = {} +for i in range(N): +    indeces[P[i]] = i + 1 +    indeces2[i + 1] = P[i] +group = {} +group2 = {} -    root = tree.find(i) -    if root not in indexGroup: -        indexGroup[root] = set() -    indexGroup[root].add(i) -for p, i in list(dic.items()): -    root = tree.find(i) -    if p in indexGroup[root]: -        n = dic2[p] -        dic[p] = p -        dic2[p] = p -        dic[n] = i -        dic2[i] = n -count = 0 -for p, i in list(dic.items()): -    if p == i: -        count += 1 -print(count) +    r = tree.find(i) +    if r not in group: +        group[r] = set() +    group[r].add(i) +    group2[i] = r +ans = 0 +for n, index in list(indeces.items()): +    if n == index: +        ans += 1 +    else: +        groupNum = group2[index] +        if n in group[groupNum]: +            ans += 1 +            a = indeces2[n]  # n番目に現在君臨している数字 +            indeces2[n] = n +            indeces2[index] = a +            indeces[a] = index +            indeces[n] = n +print(ans)