#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m;
int p[maxn], belong[maxn];
int fa[maxn];
int fd(int f){
    return f == fa[f] ? f : fa[f] = fd(fa[f]);
}
int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> m;
    for(int i = 1; i <= n; ++i){
        cin >> p[i];
        fa[i] = i;
    }
    for(int i = 1; i <= m; ++i){
        int a, b;
        cin >> a >> b;
        int fl = fd(a), fr = fd(b);
        fa[fl] = fr;
    }
    for(int i = 1; i <= n; ++i){
        int f = fd(i);
        belong[p[i]] = f;
    }
    int ans = 0;
    for(int i = 1; i <= n; ++i){
        if(belong[i] == fd(i))
            ++ans;
    }
    cout << ans << endl;
    return 0;
}