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

struct UnionFind{
  vector<int> par;
  UnionFind(int n):par(n){
    rep(i,n) par[i]=i;
  }
  
  int root(int x){
    if(par[x]==x) return x;
    return par[x]=root(par[x]);
  }
  void unite(int x,int y){
    int rx = root(x);
    int ry = root(y);
    if(rx==ry) return;
    par[rx] = ry;
  }
  bool same(int x,int y){
    return root(x)==root(y);
  }
};

int main(){
  int n,m;
  cin>>n>>m;
  vector<int> p(n),q(n);
  rep(i,n){cin>>p[i]; p[i]--; q[p[i]]=i;}
  
  UnionFind tree(n);
  
  rep(i,m){
    int x,y;
    cin>>x>>y;
    x--; y--;
    tree.unite(x,y);
  }
  
  int ans = 0;
  rep(i,n){
    if(tree.same(q[i],i)) ans++;
  }
  cout << ans << endl;
}