#include <bits/stdc++.h>
using namespace std;
const int N = 210005;
int n, m, a[N], ans, fa[N];
vector<int> v[N];
int findfa(int x) { return fa[x] == x ? x : fa[x] = findfa(fa[x]); }
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    v[a[i]].push_back(i);
  }
  for (int i = 1; i <= m; ++i) fa[i] = i;
  for (int i = 1; i < n; ++i)
    if (a[i] != a[i + 1]) ++ans;
  printf("%d\n", ans);
  for (int i = 1, x, y; i < m; ++i) {
    scanf("%d%d", &x, &y);
    x = findfa(x), y = findfa(y);
    if (v[x].size() > v[y].size()) swap(x, y);
    fa[x] = y;
    for (int j : v[x]) {
      if (a[j - 1] == y) --ans;
      if (a[j + 1] == y) --ans;
      v[y].push_back(j);
    }
    for (int j : v[x]) a[j] = y;
    printf("%d\n", ans);
  }
  return 0;
}
