#include <bits/stdc++.h>
const int MAX_N = 200000;
const int MAX_M = 200000;
int towerBoss[1 + MAX_M];
int mismatches;
struct DSU {
  int boss, color;
  std::vector<int> sons;
} dsu[1 + MAX_N];
int myfind(int nod) {
  if (nod == dsu[nod].boss) return nod;
  dsu[nod].boss = myfind(dsu[nod].boss);
  return dsu[nod].boss;
}
void myunion(int a, int b, int N) {
  int sa = myfind(towerBoss[a]), sb = myfind(towerBoss[b]);
  if (sa != sb) {
    if (dsu[sa].sons.size() < dsu[sb].sons.size()) {
      std::swap(sa, sb);
      std::swap(a, b);
    }
    towerBoss[a] = sa;
    for (auto it : dsu[sb].sons) {
      if (it - 1 >= 1 && myfind(it - 1) == sa) --mismatches;
      if (it + 1 <= N && myfind(it + 1) == sa) --mismatches;
    }
    for (auto it : dsu[sb].sons) {
      dsu[it].color = dsu[sa].color;
      dsu[sa].sons.push_back(it);
    }
    dsu[sb].sons.clear();
    dsu[sb].boss = sa;
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    dsu[i].boss = i;
    scanf("%d", &dsu[i].color);
    if (i > 1 && dsu[i].color != dsu[i - 1].color) ++mismatches;
    if (towerBoss[dsu[i].color] != 0) {
      dsu[i].boss = towerBoss[dsu[i].color];
      dsu[towerBoss[dsu[i].color]].sons.push_back(i);
    } else {
      dsu[i].sons.push_back(i);
      towerBoss[dsu[i].color] = i;
    }
  }
  printf("%d\n", mismatches);
  for (int i = 0; i < m - 1; ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    myunion(a, b, n);
    printf("%d\n", mismatches);
  }
  return 0;
}
