#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
int n, m, cnt, a, b;
set<int> st[200009];
int pos[200009], boss[200009];
map<pair<int, int>, int> mp;
int fun(int v) {
  if (boss[v] != v) boss[v] = fun(boss[v]);
  return boss[v];
}
void gun(int bo, int v) { boss[v] = bo; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> pos[i];
    st[pos[i]].insert(i);
  }
  for (int i = 1; i <= m; i++) {
    boss[i] = i;
  }
  for (int i = n; i > 1; i--) {
    if (pos[i] != pos[i - 1]) cnt++;
  }
  cout << cnt << "\n";
  for (int i = 0; i < m - 1; i++) {
    cin >> a >> b;
    a = fun(a);
    b = fun(b);
    if (st[a].size() < st[b].size()) {
      gun(b, a);
      swap(a, b);
    }
    for (auto x : st[b]) {
      if (pos[x - 1] == a && st[b].count(x - 1) == 0) cnt--;
      if (pos[x + 1] == a && st[b].count(x + 1) == 0) cnt--;
      pos[x] = a;
      st[a].insert(x);
    }
    cout << cnt << "\n";
  }
  return 0;
}
