#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, res;
int a[N], f[N], root[N];
vector<pair<int, int> > d;
vector<int> h[N + 1];
int findRoot(int u) {
  while (root[u] > 0) u = root[u];
  return u;
}
void process(int x, int y) {
  if (f[x] < f[y])
    process(y, x);
  else {
    root[y] = x;
    for (int i = 0; i < (int)h[y].size(); i++) {
      int k = h[y][i];
      if (d[k].first == y) {
        if (d[k].second == x)
          res--;
        else {
          f[x]++;
          d[k].first = x;
          h[x].push_back(k);
        }
      } else {
        if (d[k].first == x)
          res--;
        else {
          f[x]++;
          d[k].second = x;
          h[x].push_back(k);
        }
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i < n; i++)
    if (a[i] != a[i + 1]) {
      res++;
      d.push_back(make_pair(a[i], a[i + 1]));
      h[a[i]].push_back(res - 1);
      f[a[i]]++;
      h[a[i + 1]].push_back(res - 1);
      f[a[i + 1]]++;
    }
  cout << res << "\n";
  for (int i = 1; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x = findRoot(x);
    y = findRoot(y);
    process(x, y);
    cout << res << "\n";
  }
  return 0;
}
