#include <bits/stdc++.h>
using namespace std;
class UnionFind {
  vector<int> p, rank;

 public:
  UnionFind(int n) : rank(n) {
    p.resize(n);
    for (int i = 0; i < n; i++) p[i] = i;
  }
  int find_set(int i) {
    if (p[i] == i)
      return p[i];
    else
      return p[i] = find_set(p[i]);
  }
  bool is_same_set(int i, int j) { return find_set(i) == find_set(j); }
  void merge_set(int i, int j) {
    int x = find_set(i), y = find_set(j);
    if (x == y) return;
    p[x] = y;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<int> t(n);
  vector<vector<int> > discs(m + 1);
  for (int i = 0; i < n; i++) {
    cin >> t[i];
    discs[t[i]].push_back(i);
  }
  int ans = 0;
  for (int i = 0; i + 1 < n; i++) {
    ans += t[i] != t[i + 1];
  }
  cout << ans << endl;
  UnionFind uf(m + 1);
  for (int j = 0; j < m - 1; j++) {
    int dec = 0;
    int a, b;
    cin >> a >> b;
    a = uf.find_set(a);
    b = uf.find_set(b);
    if (discs[a].size() < discs[b].size()) {
      uf.merge_set(a, b);
      swap(a, b);
    }
    for (auto& u : discs[b]) {
      if (u - 1 >= 0) {
        if (t[u - 1] != t[u] && t[u - 1] == a) ++dec;
      }
      if (u + 1 < n) {
        if (t[u + 1] != t[u] && t[u + 1] == a) ++dec;
      }
      discs[a].push_back(u);
    }
    for (auto& u : discs[b]) t[u] = a;
    discs[b].clear();
    ans -= dec;
    cout << ans << endl;
  }
  return 0;
}
