#include<stdio.h>
int main(void){
  char *s="python3 -c\""
"class UnionFind():\n"
"    def __init__(self,size):\n"
"        self.table=[-1]*size\n"
"    def root(self,x):\n"
"        while self.table[x]>=0:\n"
"            x=self.table[x]\n"
"        return x\n"
"    def unite(self,x,y):\n"
"        s1=self.root(x)\n"
"        s2=self.root(y)\n"
"        if s1!=s2:\n"
"            if self.table[s1]>self.table[s2]:\n"
"                s1,s2=s2,s1\n"
"            self.table[s1]+=self.table[s2]\n"
"            self.table[s2]=s1\n"
"        return\n"
"    def same(self,x,y):\n"
"        return self.root(x)==self.root(y)\n"
"    def size(self,x):\n"
"        return -self.table[self.root(x)]\n"
"input=open(0).readline\n"
"n,m=map(int,input().split())\n"
"*p,=map(int,input().split())\n"
"u=UnionFind(n+1)\n"
"for _ in range(m):\n"
"    u.unite(*map(int,input().split()))\n"
"print(sum(u.same(p[i],i+1)for i in range(n)))\n"
"\"";
  system(s);
  return 0;
}            