#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
bool contains(const set<T> &s, T elem) {
  return s.find(elem) != s.end();
}
const ll N = 2e5;
set<ll> towers[N + 1];
ll parent[N + 1];
ll getparent(ll tower) {
  if (tower == parent[tower]) {
    return tower;
  }
  return parent[tower] = getparent(parent[tower]);
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  ll numdisks, numtowers;
  cin >> numdisks >> numtowers;
  for (ll i = 1; i <= numdisks; i++) {
    ll t;
    cin >> t;
    towers[t].insert(i);
  }
  ll totalcost = 0;
  for (ll i = 1; i <= numtowers; i++) {
    parent[i] = i;
    for (const auto &disk : towers[i]) {
      if (!contains(towers[i], disk + 1)) {
        totalcost++;
      }
    }
  }
  cout << totalcost - 1 << "\n";
  for (ll i = 1; i <= numtowers - 1; i++) {
    ll a, b;
    cin >> a >> b;
    ll smallp = getparent(a);
    ll bigp = getparent(b);
    if (towers[smallp].size() > towers[bigp].size()) {
      swap(smallp, bigp);
    }
    ll lowered = 0;
    for (const auto &disk : towers[smallp]) {
      if (contains(towers[bigp], disk + 1)) {
        lowered++;
      }
      if (contains(towers[bigp], disk - 1)) {
        lowered++;
      }
    }
    for (const auto &disk : towers[smallp]) {
      towers[bigp].insert(disk);
    }
    parent[smallp] = bigp;
    totalcost -= lowered;
    cout << totalcost - 1 << "\n";
  }
  return 0;
}
