#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200010;
const int inf = 2147483647;
const double pi = acos(-1.0);
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
int n, m, ans = 0, a[Maxn], c[Maxn];
set<int> S[Maxn];
void insert(int x, int v) {
  int t = 0;
  if (S[x].find(v - 1) != S[x].end()) t++;
  if (S[x].find(v + 1) != S[x].end()) t++;
  if (t == 2) ans--, c[x]--;
  if (t == 0) ans++, c[x]++;
  S[x].insert(v);
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++) a[i] = i;
  for (int i = 1; i <= n; i++) insert(read(), i);
  m--;
  printf("%d\n", ans - 1);
  while (m--) {
    int x = read(), y = read();
    if (S[a[x]].size() < S[a[y]].size()) swap(a[x], a[y]);
    ans -= c[a[y]];
    for (set<int>::iterator it = S[a[y]].begin(); it != S[a[y]].end(); it++)
      insert(a[x], (*it));
    S[a[y]].clear();
    c[a[y]] = 0;
    a[a[x]] = a[a[y]] = a[x];
    printf("%d\n", ans - 1);
  }
}
