#include <iostream>
using namespace std;
int p[110000], n, m, par[110000], res;

int root(int x) {
    if (par[x] == x) {
        return x;
    } else {
        return par[x] = root(par[x]);
    }
}

void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return;
    par[x] = y;
}

bool same (int x, int y) {
    return root(x) == root(y);
}

int main(void){
    // Your code here!
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        par[i] = i;
        cin >> p[i];
    }
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        unite(a, b);
    }
    for (int i = 1; i <= n; i++) {
        if(same(i, p[i])) res++;
    }
    cout << res << endl;
}
