#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e5;
int n,m;
int a[MAXN+5], fa[MAXN+5];
int rpos[MAXN+5];
//int p[100050],x[100050],y[100050];

int find(int x){
	if(fa[x] == x) return x;
	else return fa[x] = find(fa[x]);
}

void Init(){
	scanf("%d %d", &n, &m);
	for(int i = 1; i <= n; i++){
		scanf("%d", &a[i]);
		fa[i] = i;
		rpos[a[i]] = i;
	}
	for(int i = 1; i <= m; i++){
		int u, v;
		scanf("%d %d", &u, &v);
		fa[find(u)] = find(v);
	}
	//for(int i = 1;  i<= n; i++) printf("%d ", find(i));
}

int main(){
	Init();
	int res = 0;
	for(int i = 1; i <= n; i++){
		if(find(i) == find(rpos[i])) res++;
	}
	printf("%d\n", res);
	return 0;
}