#16:00

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

a = list([int(x)-1 for x in input().split()])

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

for _ in range(m):

  X,Y = list(map(int,input().split()))

  X -= 1

  Y -= 1

  while pre[X] != X:

    X = pre[X]

  while pre[Y] != Y:

    Y = pre[Y]

  if X == Y:

    continue

  elif X < Y:

    pre[Y] = X

  else:

    pre[X] = Y

#print(a)

#print(pre)

region = [[] for _ in range(n)]

ser = [[] for _ in range(n)]

for i in range(n):

  b = a[i]

  I = i

  while pre[I] != I:

    I = pre[I]

  region[I].append(b)

  ser[I].append(i)

#print(region)

#print(ser)

home = [10**6 for _ in range(n)]

mother = [10**6 for _ in range(n)]

for i in range(n):

  for x in region[i]:

    home[x] = i

for i in range(n):

  for y in ser[i]:

    mother[y] = i

#print(home)

#print(mother)

ans = []

for i in range(n):

  if home[i] == mother[i]:

    ans.append(i)

#print(ans)

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

a = list([int(z)-1 for z in input().split()])

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

for _ in range(m):

  x,y = [int(z)-1 for z in input().split()]

  while pre[x] != x:

    x = pre[x]

  while pre[y] != y:

    y = pre[y]

  if x == y:

    continue

  elif x < y:

    pre[y] = x

  else:

    pre[x] = y

home1 = [10**6 for _ in range(n)]

home2 = [10**6 for _ in range(n)]

for i in range(n):

  I = i

  while pre[I] != I:

    I = pre[I]

  home1[i] = I

  home2[a[i]] = I

ans = 0

for i in range(n):

  if home1[i] == home2[i]:

    ans += 1

print(ans)
# 16:00
n, m = list(map(int, input().split()))
a = list([int(x) - 1 for x in input().split()])
pre = [i for i in range(n)]
for _ in range(m):
    X, Y = list(map(int, input().split()))
    X -= 1
    Y -= 1
    while pre[X] != X:
        X = pre[X]
    while pre[Y] != Y:
        Y = pre[Y]
    if X == Y:
        continue
    elif X < Y:
        pre[Y] = X
    else:
        pre[X] = Y
# print(a)
# print(pre)
region = [[] for _ in range(n)]
ser = [[] for _ in range(n)]
for i in range(n):
    b = a[i]
    I = i
    while pre[I] != I:
        I = pre[I]
    region[I].append(b)
    ser[I].append(i)
# print(region)
# print(ser)
home = [10**6 for _ in range(n)]
mother = [10**6 for _ in range(n)]
for i in range(n):
    for x in region[i]:
        home[x] = i
for i in range(n):
    for y in ser[i]:
        mother[y] = i
# print(home)
# print(mother)
ans = []
for i in range(n):
    if home[i] == mother[i]:
        ans.append(i)
# print(ans)
print((len(ans)))

n, m = list(map(int, input().split()))
a = list([int(z) - 1 for z in input().split()])
pre = [i for i in range(n)]
for _ in range(m):
    x, y = [int(z) - 1 for z in input().split()]
    while pre[x] != x:
        x = pre[x]
    while pre[y] != y:
        y = pre[y]
    if x == y:
        continue
    elif x < y:
        pre[y] = x
    else:
        pre[x] = y
home1 = [10**6 for _ in range(n)]
home2 = [10**6 for _ in range(n)]
for i in range(n):
    I = i
    while pre[I] != I:
        I = pre[I]
    home1[i] = I
    home2[a[i]] = I
ans = 0
for i in range(n):
    if home1[i] == home2[i]:
        ans += 1
print(ans)

-# 16:00 -a = list([int(x) - 1 for x in input().split()]) +a = list([int(z) - 1 for z in input().split()]) -    X, Y = list(map(int, input().split())) -    X -= 1 -    Y -= 1 -    while pre[X] != X: -        X = pre[X] -    while pre[Y] != Y: -        Y = pre[Y] -    if X == Y: +    x, y = [int(z) - 1 for z in input().split()] +    while pre[x] != x: +        x = pre[x] +    while pre[y] != y: +        y = pre[y] +    if x == y: -    elif X < Y: -        pre[Y] = X +    elif x < y: +        pre[y] = x -        pre[X] = Y -# print(a) -# print(pre) -region = [[] for _ in range(n)] -ser = [[] for _ in range(n)] +        pre[x] = y +home1 = [10**6 for _ in range(n)] +home2 = [10**6 for _ in range(n)] -    b = a[i] -    region[I].append(b) -    ser[I].append(i) -# print(region) -# print(ser) -home = [10**6 for _ in range(n)] -mother = [10**6 for _ in range(n)] +    home1[i] = I +    home2[a[i]] = I +ans = 0 -    for x in region[i]: -        home[x] = i -for i in range(n): -    for y in ser[i]: -        mother[y] = i -# print(home) -# print(mother) -ans = [] -for i in range(n): -    if home[i] == mother[i]: -        ans.append(i) -# print(ans) -print((len(ans))) +    if home1[i] == home2[i]: +        ans += 1 +print(ans)