#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> v[200005];
int to[200005];
set<pair<int, int> > s[200005];
int num = 0;
set<pair<int, int> >::iterator iter, it, itt;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int t;
    cin >> t;
    v[t].push_back(i);
  }
  for (int i = 1; i <= m; ++i) {
    to[i] = i;
    int lst = 0;
    for (int j = 1; j < v[i].size(); ++j) {
      if (v[i][j] - v[i][j - 1] != 1) {
        s[i].insert(make_pair(v[i][lst], v[i][j - 1]));
        lst = j;
      }
    }
    s[i].insert(make_pair(v[i][lst], v[i].back()));
    num += s[i].size();
  }
  cout << num - 1 << endl;
  for (int i = 1; i <= m - 1; ++i) {
    int x, y;
    cin >> x >> y;
    if (s[to[x]].size() < s[to[y]].size()) swap(to[x], to[y]);
    x = to[x];
    y = to[y];
    for (iter = s[y].begin(); iter != s[y].end(); ++iter) {
      pair<int, int> p = *iter;
      it = s[x].insert(p).first;
      if (it != s[x].begin()) {
        itt = it;
        --itt;
        if (it->first - itt->second == 1) {
          int nf = itt->first;
          ++it;
          s[x].erase(itt, it);
          it = s[x].insert(p = make_pair(nf, p.second)).first;
          --num;
        }
      }
      itt = it;
      ++itt;
      if (itt != s[x].end() && itt->first - it->second == 1) {
        int ns = itt->second;
        ++itt;
        s[x].erase(it, itt);
        s[x].insert(make_pair(p.first, ns));
        --num;
      }
    }
    cout << num - 1 << endl;
    s[y].clear();
  }
  return 0;
}
