#include <bits/stdc++.h>
using namespace std;
const long long int MAXN = 2e5 + 5;
long long int n, m, pm[MAXN], kd[MAXN];
vector<long long int> arr[MAXN];
long long int find(long long int val) {
  if (val == pm[val]) {
    return val;
  }
  return pm[val] = find(pm[val]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  while (t--) {
    cin >> n >> m;
    for (long long int i = 1; i <= n; i++) {
      cin >> kd[i];
      arr[kd[i]].push_back(i);
    }
    for (long long int i = 1; i <= m; i++) {
      pm[i] = i;
    }
    long long int cnt = 0;
    for (long long int i = 2; i <= n; i++) {
      if (kd[i] == kd[i - 1]) {
        cnt += 1;
      }
    }
    for (long long int i = 1; i < m; i++) {
      cout << n - 1 - cnt << '\n';
      long long int a, b;
      cin >> a >> b;
      long long int u = find(a);
      long long int v = find(b);
      if ((long long int)arr[u].size() > (long long int)arr[v].size()) {
        swap(u, v);
      }
      for (auto &it : arr[u]) {
        arr[v].push_back(it);
        if (kd[it - 1] == v) {
          cnt += 1;
        }
        if (kd[it + 1] == v) {
          cnt += 1;
        }
      }
      for (auto &it : arr[u]) {
        kd[it] = v;
      }
      pm[u] = v;
    }
    cout << "0\n";
  }
  return 0;
}
