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

f=lambda x:int(x)-1

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

par=[j for j in range(n)]

def find(x):

  if x==par[x]:

    return x

  else:

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

    return par[x]

 

def union(a,b):

  ra=find(a)

  rb=find(b)

  if ra!=rb:

    par[ra]=par[rb]

 

for i in range(m):

  v,w=list(map(f,input().split()))

  union(v,w)

 

ans=0

for j in range(n):

  if find(j)==find(p[j]):

    ans+=1

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

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

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

 

def find(x):

  if par[x]==x:

    return par[x]

  else:

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

    return par[x]

 

def union(x,y):

  fx=find(x)

  fy=find(y)

  if fx!=fy:

    par[fx]=fy

 

f=lambda x:int(x)-1

for mm in range(m):

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

  union(x,y)

 

ans=0

for i in range(n):

  if find(p[i]-1)==find(i):

    ans+=1

print(ans)
n, m = list(map(int, input().split()))
f = lambda x: int(x) - 1
p = list(map(f, input().split()))
par = [j for j in range(n)]


def find(x):
    if x == par[x]:
        return x
    else:
        par[x] = find(par[x])
        return par[x]


def union(a, b):
    ra = find(a)
    rb = find(b)
    if ra != rb:
        par[ra] = par[rb]


for i in range(m):
    v, w = list(map(f, input().split()))
    union(v, w)
ans = 0
for j in range(n):
    if find(j) == find(p[j]):
        ans += 1
print(ans)

n, m = list(map(int, input().split()))
p = list(map(int, input().split()))
par = [i for i in range(n)]


def find(x):
    if par[x] == x:
        return par[x]
    else:
        par[x] = find(par[x])
        return par[x]


def union(x, y):
    fx = find(x)
    fy = find(y)
    if fx != fy:
        par[fx] = fy


f = lambda x: int(x) - 1
for mm in range(m):
    x, y = list(map(f, input().split()))
    union(x, y)
ans = 0
for i in range(n):
    if find(p[i] - 1) == find(i):
        ans += 1
print(ans)

-f = lambda x: int(x) - 1 -p = list(map(f, input().split())) -par = [j for j in range(n)] +p = list(map(int, input().split())) +par = [i for i in range(n)] -    if x == par[x]: -        return x +    if par[x] == x: +        return par[x] -def union(a, b): -    ra = find(a) -    rb = find(b) -    if ra != rb: -        par[ra] = par[rb] +def union(x, y): +    fx = find(x) +    fy = find(y) +    if fx != fy: +        par[fx] = fy -for i in range(m): -    v, w = list(map(f, input().split())) -    union(v, w) +f = lambda x: int(x) - 1 +for mm in range(m): +    x, y = list(map(f, input().split())) +    union(x, y) -for j in range(n): -    if find(j) == find(p[j]): +for i in range(n): +    if find(p[i] - 1) == find(i):