//Nobody_hacks_me
//by lzt
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;

#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define fuck cerr<<"fuck"<<endl
#define debug(x) cerr<<#x<<"="<<x<<endl
#define lc (i<<1)
#define rc (i<<1|1)
#define clr(x) memset(x,0,sizeof(x))
#define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
#define SZ(x) ((int)x.size())
#define geti(x) x=read()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) print(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) print(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
#define rep(x,a,b) for(register int x=(int)a;x<=(int)b;x++)
#define rrep(x,a,b) for(register int x=(int)a;x>=(int)b;x--)

ll read()
{
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}

template<class T> inline void print(T x)
{
	if(x<0)putchar('-'),x=-x;
	register short a[20]={},sz=0;
	while(x)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}

const ll inf=2147483647;
const ll llinf=2e18;
const int mod=998244353;
const ull MAXMOD=(ull)mod*(mod-1);

//------------head-------------

//Problem No. 

int n,m;
int a[111111];
int fa[111111];
set<int> ss;

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

void uni(int x,int y){
	x=fp(x),y=fp(y);
	if(x==y) return;
	fa[x]=y;
}

vector<int> sy[111111];
int ans;

int main(){
	#ifdef LZT
	freopen("in","r",stdin);
	#endif
	
	getii(n,m);
	for(int i=1;i<=n;i++) fa[i]=i;
	for(int i=1;i<=n;i++)
		geti(a[i]);
	
	for(int i=1;i<=m;i++){
		int a,b;
		getii(a,b);
		uni(a,b);
	}
	
	for(int i=1;i<=n;i++)
		fa[i]=fp(i);
	
	for(int i=1;i<=n;i++)
		sy[fa[i]].pb(i);
	
	for(int i=1;i<=n;i++){
		if(fa[i]==i){
			ss.clear();
			for(int j=0;j<sy[i].size();j++){
				int nw=sy[i][j];
				ss.insert(a[nw]);
			}
			for(int j=0;j<sy[i].size();j++){
				if(ss.find(sy[i][j])!=ss.end()) ans++;
			}
		}
	}
	putsi(ans);
	return 0;
}

Measured memory_bytes: 8696