#include<cstdio>
using namespace std;
int f[100001],p[100001],t[100001];
int fa(int a)
{
	if (f[a]!=a) f[a]=fa(f[a]);
	return f[a];
}
int main()
{
	int n,m,a,b,ans=0;
	scanf("%d %d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		f[i]=i;
		scanf("%d",&p[i]);
	}
	while (m--)
	{
		scanf("%d %d",&a,&b);
		f[fa(a)]=fa(b);
	}
	for (int i=1;i<=n;i++) t[p[i]]=fa(i);
	for (int i=1;i<=n;i++) if (f[i]==t[i]) ans++;
	printf("%d",ans);
	return 0;
}