#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, ans, p[N];
vector<int> v[N];
set<int> s[N];
int find_set(int x) { return p[x] == x ? x : p[x] = find_set(p[x]); }
void union_sets(int x, int y) {
  x = find_set(x);
  y = find_set(y);
  if (s[x].size() < s[y].size()) {
    swap(x, y);
  }
  p[y] = x;
  for (int p : s[y]) {
    if (s[x].find(p + 1) != s[x].end()) {
      --ans;
    }
    if (s[x].find(p - 1) != s[x].end()) {
      --ans;
    }
  }
  for (int i : s[y]) {
    s[x].insert(i);
  }
  s[y].clear();
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    v[x].push_back(i);
    p[i] = i;
    s[i].insert(i);
  }
  ans = n;
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j < v[i].size(); ++j) {
      union_sets(v[i][j], v[i][j - 1]);
    }
  }
  cout << ans - 1 << '\n';
  for (int i = 1; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    union_sets(v[x][0], v[y][0]);
    cout << ans - 1 << '\n';
  }
}
