#include <bits/stdc++.h>
using namespace std;
map<int, int> mp[202000 + 5];
set<int> s[202020];
int num[202020];
int fa[202020];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int ans = 0;
  for (int i = 1; i <= m; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d", &t);
    ans++;
    mp[t][i] = 1;
    num[t]++;
    s[t].insert(i);
    if (mp[t][i - 1] == 1) num[t]--, ans--;
    if (mp[t][i + 1] == 1) num[t]--, ans--;
  }
  printf("%d\n", ans - 1);
  for (int i = 1; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u = find(u), v = find(v);
    if (mp[u].size() > mp[v].size()) swap(u, v);
    ans -= num[u];
    fa[u] = v;
    for (auto x : s[u]) {
      ans++;
      num[v]++;
      if (mp[v][x - 1]) num[v]--, ans--;
      if (mp[v][x + 1]) num[v]--, ans--;
      mp[v][x] = 1;
      s[v].insert(x);
    }
    mp[u].clear();
    s[u].clear();
    printf("%d\n", ans - 1);
  }
  return 0;
}
