#include<bits/stdc++.h>

using namespace std;
vector<int>dsu;

int parent(int a){
    if(dsu[a] == a) return a;
    return dsu[a] = parent(dsu[a]);
}


void join(int a, int b){
    int pa = parent(a);
    int pb = parent(b);
    dsu[pa] = pb;
}

int main(){
    int n, m, a, b;
    scanf("%d %d", &n, &m);
    vector<int>p(n);
    dsu = vector<int>(n);
    for(int i = 0; i < n; i++){
        scanf("%d", &p[i]);
        p[i]--;
    }

    for(int i = 0; i < n; i++) dsu[i] = i;
    for(int i = 0; i < m; i++){
        scanf("%d %d", &a, &b);
        --a; --b;
        join(a,b);
    }
    int ans = 0;
    for(int i = 0; i < n; i++){
        ans += parent(p[i]) == parent(i); 
    }

    printf("%d\n", ans);
}   