#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
#define MAX 1005
int fa[MAX],arr[MAX];
int gfa(int x){
	if (x!=fa[x]){
		fa[x]=gfa(fa[x]);
	}
	return fa[x];
}

void sfa(int x,int y){
	int fx=gfa(x),fy=gfa(y);
	if (fx!=fy){
		fa[fx]=fy;
	}
}
int main(){
	int n,m,u,v;
	while (scanf("%d%d",&n,&m)!=EOF){
		int ans=0;
		for (int i=1;i<=n;i++){
			scanf("%d",&arr[i]);
			fa[i]=i;
		}
		for (int i=0;i<m;i++){
			scanf("%d%d",&u,&v);
			sfa(u,v);
		}
		for (int i=1;i<=n;i++){
			if (gfa(arr[i])==gfa(i)){
				ans++;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
