#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using  namespace std;

class UnionFind {
  public:
  vector<int> par;

  UnionFind(int n) {
    par = vector<int>(n, -1);
  }

  int root(int x) {
    return (par[x] < 0) ? x : par[x] = root(par[x]);
  }

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

  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if(x == y) return ;
    if (par[x] > par[y]) swap(x, y);
    par[x] += par[y];
    par[y] = x;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  int p[N];
  UnionFind uf(N);
  rep(i, N) {
    cin >> p[i];
    p[i]--;
  }
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    uf.unite(x-1, y-1);
  }

  int ans = 0;
  rep(i, N) {
    if (uf.same(i, p[i])) ++ans;
  }
  cout << ans << endl;
}