#include <bits/stdc++.h>
using namespace std;
int n, m;
int pile[200009];
vector<int> contents[200009];
int p[200009];
long long int ans;
int find_p(int u) {
  if (p[u] == u) return u;
  return p[u] = find_p(p[u]);
}
int union_pile(int u, int v) {
  u = find_p(u);
  v = find_p(v);
  if (contents[u].size() > contents[v].size()) swap(u, v);
  for (int x : contents[u]) {
    contents[v].push_back(x);
    if (x > 1)
      if (pile[x - 1] == v) ans--;
    if (x < n)
      if (pile[x + 1] == v) ans--;
  }
  for (int x : contents[u]) pile[x] = v;
  p[u] = v;
  return v;
}
int main() {
  cin >> n >> m;
  ans = n - 1;
  for (int i = 1; i <= n; i++) {
    cin >> pile[i];
    contents[pile[i]].push_back(i);
    if (i != 1 and pile[i] == pile[i - 1]) ans--;
  }
  cout << ans << "\n";
  for (int i = 1; i <= m; i++) p[i] = i;
  int u, v;
  for (int i = 1; i < m; i++) {
    cin >> u >> v;
    union_pile(u, v);
    cout << ans << "\n";
  }
}
