I=lambda:list(map(int,input().split()));n,m=I();p=I();l=[i for i in range(n+1)]

def u(x):

 if l[x]!=x:l[x]=u(l[x])

 return l[x]

for _ in[0]*m:x,y=I();l[u(x)]=u(y)

print((sum(u(i)==u(j)for i,j in enumerate(p,1))))
class UnionFind():

    def __init__(self,size):

        self.table=[-1]*size

    def root(self,x):

        while self.table[x]>=0:

            x=self.table[x]

        return x

    def unite(self,x,y):

        s1=self.root(x)

        s2=self.root(y)

        if s1!=s2:

            if self.table[s1]>self.table[s2]:

                s1,s2=s2,s1

            self.table[s1]+=self.table[s2]

            self.table[s2]=s1

        return

    def same(self,x,y):

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

    def size(self,x):

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

input=open(0).readline

n,m=list(map(int,input().split()))

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

u=UnionFind(n+1)

for _ in range(m):

    u.unite(*list(map(int,input().split())))

print((sum(u.same(p[i],i+1)for i in range(n))))
I = lambda: list(map(int, input().split()))
n, m = I()
p = I()
l = [i for i in range(n + 1)]


def u(x):
    if l[x] != x:
        l[x] = u(l[x])
    return l[x]


for _ in [0] * m:
    x, y = I()
    l[u(x)] = u(y)
print((sum(u(i) == u(j) for i, j in enumerate(p, 1))))

class UnionFind:
    def __init__(self, size):
        self.table = [-1] * size

    def root(self, x):
        while self.table[x] >= 0:
            x = self.table[x]
        return x

    def unite(self, x, y):
        s1 = self.root(x)
        s2 = self.root(y)
        if s1 != s2:
            if self.table[s1] > self.table[s2]:
                s1, s2 = s2, s1
            self.table[s1] += self.table[s2]
            self.table[s2] = s1
        return

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

    def size(self, x):
        return -self.table[self.root(x)]


input = open(0).readline
n, m = list(map(int, input().split()))
(*p,) = list(map(int, input().split()))
u = UnionFind(n + 1)
for _ in range(m):
    u.unite(*list(map(int, input().split())))
print((sum(u.same(p[i], i + 1) for i in range(n))))

-I = lambda: list(map(int, input().split())) -n, m = I() -p = I() -l = [i for i in range(n + 1)] +class UnionFind: +    def __init__(self, size): +        self.table = [-1] * size + +    def root(self, x): +        while self.table[x] >= 0: +            x = self.table[x] +        return x + +    def unite(self, x, y): +        s1 = self.root(x) +        s2 = self.root(y) +        if s1 != s2: +            if self.table[s1] > self.table[s2]: +                s1, s2 = s2, s1 +            self.table[s1] += self.table[s2] +            self.table[s2] = s1 +        return + +    def same(self, x, y): +        return self.root(x) == self.root(y) + +    def size(self, x): +        return -self.table[self.root(x)] -def u(x): -    if l[x] != x: -        l[x] = u(l[x]) -    return l[x] - - -for _ in [0] * m: -    x, y = I() -    l[u(x)] = u(y) -print((sum(u(i) == u(j) for i, j in enumerate(p, 1)))) +input = open(0).readline +n, m = list(map(int, input().split())) +(*p,) = list(map(int, input().split())) +u = UnionFind(n + 1) +for _ in range(m): +    u.unite(*list(map(int, input().split()))) +print((sum(u.same(p[i], i + 1) for i in range(n))))