#include <bits/stdc++.h>
using namespace std;
const int MN = 400012;
const int LOG = 20;
int n, m;
int a[MN];
vector<int> g[MN];
int rep[MN];
int pa[MN][LOG];
int res[MN];
int tin[MN], tout[MN], ctr = 0;
void dfs(int u, int p) {
  tin[u] = ctr++;
  pa[u][0] = p;
  for (int i = 1; i < LOG; i++) pa[u][i] = pa[pa[u][i - 1]][i - 1];
  for (int v : g[u]) {
    if (v == p) continue;
    dfs(v, u);
  }
  tout[u] = ctr++;
}
bool anc(int u, int v) { return tin[u] <= tin[v] and tout[u] >= tout[v]; }
int lca(int u, int v) {
  if (anc(u, v)) return u;
  if (anc(v, u)) return v;
  for (int i = LOG - 1; i >= 0; i--) {
    if (!anc(pa[u][i], v)) u = pa[u][i];
  }
  return pa[u][0];
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i > 0 and a[i] != a[i - 1]) res[0]++;
  }
  for (int i = 1; i < 2 * m; i++) rep[i] = i;
  int node = m + 1;
  for (int i = 0; i < m - 1; i++) {
    int u, v;
    cin >> u >> v;
    g[node].push_back(rep[u]);
    g[node].push_back(rep[v]);
    rep[u] = node;
    rep[v] = node;
    node++;
  }
  int root = 2 * m - 1;
  dfs(root, root);
  set<pair<int, int>> st;
  for (int i = 1; i < n; i++) {
    if (a[i] == a[i - 1]) continue;
    int node = lca(a[i], a[i - 1]);
    res[node - m]--;
  }
  for (int i = 0; i < m; i++) {
    if (i > 0) {
      res[i] += res[i - 1];
    }
    cout << res[i] << '\n';
  }
  return 0;
}
