#include <bits/stdc++.h>
using namespace std;
struct re {
  int x, y;
  const bool operator<(const re &a) const {
    if (x != a.x) return x < a.x;
    return y < a.y;
  }
};
const int M = 1000000000 + 7;
const int N = 200000 + 10;
int n, m, a[N], p[N];
vector<int> g[N];
int r(int x) { return (p[x] == x) ? x : p[x] = r(p[x]); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = (1); (i <= (n)); ++i) {
    cin >> a[i];
    g[a[i]].push_back(i);
  }
  for (int i = (1); (i <= (m)); ++i) p[i] = i;
  int res = 0;
  for (int i = (1); (i <= (n - 1)); ++i) res += a[i] != a[i + 1];
  cout << res << endl;
  for (int i = (1); (i <= (m - 1)); ++i) {
    int u, v;
    cin >> u >> v;
    u = r(u);
    v = r(v);
    if (g[u].size() > g[v].size()) swap(u, v);
    p[u] = v;
    for (int x : g[u]) {
      if (x < n && a[x + 1] == v) --res;
      if (x > 1 && a[x - 1] == v) --res;
    }
    while (!g[u].empty()) {
      int x = g[u].back();
      g[u].pop_back();
      g[v].push_back(x);
      a[x] = v;
    }
    cout << res << endl;
  }
}
