#include <bits/stdc++.h>
using namespace std;
vector<int> data(100001,-1);

int root(int x){
  if(data[x] == -1)return x;
  else return data[x] = root(data[x]);
}

bool same(int x,int y){
  if(root(x) == root(y))return 1;
  else return 0;
}

int unite(int x,int y){
  if(!same(x,y))data[root(x)] = y;
  return 0;
}

int main(){
  int N,M,x,y,q,ans=0;
  cin >> N >> M;
  int p[2*N];
  for(int i = 0;i < 2*N;i++)p[i]=0;
  for(int i = 1;i <= N;i++){
    cin >> q;
    p[q] = i;
  }
  for(int i = 0;i < M;i++){
    cin >> x >> y;
    unite(x,y);
  }
  for(int i = 1;i <= N;i++)if(same(p[i],i))ans++;
  cout << ans << endl;
}
