#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, p[N], in[N];
vector<int> comp[N];
int Find(int x) {
  if (x == p[x]) return x;
  return p[x] = Find(p[x]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> m >> n;
  for (int i = 1; i <= m; i++) {
    cin >> in[i];
    comp[in[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) p[i] = i;
  int cnt = 0;
  for (int i = 2; i <= m; i++)
    if (in[i] == in[i - 1]) cnt++;
  for (int i = 1; i < n; i++) {
    cout << m - 1 - cnt << '\n';
    int a, b;
    cin >> a >> b;
    int u = Find(a);
    int v = Find(b);
    if (comp[u].size() > comp[v].size()) swap(u, v);
    for (auto& x : comp[u]) {
      comp[v].push_back(x);
      if (in[x - 1] == v) cnt++;
      if (in[x + 1] == v) cnt++;
    }
    for (auto& x : comp[u]) in[x] = v;
    p[u] = v;
  }
  cout << 0;
}
