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

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

uf = {i:-1 for i in range(1,n+1)}



def get_p(i):

  while uf[i] != i:

    if uf[i] == -1:

      return i

    i = uf[i]

  return i



for _ in range(m):

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

  if x > y:

    x,y = y,x

  if uf[x]== uf[y] == -1:

    uf[x] = x

    uf[y] = x

  elif uf[x] != -1 and uf[y] != -1:

    p = get_p(x)

    uf[x] = p

    index = y

    while index != uf[index]:

      index = uf[y]

      uf[y] = p

      y = index

    else:

      uf[index] = p

  else:

    if uf[x] == -1:

      p = get_p(y)

    else:

      p = get_p(x)

    uf[x] = p

    uf[y] = p



ans = 0  

for i, p in enumerate(P):

  if get_p(i+1) == get_p(p):

    ans += 1

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

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



uf = {i:i for i in range(1,n+1)}



def get_parent(i):

  lst = []

  while uf[i] != i:

    lst.append(i)

    i = uf[i]

  for l in lst[:-1]:

    uf[l] = i

  return i



def unite(i,j):

  p = get_parent(i)

  uf[get_parent(j)] = p



def same(i,j):

  return get_parent(i) == get_parent(j)



for _ in range(m):

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

  unite(x,y)



ans = 0

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

  ans += int(same(P[i-1],uf[i]))

print(ans)



n, m = list(map(int, input().split()))
P = list(map(int, input().split()))
uf = {i: -1 for i in range(1, n + 1)}


def get_p(i):
    while uf[i] != i:
        if uf[i] == -1:
            return i
        i = uf[i]
    return i


for _ in range(m):
    x, y = list(map(int, input().split()))
    if x > y:
        x, y = y, x
    if uf[x] == uf[y] == -1:
        uf[x] = x
        uf[y] = x
    elif uf[x] != -1 and uf[y] != -1:
        p = get_p(x)
        uf[x] = p
        index = y
        while index != uf[index]:
            index = uf[y]
            uf[y] = p
            y = index
        else:
            uf[index] = p
    else:
        if uf[x] == -1:
            p = get_p(y)
        else:
            p = get_p(x)
        uf[x] = p
        uf[y] = p
ans = 0
for i, p in enumerate(P):
    if get_p(i + 1) == get_p(p):
        ans += 1
print(ans)

n, m = list(map(int, input().split()))
P = list(map(int, input().split()))
uf = {i: i for i in range(1, n + 1)}


def get_parent(i):
    lst = []
    while uf[i] != i:
        lst.append(i)
        i = uf[i]
    for l in lst[:-1]:
        uf[l] = i
    return i


def unite(i, j):
    p = get_parent(i)
    uf[get_parent(j)] = p


def same(i, j):
    return get_parent(i) == get_parent(j)


for _ in range(m):
    x, y = list(map(int, input().split()))
    unite(x, y)
ans = 0
for i in range(1, n + 1):
    ans += int(same(P[i - 1], uf[i]))
print(ans)

-uf = {i: -1 for i in range(1, n + 1)} +uf = {i: i for i in range(1, n + 1)} -def get_p(i): +def get_parent(i): +    lst = [] -        if uf[i] == -1: -            return i +        lst.append(i) +    for l in lst[:-1]: +        uf[l] = i + + +def unite(i, j): +    p = get_parent(i) +    uf[get_parent(j)] = p + + +def same(i, j): +    return get_parent(i) == get_parent(j) -    if x > y: -        x, y = y, x -    if uf[x] == uf[y] == -1: -        uf[x] = x -        uf[y] = x -    elif uf[x] != -1 and uf[y] != -1: -        p = get_p(x) -        uf[x] = p -        index = y -        while index != uf[index]: -            index = uf[y] -            uf[y] = p -            y = index -        else: -            uf[index] = p -    else: -        if uf[x] == -1: -            p = get_p(y) -        else: -            p = get_p(x) -        uf[x] = p -        uf[y] = p +    unite(x, y) -for i, p in enumerate(P): -    if get_p(i + 1) == get_p(p): -        ans += 1 +for i in range(1, n + 1): +    ans += int(same(P[i - 1], uf[i]))