#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> p;
vector<vector<int>> val;
vector<int> who;
int getp(int a) { return a == p[a] ? a : p[a] = getp(p[a]); }
int main() {
  scanf("%d%d", &n, &m);
  p.resize(m);
  val.resize(m);
  who.resize(n);
  int ans = n - 1;
  for (int i = 0; i < int(m); i++) p[i] = i;
  for (int i = 0; i < int(n); i++) {
    int x;
    scanf("%d", &x);
    --x;
    who[i] = x;
    ans -= (i && who[i] == who[i - 1]);
    val[who[i]].push_back(i);
  }
  printf("%d\n", ans);
  for (int i = 0; i < int(m - 1); i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    v = getp(v - 1), u = getp(u - 1);
    if (val[v].size() < val[u].size()) swap(v, u);
    for (int x : val[u]) {
      if (x) ans -= who[x - 1] == v;
      if (x < n - 1) ans -= who[x + 1] == v;
    }
    for (int x : val[u]) {
      val[v].push_back(x);
      who[x] = v;
    }
    p[u] = v;
    printf("%d\n", ans);
  }
  return 0;
}
