module UnionFind
    implicit none
    private
        type :: Union_Find
            integer,allocatable,dimension(:) :: par
        end type Union_Find
    
        interface init
            module procedure initpar
        end interface init
    
        interface root
            module procedure roott
        end interface root
    
        interface unite
            module procedure unite2
        end interface unite
        public :: Union_Find
        public :: init,root,unite
    contains
    
        subroutine initpar(p,n)
            implicit none
            type(Union_Find)  p
            integer n,i
            allocate( p%par(n) )
            do i=1,n
                p%par(i) = i
            enddo
        end subroutine initpar
    
        recursive function roott(p,n) result(ret)
            implicit none
            type(Union_Find) p
            integer ret,n
            if(p%par(n)==n)then
                ret = n
                return
            else
                ret = roott(p,p%par(n))
                return
            endif
        end
    
        subroutine unite2(p,a,b)
            implicit none
            type(Union_Find) p
            integer a,b
            a = roott(p,a)
            b = roott(p,b)
            if(a/=b)then
                p%par(a) = b
            endif
        end
end module UnionFind


program atcoder_abc97d
    use UnionFind
    implicit none
    type(Union_Find) a
    integer n,m,i,x,y,ans
    integer,allocatable,dimension(:) :: p
    read*,n,m
    allocate( p(n) )
    call init(a,n)

    read(*,*)(p(i),i=1,n)
    do i = 1,m
        read*,x,y
        call unite(a,x,y)
    enddo

    ans = 0
    do i=1,n
        if(root(a,p(i))==root(a,i))then
            ans = ans+1
        endif
    enddo
    write(*,'(I0)')ans
!----------------------functions--------------------------
contains



end program atcoder_abc97d

