// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cmath>

#include <vector>

#include <list>

#include <set>

#include <map>

#include <stack>

#include <queue>

#include <string>

#include <utility>

#include <algorithm>

#include <functional>

#include <deque>

#define INF 1e9

#define MOD 1000000007

#define MAX_N 100001

#define mod(x) ((x % MOD) + MOD) % MOD



using namespace std;



typedef long long ll;

typedef pair<int,int> P;



struct edge { int from, to; };

vector<edge> G[MAX_N];

int value[MAX_N];

bool used[MAX_N];

vector<int> idx_list;

vector<int> value_list;



void dfs(int i) {

    used[i] = true;

    idx_list.push_back(i);

    value_list.push_back(value[i]);

    

    for (int j = 0; j < G[i].size(); j++) {

        edge e = G[i][j];

        if (!used[e.to]) {

            dfs(e.to);

        }

    }

}









int main() {

    int N, M;

    cin >> N >> M;

    

    for (int i = 1; i <= N; ++i) {

        cin >> value[i];

    }

    for (int i = 1; i <= M; ++i) {

        int x, y;

        cin >> x >> y;

        G[x].push_back(edge{x, y});

        G[y].push_back(edge{y, x});

    }

    

    int ans = 0;

    

    fill(used, used + MAX_N, false);

    for (int i = 1; i <= N; ++i) {

        idx_list.clear();

        value_list.clear();

        if (!used[i]) {

            dfs(i);

            sort(idx_list.begin(), idx_list.end());

            sort(value_list.begin(), value_list.end());

        

            vector<int> list_inter;

            set_intersection(idx_list.begin(), idx_list.end(), value_list.begin(), value_list.end(), back_inserter(list_inter));

            ans += (int)list_inter.size();

        }

    }

    

    cout << ans << endl;

    

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 3.5629088584137105x):
#include <iostream>

#include <cstdio>

#include <cmath>

#include <vector>

#include <list>

#include <set>

#include <map>

#include <stack>

#include <queue>

#include <string>

#include <utility>

#include <algorithm>

#include <functional>

#include <deque>

#define INF 1e9

#define MOD 1000000007

#define mod(x) ((x % MOD) + MOD) % MOD

#define MAX_N 100001

#define MAX_M 100001



using namespace std;



typedef long long ll;

typedef pair<int,int> P;



int par[MAX_N];

int myrank[MAX_N];



void init(int n)

{

  for (int i = 0; i < n; i++) {

    par[i] = i;

    myrank[i] = 0;

  }

}



int find(int x)

{

  if (par[x] == x) return x;

  return par[x] = find(par[x]);

}



void unite(int x, int y)

{

  x = find(x);

  y = find(y);

  if (x == y) return;

  if (myrank[x] < myrank[y]) {

    par[x] = y;

  } else {

    par[y] = x;

    if(myrank[x] == myrank[y]) myrank[x]++;

  }

}



bool same(int x, int y)

{

  return find(x) == find(y);

}



int main() {

  int N, M;

  int p[MAX_N];

  int x[MAX_M]; int y[MAX_N];

  cin >> N >> M;

  for (int i = 0; i < N; i++) {

    cin >> p[i]; p[i]--;

  }

  for (int i = 0; i < M; i++) {

    cin >> x[i] >> y[i]; x[i]--; y[i]--;

  }



  init(N);

  for (int i = 0; i < M; i++) {

    unite(x[i], y[i]);

  }



  int ans = 0;

  for (int i = 0; i < N; i++) {

    if (same(i, p[i])) {

      ans++;

    }

  }



  cout << ans << endl;



  return 0;

}