module mod1
implicit none
integer :: par(0:100000)
end module mod1
 
program test
use mod1
implicit none
integer :: n,m,i,j,k,count=0
integer :: p(200000),x(200000),y(200000)
 
read(*,*) n,m
read(*,*) (p(i),i=1,n)
do i=1,m
	read(*,*) x(i),y(i)
enddo
 
!親の番号
do i=0,n
	par(i) = i
enddo

!連結していく
do i=1,m
	call unite(x(i),y(i))
enddo

!判定する
do i=1,n
	if(same(i,p(i)) == 1)then
		count = count + 1
	endif
enddo

write(*,*) count

contains
recursive function root(x) result(res)
integer :: x,res
	if(par(x) == x) then
		continue
	else
		par(x) = root(par(x))
	endif
	res = par(x)
end function

subroutine unite(x,y) !連結
integer :: x,y
x = root(x)
y = root(y)
if(x .ne. y) then
	par(x) = y
endif
end subroutine
 
 
function same(x,y) !判定
integer :: x,y,same
if(root(x) .eq. root(y)) then
	same = 1
else
	same = 0
endif
end function
 
end program test