#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-') nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1) return -ans;
  return ans;
}
int fa[200005], a[200005], n, m, siz[200005];
vector<int> v[200005];
int find(int u) { return fa[u] == u ? u : fa[u] = find(fa[u]); }
signed main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) v[a[i]].push_back(i);
  for (int i = 1; i <= m; i++) fa[i] = i, siz[i] = 1;
  int ans = n - 1;
  for (int i = 1; i < n; i++) ans -= a[i] == a[i + 1];
  printf("%d\n", ans);
  for (int _ = 1; _ < m; _++) {
    int x = find(read()), y = find(read());
    if (siz[x] > siz[y]) swap(x, y);
    fa[x] = y, siz[y] += siz[x];
    for (int i : v[x]) {
      ans += a[i] == a[i + 1];
      ans += a[i] == a[i - 1];
      a[i] = y;
      ans -= a[i] == a[i + 1];
      ans -= a[i] == a[i - 1];
      v[y].push_back(i);
    }
    printf("%d\n", ans);
  }
  return 0;
}
