#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int oo = 2e9;
set<int> t[N];
int n, m, ans, x, y, a[N], pos[N];
void mrg(int x, int y) {
  int prv = -1;
  while (!t[y].empty()) {
    int now = *t[y].rbegin();
    t[y].erase(now);
    t[x].insert(now);
    if (now + 1 != prv) {
      if (t[x].find(now + 1) != t[x].end()) --ans;
    }
    if (t[x].find(now - 1) != t[x].end()) --ans;
    prv = now;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) pos[i] = i;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    t[a[i] - 1].insert(i);
  }
  for (int i = n - 2; i >= 0; --i)
    if (a[i] != a[i + 1]) ++ans;
  cout << ans << '\n';
  for (int i = 1; i < m; ++i) {
    cin >> x >> y;
    --x;
    --y;
    if (t[pos[x]].size() < t[pos[y]].size()) swap(pos[x], pos[y]);
    mrg(pos[x], pos[y]);
    cout << ans << '\n';
  }
}
