#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 100;
const int INF = 1e9;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> t(n);
  for (int i = 0; i < n; i++) cin >> t[i];
  int ans = 0;
  for (int i = 1; i < n; i++)
    if (t[i] != t[i - 1]) ans++;
  vector<vector<int>> b(m);
  for (int i = 0; i < n; i++) b[t[i] - 1].emplace_back(i);
  cout << ans << '\n';
  vector<int> pos(n, 0);
  iota(pos.begin(), pos.end(), 0);
  vector<int> col(n, -1);
  for (int i = 0; i < m - 1; i++) {
    int az, bz;
    cin >> az >> bz;
    az--, bz--;
    int j = az;
    int k = bz;
    if (b[k].size() > b[j].size()) {
      swap(b[k], b[j]);
      swap(pos[k], pos[j]);
    }
    int o = pos[j] + 1;
    for (auto w : b[k]) {
      b[j].emplace_back(w);
      t[w] = o;
      col[w] = i;
      if (w > 0 && t[w - 1] == o && col[w - 1] < i) ans--;
      if (w < n - 1 && t[w + 1] == o && col[w + 1] < i) ans--;
    }
    cout << ans << '\n';
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int Q;
  Q = 1;
  while (Q--) solve();
  return 0;
}
