def solve():

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

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

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

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

  

  def get_root(x):

    p = pare_inds[x]

    if p == x:

      return x

    a = get_root(p)

    pare_inds[x] = a

    return a

  

  for i in range(m):

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

    x, y = x - 1, y - 1

    r_x, r_y  = get_root(x), get_root(y)

    if r_x < r_y:

      pare_inds[y] = r_x

      pare_inds[r_y] = r_x

    else:

      pare_inds[x] = r_y

      pare_inds[r_x] = r_y

  

  for i in range(n):

    belongs[get_root(i)].append(i)



  ans = 0



  for lst in belongs:

    lst2 = [plst[i] - 1 for i in lst]

    set1 = set(lst)

    set2 = set(lst2)

    ans += len(set1 & set2)

#    lst.sort()

#    lst2.sort()

#    ind1 = 0

#    ind2 = 0

#    end = len(lst)

#    while ind1 < end and ind2 < end:

#      if lst[ind1] == lst2[ind2]:

#        ans += 1

#        ind1 += 1

#        ind2 += 1

#      elif lst[ind1] < lst2[ind2]:

#        ind1 += 1

#      else:

#        ind2 += 1

  

  print(ans)

solve()
def solve():

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

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

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

  

  def find(x):

    p = pare_inds[x]

    if p == x:

      return x

    a = find(p)

    pare_inds[x] = a

    return a

  

  for i in range(m):

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

    x, y = x - 1, y - 1

    r_x, r_y  = find(x), find(y)

    pare_inds[y] = r_x

    pare_inds[r_y] = r_x

  

  ans = 0

  for i in range(n):

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

      ans += 1



  print(ans)

solve()
def solve():
    n, m = list(map(int, input().split()))
    plst = list(map(int, input().split()))
    pare_inds = [i for i in range(n)]
    belongs = [[] for i in range(n)]

    def get_root(x):
        p = pare_inds[x]
        if p == x:
            return x
        a = get_root(p)
        pare_inds[x] = a
        return a

    for i in range(m):
        x, y = list(map(int, input().split()))
        x, y = x - 1, y - 1
        r_x, r_y = get_root(x), get_root(y)
        if r_x < r_y:
            pare_inds[y] = r_x
            pare_inds[r_y] = r_x
        else:
            pare_inds[x] = r_y
            pare_inds[r_x] = r_y
    for i in range(n):
        belongs[get_root(i)].append(i)
    ans = 0
    for lst in belongs:
        lst2 = [plst[i] - 1 for i in lst]
        set1 = set(lst)
        set2 = set(lst2)
        ans += len(set1 & set2)
    #    lst.sort()
    #    lst2.sort()
    #    ind1 = 0
    #    ind2 = 0
    #    end = len(lst)
    #    while ind1 < end and ind2 < end:
    #      if lst[ind1] == lst2[ind2]:
    #        ans += 1
    #        ind1 += 1
    #        ind2 += 1
    #      elif lst[ind1] < lst2[ind2]:
    #        ind1 += 1
    #      else:
    #        ind2 += 1
    print(ans)


solve()

def solve():
    n, m = list(map(int, input().split()))
    plst = list(map(int, input().split()))
    pare_inds = [i for i in range(n)]

    def find(x):
        p = pare_inds[x]
        if p == x:
            return x
        a = find(p)
        pare_inds[x] = a
        return a

    for i in range(m):
        x, y = list(map(int, input().split()))
        x, y = x - 1, y - 1
        r_x, r_y = find(x), find(y)
        pare_inds[y] = r_x
        pare_inds[r_y] = r_x
    ans = 0
    for i in range(n):
        if find(i) == find(plst[i] - 1):
            ans += 1
    print(ans)


solve()

-    belongs = [[] for i in range(n)] -    def get_root(x): +    def find(x): -        a = get_root(p) +        a = find(p) -        r_x, r_y = get_root(x), get_root(y) -        if r_x < r_y: -            pare_inds[y] = r_x -            pare_inds[r_y] = r_x -        else: -            pare_inds[x] = r_y -            pare_inds[r_x] = r_y +        r_x, r_y = find(x), find(y) +        pare_inds[y] = r_x +        pare_inds[r_y] = r_x +    ans = 0 -        belongs[get_root(i)].append(i) -    ans = 0 -    for lst in belongs: -        lst2 = [plst[i] - 1 for i in lst] -        set1 = set(lst) -        set2 = set(lst2) -        ans += len(set1 & set2) -    #    lst.sort() -    #    lst2.sort() -    #    ind1 = 0 -    #    ind2 = 0 -    #    end = len(lst) -    #    while ind1 < end and ind2 < end: -    #      if lst[ind1] == lst2[ind2]: -    #        ans += 1 -    #        ind1 += 1 -    #        ind2 += 1 -    #      elif lst[ind1] < lst2[ind2]: -    #        ind1 += 1 -    #      else: -    #        ind2 += 1 +        if find(i) == find(plst[i] - 1): +            ans += 1