#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> v[N];
int n, m, p[N], pre[N];
int f(int x) { return pre[x] == x ? x : pre[x] = f(pre[x]); }
signed main() {
  scanf("%d %d", &n, &m);
  for (register int i = (1); i <= (m); ++i) pre[i] = i;
  int ans = n - 1;
  for (register int i = (1); i <= (n); ++i) {
    int x;
    scanf("%d", &x);
    v[x].push_back(i);
    p[i] = x;
    ans -= (i > 1 && p[i] == p[i - 1]);
  }
  printf("%d\n", ans);
  for (register int i = (2); i <= (m); ++i) {
    int x, y, z;
    scanf("%d %d", &x, &y);
    x = f(x);
    y = f(y);
    if (v[x].size() > v[y].size()) swap(x, y);
    for (register int j = (0); j <= ((int)(v[x].size() - 1)); ++j) {
      z = v[x][j];
      ans -= (z > 1 && p[z - 1] == y);
      ans -= (z < n && p[z + 1] == y);
    }
    for (register int j = (0); j <= ((int)(v[x].size() - 1)); ++j) {
      z = v[x][j];
      v[y].push_back(z);
      p[z] = y;
    }
    pre[x] = y;
    printf("%d\n", ans);
  }
  return 0;
}
