#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long N = 2e5 + 100;
long long par[N];
long long getPar(long long v) {
  return v == par[v] ? v : par[v] = getPar(par[v]);
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < N; i++) {
    par[i] = i;
  }
  vector<long long> pos(n), deg(n);
  vector<vector<long long>> kek(m);
  for (long long i = 0; i < n; i++) {
    cin >> pos[i];
    pos[i]--;
    deg[pos[i]]++;
    kek[pos[i]].push_back(i);
  }
  auto f = [&]() {
    long long ret = 0;
    for (long long i = 1; i < n; i++) {
      ret += pos[i] != pos[i - 1];
    }
    return ret;
  };
  long long ans = f();
  cout << ans << '\n';
  for (long long i = 1; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    a--;
    b--;
    long long c = a, d = b;
    bool sw = 0;
    if ((long long)kek[c].size() < (long long)kek[d].size()) {
      swap(c, d);
      sw = 1;
    }
    for (auto x : kek[d]) {
      if (x) ans -= getPar(pos[x - 1]) == c;
      if (x != n - 1) ans -= getPar(pos[x + 1]) == c;
    }
    kek[c].insert(kek[c].end(), kek[d].begin(), kek[d].end());
    kek[d].clear();
    par[b] = a;
    if (sw) swap(kek[c], kek[d]);
    cout << ans << '\n';
  }
}
