#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <map>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;
int fa[100010];
int finds(int x){
    if(fa[x] == x) return x;
    fa[x] = finds(fa[x]);
    return fa[x];
}
void join(int x,int y){
    int fx = finds(x);
    int fy = finds(y);
    if(fx != fy) fa[fx] = fy;
}
int p[100005];
int a[100005],b[100005];
int main()
{
    int n,m;
    cin >> n >> m;
    for(int i = 1;i <= n;++i) scanf("%d",&p[i]);
    for(int i = 1;i <= n;++i) fa[i] = i;
    for(int i = 1;i <= m;++i){
        scanf("%d%d",&a[i],&b[i]);
        join(a[i],b[i]);
    }
    int cnt = 0;
    for(int i = 1;i <= n;++i){
        if(finds(p[i]) == finds(i)) cnt++;
    }
    cout << cnt << endl;
}
