#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = (long long)1000000007 * 1000000007;
const long double eps = 1e-8;
const long double pi = acos(-1.0);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n, m, t[200010];
set<int> se[200010];
int e = 0;
void merge(int a, int b) {
  if (se[a].size() < se[b].size()) {
    swap(se[a], se[b]);
  }
  for (int s : se[b]) {
    auto it1 = se[a].lower_bound(s + 1);
    if (it1 != se[a].end()) {
      if (*it1 == s + 1) e -= 1;
    }
    auto it2 = se[a].lower_bound(s - 1);
    if (it2 != se[a].end()) {
      if (*it2 == s - 1) e -= 1;
    }
  }
  auto it = se[b].begin();
  while (!se[b].empty()) {
    se[a].insert(*it);
    it = se[b].erase(it);
  }
}
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &t[i]);
    t[i]--;
    se[t[i]].insert(i);
  }
  for (int i = 0; i < n - 1; i++) {
    e += (t[i] != t[i + 1]);
  }
  cout << e << endl;
  for (int i = 0; i < m - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    merge(a, b);
    printf("%d\n", e);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(50);
  solve();
}
