#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <iomanip>
#include <sstream>
#define PI acos(-1.0)
using namespace std;
const long long INF=1e10+7;
typedef unsigned long long ULL;
typedef long long LL;
const int maxn=100005;
int fa[maxn];
int getfa(int u) {
    if(fa[u]!=u) fa[u]=getfa(fa[u]);
    return fa[u];
}
void Union(int x,int y) {
    int fx=getfa(x),fy=getfa(y);
    if(fx!=fy)
        fa[fx]=fy;
}
int in[maxn];
int main() {
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) {
        scanf("%d",in+i);
    }
    for(int i=1;i<=n;i++)
        fa[i]=i;
    while(m--) {
        int a,b;
        scanf("%d%d",&a,&b);
        Union(a,b);
    }
    int ans=0;
    for(int i=1;i<=n;i++) {
        if(getfa(i)==getfa(in[i])){
            ans++;
        }
    }
    printf("%d\n",ans);
}
