#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, i, j, k, ans, a, b, x, last, l, r, ll, rr;
  cin >> n >> m;
  vector<vector<pair<int, int>>> v(m + 1);
  for (i = 1; i <= n; i++) {
    cin >> x;
    if (v[x].empty())
      v[x].push_back({i, i});
    else if (v[x].back().second + 1 == i)
      v[x].back().second++;
    else
      v[x].push_back({i, i});
  }
  ans = 0;
  for (i = 1; i <= m; i++) ans += v[i].size();
  ans--;
  cout << ans << "\n";
  vector<set<pair<int, int>>> vv(m + 1);
  set<pair<int, int>>::iterator it;
  for (i = 1; i <= m; i++) {
    for (auto p : v[i]) vv[i].insert(p);
  }
  m--;
  while (m--) {
    cin >> a >> b;
    queue<pair<int, int>> res;
    last = -1;
    ans -= vv[a].size();
    ans -= vv[b].size();
    if (vv[a].size() < vv[b].size()) swap(vv[a], vv[b]);
    for (auto p : vv[b]) {
      l = p.first;
      r = p.second;
      it = vv[a].lower_bound({r + 1, 0});
      if ((it != vv[a].end()) && (it->first == r + 1)) {
        r = it->second;
        vv[a].erase(it);
      }
      it = vv[a].lower_bound({l, 0});
      if (it != vv[a].begin()) {
        it--;
        if (it->second == l - 1) {
          l = it->first;
          vv[a].erase(it);
        }
      }
      vv[a].insert({l, r});
    }
    vv[b].clear();
    ans += vv[a].size();
    cout << ans << "\n";
  }
  return 0;
}
