#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int n, m;
  cin >> n >> m;
  long long int a[n + 1];
  for (long long int i = 1; i < n + 1; i++) cin >> a[i];
  std::vector<long long int> v[n + 1];
  for (long long int i = 1; i < n + 1; i++) {
    v[a[i]].push_back(i);
  }
  unordered_map<long long int, long long int> mp, transfer;
  for (long long int i = 1; i < n + 1; i++) transfer[i] = i;
  for (long long int i = 1; i < n + 1; i++) {
    for (long long int j : v[i]) {
      mp[j] = i;
    }
  }
  long long int ans = n - 1;
  for (long long int i = 2; i < n + 1; i++) {
    if (mp[i] == mp[i - 1]) ans--;
  }
  cout << ans << "\n";
  m--;
  while (m--) {
    long long int X, Y;
    cin >> X >> Y;
    long long int mn, mx;
    long long int x = transfer[X], y = transfer[Y];
    if (v[x].size() > v[y].size()) {
      mn = y;
      mx = x;
    } else {
      mn = x;
      mx = y;
    }
    if (mx == y) transfer[X] = mx;
    if (mx == x) transfer[Y] = mx;
    for (long long int z : v[mn]) {
      if (mp[z - 1] == mx) ans--;
      if (mp[z + 1] == mx) ans--;
      v[mx].push_back(z);
    }
    for (long long int z : v[mn]) mp[z] = mx;
    v[mn].clear();
    cout << ans << "\n";
  }
  return 0;
}
