#include <bits/stdc++.h>
using namespace std;
long long Min(long long a, long long b) { return (a < b) ? a : b; }
long long Max(long long a, long long b) { return (a < b) ? b : a; }
long long gcd(long long m, long long n) {
  if (n == 0) return m;
  return gcd(n, m % n);
}
long long lcm(long long m, long long n) { return m * n / gcd(m, n); }
long long idx[500005], v[500005];
vector<long long> child[500005];
long long p[500005][24], level[500005];
long long ans[500005];
void dfs(long long node, long long par) {
  p[node][0] = par;
  level[node] = level[par] + 1;
  for (int i = 1; i <= 20; i++) {
    p[node][i] = p[p[node][i - 1]][i - 1];
  }
  for (auto t : child[node]) {
    dfs(t, node);
  }
}
long long lca(long long a, long long b) {
  if (a == b) return a;
  if (level[a] < level[b]) {
    swap(a, b);
  }
  long long diff = level[a] - level[b], cnt = 0;
  while (diff) {
    if (diff & 1) {
      a = p[a][cnt];
    }
    cnt++, diff >>= 1;
  }
  for (int i = 20; i >= 0; i--) {
    if (p[a][i] != p[b][i]) {
      a = p[a][i], b = p[b][i];
    }
  }
  return p[a][0];
}
void solve() {
  long long n, m, root;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> idx[i];
    idx[i]--;
  }
  for (int i = 0; i < m; i++) v[i] = i;
  for (int i = 0; i < m - 1; i++) {
    long long s, e;
    cin >> s >> e;
    s--, e--;
    child[m + i].push_back(v[s]);
    child[m + i].push_back(v[e]);
    v[s] = m + i;
  }
  root = 2 * m - 2;
  dfs(root, root);
  for (int i = 0; i < n - 1; i++) {
    long long a = idx[i], b = idx[i + 1];
    long long x = lca(a, b);
    if (x - m < 0) continue;
    ans[x - m]++;
  }
  for (int i = m; i > 0; i--) {
    ans[i - 1] += ans[i];
  }
  for (int i = 0; i < m; i++) cout << ans[i] << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long tc = 1;
  while (tc--) solve();
  return 0;
}
