#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int n, m, t[MX];
int rep[MX];
vector<int> adj[2 * MX];
int d, mxd;
int tmr, bg[2 * MX], en[2 * MX];
int l, up[2 * MX][32];
int ans[MX];
void dfs(int v, int p) {
  mxd = max(mxd, d);
  bg[v] = tmr++;
  up[v][0] = p;
  ++d;
  for (auto& u : (adj[v]))
    if (u != p) dfs(u, v);
  --d;
  en[v] = tmr;
}
bool inline anc(int u, int v) { return bg[u] <= bg[v] && en[v] <= en[u]; }
int lca(int u, int v) {
  if (anc(u, v)) return u;
  if (anc(v, u)) return v;
  for (int i = (l + 1) - 1; i >= (0); --i)
    if (!anc(up[u][i], v)) u = up[u][i];
  return up[u][0];
}
void solve() {
  cin >> n >> m;
  for (int i = (0); i < (n); ++i) {
    cin >> t[i];
    --t[i];
  }
  for (int i = (0); i < (m); ++i) rep[i] = i;
  for (int j = (m); j < (2 * m - 1); ++j) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    adj[j].push_back(rep[a]);
    adj[j].push_back(rep[b]);
    rep[a] = j;
  }
  tmr = 0, d = 0;
  dfs(2 * m - 2, 2 * m - 2);
  l = 32 - __builtin_clz(mxd - 1);
  for (int i = (1); i < (l + 1); ++i)
    for (int v = (0); v < (2 * m - 1); ++v) up[v][i] = up[up[v][i - 1]][i - 1];
  memset((ans), 0, (m) * sizeof((ans)[0]));
  for (int i = (0); i < (n - 1); ++i) {
    int u = lca(t[i], t[i + 1]);
    if (u < m)
      ++ans[0];
    else
      ++ans[u - m + 1];
  }
  int p = 0;
  for (int i = (0); i < (m); ++i) {
    p += ans[i];
    cout << n - 1 - p << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}
