#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back

typedef pair<int, int> ii;
typedef long long ll;
typedef pair<ll, ll> l4;

const int maxn = 1e6+1;
int n, m, perm[maxn], p[maxn];
int pa(int id)
{
  return id==p[id]?id:p[id]=pa(p[id]);
}
void merge(int x, int y)
{
  p[pa(x)] = pa(y);
}

int main()
{
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; ++i)
    {
      scanf("%d", perm+i);
      p[i] = i;
    }
  for (int i = 1; i <= m; ++i)
    {
      int x, y;
      scanf("%d %d", &x, &y);
      merge(x, y);
    }
  int ans = 0;
  for (int i = 1; i <= n; ++i)
    ans += pa(i) == pa(perm[i]);
  printf("%d\n", ans);
}


