#include <bits/stdc++.h>
using namespace std;
vector<int> ert, inds;
int meg = -1;
void Add(set<int> &bin, int ind, int x) {
  meg -= ert[ind];
  ert[ind]++;
  auto it = bin.insert(x).first;
  if (next(it) != bin.end() && (*next(it)) == x + 1) ert[ind]--;
  if (it != bin.begin() && (*prev(it)) == x - 1) ert[ind]--;
  meg += ert[ind];
  return;
}
bool Unio(set<int> &bin1, int a, set<int> &bin2, int b) {
  if (bin1.size() < bin2.size()) {
    for (int x : bin1) Add(bin2, b, x);
    meg -= ert[a];
    bin1.clear();
    return true;
  }
  for (int x : bin2) Add(bin1, a, x);
  meg -= ert[b];
  bin2.clear();
  return false;
}
int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  inds.resize(m);
  ert.resize(m, 0);
  vector<set<int>> v(m);
  for (int i = 0; i < m; i++) inds[i] = i;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    Add(v[a - 1], a - 1, i);
  }
  cout << meg << "\n";
  for (int i = 0; i < m - 1; i++) {
    int a, b;
    cin >> a >> b;
    if (Unio(v[inds[a - 1]], inds[a - 1], v[inds[b - 1]], inds[b - 1]))
      swap(inds[a - 1], inds[b - 1]);
    cout << meg << "\n";
  }
  return 0;
}
