#include <bits/stdc++.h>
using namespace std;
set<int> s[200005];
int fa[200005], ANS[200005];
inline int ff(int x) {
  if (fa[x] == x) return x;
  return fa[x] = ff(fa[x]);
}
int main(int argc, char** argv) {
  int n, m, ans;
  cin >> n >> m;
  ans = n - 1;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    s[x].insert(i);
    if (s[x].find(i - 1) != s[x].end()) --ans, ++ANS[x];
    if (s[x].find(i + 1) != s[x].end()) --ans, ++ANS[x];
    fa[i] = i;
  }
  cout << ans << "\n";
  while (--m) {
    int u, v;
    cin >> u >> v;
    u = ff(u), v = ff(v);
    if (u == v) {
      cout << ans << '\n';
      continue;
    }
    if (s[u].size() < s[v].size()) swap(u, v);
    for (auto x : s[v]) {
      if (s[u].find(x - 1) != s[u].end()) --ans, ++ANS[u];
      if (s[u].find(x + 1) != s[u].end()) --ans, ++ANS[u];
      s[u].insert(x);
    }
    ans += ANS[v], s[v].clear();
    fa[ff(v)] = ff(u);
    cout << ans << "\n";
  }
  return 0;
}
