#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Maxn=100003;
int N,M,x,y,cnt,ans,f1,f2;
int p[Maxn],f[Maxn];
int getf(int u){
	if(f[u]==u)return u;
	return f[u]=getf(f[u]);
}
int main(){
	scanf("%d%d",&N,&M);
	for(int i=1;i<=N;++i){
		scanf("%d",&p[i]);
		f[i]=i;
	}
	for(int i=1;i<=M;++i){
		scanf("%d%d",&x,&y);
		f1=getf(x);f2=getf(y);
		if(f1!=f2)f[f1]=f2;
	}
	for(int i=1;i<=N;++i)
		if(getf(i)==getf(p[i]))++ans;
	printf("%d",ans);
}