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

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

ans=0

import sys;sys.setrecursionlimit(10**9)

class u():#UnionFind f-根探し u-連結 s-連結? c-要素数

  def __init__(self,n):self.n,self.r=[-1]*n,[0]*n

  def f(self,x):

    if self.n[x]<0:return x

    else:self.n[x]=self.f(self.n[x]);return self.n[x]

  def u(self,x,y):

    x,y=self.f(x),self.f(y)

    if x==y:return

    elif self.r[x]>self.r[y]:self.n[x]+=self.n[y];self.n[y]=x

    else:self.n[y]+=self.n[x];self.n[x]=y;self.r[y]+=self.r[x]==self.r[y]

u=u(n)

for i in range(m):

  a,b=list(map(int,input().split()))

  u.u(a-1,b-1)

for i in range(n):

  if p[i]==i+1:ans+=1

  elif u.f(i)==u.f(p[i]-1):ans+=1

print(ans)
n,m=list(map(int,input().split()));p=list(map(int,input().split()));z=0;import sys;sys.setrecursionlimit(10**9);f=[i for i in range(n)]

def r(f,x):

  if f[x]!=x:f[x]=r(f,f[x])

  return f[x]

def u(f,a,b):

  x,y=r(f,a),r(f,b)

  if x!=y:f[x]=y

for i in range(m):a,b=list(map(int,input().split()));u(f,a-1,b-1)

for i in range(n):z+=r(f,i)==r(f,p[i]-1)

print(z)
n, m = list(map(int, input().split()))
p = list(map(int, input().split()))
ans = 0
import sys

sys.setrecursionlimit(10**9)


class u:  # UnionFind f-根探し u-連結 s-連結? c-要素数
    def __init__(self, n):
        self.n, self.r = [-1] * n, [0] * n

    def f(self, x):
        if self.n[x] < 0:
            return x
        else:
            self.n[x] = self.f(self.n[x])
            return self.n[x]

    def u(self, x, y):
        x, y = self.f(x), self.f(y)
        if x == y:
            return
        elif self.r[x] > self.r[y]:
            self.n[x] += self.n[y]
            self.n[y] = x
        else:
            self.n[y] += self.n[x]
            self.n[x] = y
            self.r[y] += self.r[x] == self.r[y]


u = u(n)
for i in range(m):
    a, b = list(map(int, input().split()))
    u.u(a - 1, b - 1)
for i in range(n):
    if p[i] == i + 1:
        ans += 1
    elif u.f(i) == u.f(p[i] - 1):
        ans += 1
print(ans)

n, m = list(map(int, input().split()))
p = list(map(int, input().split()))
z = 0
import sys

sys.setrecursionlimit(10**9)
f = [i for i in range(n)]


def r(f, x):
    if f[x] != x:
        f[x] = r(f, f[x])
    return f[x]


def u(f, a, b):
    x, y = r(f, a), r(f, b)
    if x != y:
        f[x] = y


for i in range(m):
    a, b = list(map(int, input().split()))
    u(f, a - 1, b - 1)
for i in range(n):
    z += r(f, i) == r(f, p[i] - 1)
print(z)

-ans = 0 +z = 0 +f = [i for i in range(n)] -class u:  # UnionFind f-根探し u-連結 s-連結? c-要素数 -    def __init__(self, n): -        self.n, self.r = [-1] * n, [0] * n - -    def f(self, x): -        if self.n[x] < 0: -            return x -        else: -            self.n[x] = self.f(self.n[x]) -            return self.n[x] - -    def u(self, x, y): -        x, y = self.f(x), self.f(y) -        if x == y: -            return -        elif self.r[x] > self.r[y]: -            self.n[x] += self.n[y] -            self.n[y] = x -        else: -            self.n[y] += self.n[x] -            self.n[x] = y -            self.r[y] += self.r[x] == self.r[y] +def r(f, x): +    if f[x] != x: +        f[x] = r(f, f[x]) +    return f[x] -u = u(n) +def u(f, a, b): +    x, y = r(f, a), r(f, b) +    if x != y: +        f[x] = y + + -    u.u(a - 1, b - 1) +    u(f, a - 1, b - 1) -    if p[i] == i + 1: -        ans += 1 -    elif u.f(i) == u.f(p[i] - 1): -        ans += 1 -print(ans) +    z += r(f, i) == r(f, p[i] - 1) +print(z)