module UnionFind

implicit none
integer,allocatable,dimension(:)::par
!parentより

contains

recursive function root(x) result(res)
integer :: x,res
if(par(x) /= x) par(x)=root(par(x))
res = par(x)
end function

subroutine unite(x,y)
integer :: x,y
x = root(x)
y = root(y)
if(x /= y) par(x) = y
end subroutine

end module UnionFind

use UnionFind

integer N,M
integer,allocatable,dimension(:)::p
integer x,y
integer ans
read*,N,M
allocate(p(N),par(N))
read*,p

!par初期化
do i=1,N
  par(i)=i
end do


do i=1,M
  read*,x,y
  call unite(x,y)
end do

ans=0
do i=1,N
  if(root(i)==root(p(i)))ans=ans+1
end do

print"(i0)",ans
end