#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int maxn = 2e5 + 3;
int n, m;
int a[maxn];
void read() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
}
int actual[maxn];
int ans = 0;
void assign(int idx, int val) {
  if (idx != 1 && actual[a[idx]] != actual[a[idx - 1]]) ans--;
  if (idx != n && actual[a[idx]] != actual[a[idx + 1]]) ans--;
  a[idx] = val;
  if (idx != 1 && actual[a[idx]] != actual[a[idx - 1]]) ans++;
  if (idx != n && actual[a[idx]] != actual[a[idx + 1]]) ans++;
}
vector<int> v[maxn];
void solve() {
  for (int i = 1; i <= m; i++) actual[i] = i;
  for (int i = 1; i <= n; i++) v[a[i]].push_back(i);
  for (int i = 1; i <= n - 1; i++)
    if (a[i] != a[i + 1]) ans++;
  cout << ans << '\n';
  for (int i = 1; i <= m - 1; i++) {
    int first, second;
    cin >> first >> second;
    bool is = false;
    if ((int)v[first].size() < (int)v[second].size()) {
      is = true;
      swap(first, second);
    }
    for (auto j : v[second]) {
      v[first].push_back(j);
      assign(j, first);
    }
    v[second].clear();
    if (is) swap(v[first], v[second]);
    actual[second] = actual[first];
    cout << ans << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  read();
  solve();
}
