#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200200;
int n, m, ans, idx[MAXN];
set<int> s[MAXN];
int main() {
  scanf("%d %d", &n, &m);
  ans = n - 1;
  for (int i = 1, t; i <= n; ++i) {
    scanf("%d", &t);
    idx[i] = i;
    s[t].insert(i);
    if (s[t].find(i - 1) != s[t].end()) ans--;
    if (s[t].find(i + 1) != s[t].end()) ans--;
  }
  printf("%d\n", ans);
  for (int i = 1, a, b; i < m; ++i) {
    scanf("%d %d", &a, &b);
    if (s[idx[a]].size() > s[idx[b]].size()) {
      for (const int& x : s[idx[b]]) {
        if (s[idx[a]].find(x - 1) != s[idx[a]].end()) ans--;
        if (s[idx[a]].find(x + 1) != s[idx[a]].end()) ans--;
      }
      s[idx[a]].insert(s[idx[b]].begin(), s[idx[b]].end());
    } else {
      for (const int& x : s[idx[a]]) {
        if (s[idx[b]].find(x - 1) != s[idx[b]].end()) ans--;
        if (s[idx[b]].find(x + 1) != s[idx[b]].end()) ans--;
      }
      s[idx[b]].insert(s[idx[a]].begin(), s[idx[a]].end());
      idx[a] = idx[b];
    }
    printf("%d\n", ans);
  }
}
