#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
vector<int> v[maxn];
int b[maxn], f[maxn], ans;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &b[i]);
    v[b[i]].push_back(i);
    f[i] = i;
  }
  for (int i = 1; i < n; ++i) {
    if (b[i] != b[i + 1]) {
      ans++;
    }
  }
  printf("%d\n", ans);
  for (int i = 1; i < m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (v[f[x]].size() < v[f[y]].size()) {
      swap(x, y);
    }
    for (int j : v[f[y]]) {
      v[f[x]].push_back(j);
      if (j > 1) {
        ans = ans - (b[j] != b[j - 1]) + (f[x] != b[j - 1]);
      }
      if (j < n) {
        ans = ans - (b[j] != b[j + 1]) + (f[x] != b[j + 1]);
      }
      b[j] = f[x];
    }
    v[f[y]].clear();
    f[y] = f[x];
    printf("%d\n", ans);
  }
}
