! https://beta.atcoder.jp/contests/abc097/submissions/3768076

module UnionFind
    implicit none
    public
        type :: Union_Find
            integer,allocatable,dimension(:) :: par
        end type Union_Find
    contains
        subroutine init(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 init
        recursive function root(p,n) result(ret)
            implicit none
            type(Union_Find) p
            integer ret,n
            if(p%par(n)==n)then
                ret = n
                return
            else
                p%par(n) = root(p,p%par(n))
                ret = p%par(n)
                return
            endif
        end
        subroutine unite(p,a,b)
            implicit none
            type(Union_Find) p
            integer a,b
            a = root(p,a)
            b = root(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