#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int inf = 1000000000;
const long long inf_ll = (long long)1 << 60;
inline 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 = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline long long readll() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long power(long long x, long long y) {
  long long t = 1;
  for (; y; y >>= 1, x = x * x % (1000000007))
    if (y & 1) t = t * x % (1000000007);
  return t;
}
int n, m, b[(500000 + 5)], fa[(500000 + 5)];
set<int> s[(500000 + 5)];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  n = read();
  m = read();
  for (int i = (1); i <= (m); i++) fa[i] = i;
  for (int i = (1); i <= (n); i++) {
    int x = read();
    s[x].insert(i);
    b[i] = x;
  }
  int ans = n - 1;
  for (int i = (n); i >= (2); i--)
    if (b[i] == b[i - 1]) ans--;
  printf("%d\n", ans);
  m--;
  while (m--) {
    int x = read(), y = read();
    x = find(x);
    y = find(y);
    if (s[x].size() > s[y].size()) swap(x, y);
    for (auto z : s[x]) {
      if (z != 1 && b[z - 1] == y) ans--;
      if (z != n && b[z + 1] == y) ans--;
    }
    for (auto z : s[x]) {
      b[z] = y;
      s[y].insert(z);
    }
    fa[x] = y;
    printf("%d\n", ans);
  }
  return 0;
}
