#include<iostream>
#include<vector>
using namespace std;

int n, m;
int par[100001];
vector<int> v;

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

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

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

int main() {
    cin >> n >> m;
    for(int i = 0; i < n; i++) {
        int a;
        cin >> a;
        v.push_back(--a);
        par[i] = i;
    }

    for(int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        merge(--x, --y);
    }

    int ans = 0;
    for(int i = 0; i < n; i++) {
        if(same(i, v[i])) ans++;
    }

    cout << ans << endl;

    return 0;
}