#include <bits/stdc++.h>
using namespace std;
vector<int> dis[(int)2e5 + 10];
int parent[(int)2e5 + 10];
int main() {
  int n, m;
  cin >> n >> m;
  unordered_map<int, int> kv;
  for (int i = 1; i <= n; i++) {
    kv[i] = i;
    int x;
    cin >> x;
    dis[x].push_back(i);
    parent[i] = x;
  }
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (parent[i + 1] != parent[i]) {
      ans++;
    }
  }
  cout << ans << "\n";
  m -= 1;
  while (m--) {
    int a, b, a1, b1;
    cin >> a1 >> b1;
    a = kv[a1];
    b = kv[b1];
    if (dis[a].size() < dis[b].size()) {
      for (int j : dis[a]) {
        if (parent[j + 1] == b) {
          ans--;
        }
        if (parent[j - 1] == b) {
          ans--;
        }
      }
      for (int j : dis[a]) {
        parent[j] = b;
        dis[b].push_back(j);
      }
      kv[a1] = b;
    } else {
      for (int j : dis[b]) {
        if (parent[j + 1] == a) {
          ans--;
        }
        if (parent[j - 1] == a) {
          ans--;
        }
      }
      for (int j : dis[b]) {
        parent[j] = a;
        dis[a].push_back(j);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
