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

vector<int> G[100000];
int p[100000];
int group[100000];

void dfs(int v, int g) {
    group[v] = g;

    for (int to : G[v]) {
        if (group[to] < 0) dfs(to, g);
    }
}

int main() {
    int N, M;
    cin >> N >> M;

    for (int i = 0; i < N; i++) {
        cin >> p[i];
        p[i]--;
    }

    for (int i = 0; i < M; i++) {
        int x, y;
        cin >> x >> y;

        x--; y--;
        G[x].push_back(y);
        G[y].push_back(x);
    }

    memset(group, -1, sizeof(group));
    int g = 0;
    for (int v = 0; v < N; v++) {
        if (group[v] < 0) dfs(v, g++);
    }

    int ans = 0;
    for (int v = 0; v < N; v++) {
        if (group[v] == group[p[v]]) ans++;
    }
    cout << ans << endl;

    return 0;
}