from collections import defaultdict



class UnionFind(object):

  def __init__(self, n=1):

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

    self.rank = [0 for _ in range(n)]

    self.size = [1 for _ in range(n)]

  def find(self, x):

    if self.par[x] == x:

      return x

    else:

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

      return self.par[x]

  def union(self, x, y):

    x = self.find(x)

    y = self.find(y)

    if x != y:

      if self.rank[x] < self.rank[y]:

        x, y = y, x

      if self.rank[x] == self.rank[y]:

        self.rank[x] += 1

      self.par[y] = x

      self.size[x] += self.size[y]

  def is_same(self, x, y):

    return self.find(x) == self.find(y)

  def get_size(self, x):

    x = self.find(x)

    return self.size[x]





N, M = list(map(int,input().split()))



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



z = []

for i in range(M):

  t, s = list(map(int,input().split()))

  z.append((t,s))



uf = UnionFind(N)



for i in range(M):

  uf.union(z[i][0]-1, z[i][1]-1) #1というのは0番目のため1を引く。



#c = defaultdict(int)

ans = 0

for i in range(N):

  if uf.is_same(i, P[i]-1):

    ans += 1

print(ans)
class UnionFind(object):

    def __init__(self, n=1):

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

        self.rank = [0 for _ in range(n)]

        self.size = [1 for _ in range(n)]

    def find(self, x):

        """

        x が属するグループを探索

        """

        if self.par[x] == x:

            return x

        else:

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

            return self.par[x]

    def union(self, x, y):

        """

        x と y のグループを結合

        """

        x = self.find(x)

        y = self.find(y)

        if x != y:

            if self.rank[x] < self.rank[y]:

                x, y = y, x

            if self.rank[x] == self.rank[y]:

                self.rank[x] += 1

            self.par[y] = x

            self.size[x] += self.size[y]

    def is_same(self, x, y):

        """

        x と y が同じグループか否か

        """

        return self.find(x) == self.find(y)

    def get_size(self, x):

        """

        x が属するグループの要素数

        """

        x = self.find(x)

        return self.size[x]



    

N,M = list(map(int,input().split()))

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

uf = UnionFind(N)

for i in range(M):

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

    x-=1;y-=1

    uf.union(x,y)

ans = 0

for i in range(N):

    now = P[i]-1

    loc = i

    if uf.is_same(now,loc):

        ans += 1

print(ans)
from collections import defaultdict


class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
z = []
for i in range(M):
    t, s = list(map(int, input().split()))
    z.append((t, s))
uf = UnionFind(N)
for i in range(M):
    uf.union(z[i][0] - 1, z[i][1] - 1)  # 1というのは0番目のため1を引く。
# c = defaultdict(int)
ans = 0
for i in range(N):
    if uf.is_same(i, P[i] - 1):
        ans += 1
print(ans)

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        """
        x が属するグループを探索
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)

    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]


N, M = list(map(int, input().split()))
P = list(map(int, input().split()))
uf = UnionFind(N)
for i in range(M):
    x, y = list(map(int, input().split()))
    x -= 1
    y -= 1
    uf.union(x, y)
ans = 0
for i in range(N):
    now = P[i] - 1
    loc = i
    if uf.is_same(now, loc):
        ans += 1
print(ans)

-from collections import defaultdict - - +        """ +        x が属するグループを探索 +        """ +        """ +        x と y のグループを結合 +        """ +        """ +        x と y が同じグループか否か +        """ +        """ +        x が属するグループの要素数 +        """ -z = [] -for i in range(M): -    t, s = list(map(int, input().split())) -    z.append((t, s)) -    uf.union(z[i][0] - 1, z[i][1] - 1)  # 1というのは0番目のため1を引く。 -# c = defaultdict(int) +    x, y = list(map(int, input().split())) +    x -= 1 +    y -= 1 +    uf.union(x, y) -    if uf.is_same(i, P[i] - 1): +    now = P[i] - 1 +    loc = i +    if uf.is_same(now, loc):