#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll n, m, p[210000], x, y, d[210000], res;
const ll INF = 1000000000000000000;
vector<ll> vec[210000];

void dfs(ll x, ll y){
    d[x] = y;
    for(ll i = 0; i < vec[x].size(); i++) {
        if (d[vec[x][i]] != INF) continue;
        dfs(vec[x][i], y);
    }
}

int main(void){
    // Your code here!
    cin >> n >> m;
    for (ll i = 1; i <= n; i++) cin >> p[i];
    for (ll i = 0; i < m; i++) {
        cin >> x >> y;
        vec[x].push_back(y);
        vec[y].push_back(x);
    }
    for (ll i = 1; i <= n; i++) d[i] = INF;
    for (ll i = 1; i <= n; i++) {
        if (d[i] != INF) continue;
        dfs(i, i);
    }
    for (ll i = 1; i <= n; i++) {
        if (d[p[i]] == d[i]) res++;
    }
    cout << res << endl;
}
