#include <bits/stdc++.h>
using namespace std;
const long long INF = 2e9;
const long long MB = 20;
const long long MOD = 1e9 + 7;
const long long maxN = 101;
vector<vector<long long>> a;
vector<long long> p, s, t;
vector<bool> bad;
long long n, m;
long long now = 0;
void build() {
  p.resize(m);
  s.resize(m);
  for (long long i = 0; i < m; i++) {
    p[i] = i;
    s[i] = 1;
  }
}
long long find(long long x) {
  if (x == p[x]) return x;
  return p[x] = find(p[x]);
}
void unite(long long one, long long two) {
  one = find(one);
  two = find(two);
  if (s[one] < s[two]) swap(one, two);
  if (a[one].size() < a[two].size()) swap(a[one], a[two]);
  s[one] += s[two];
  p[two] = one;
  for (long long x : a[two]) {
    a[one].push_back(x);
    if (x && find(t[x - 1]) == one) {
      if (bad[x]) now--;
      bad[x] = false;
    }
    if (x + 1 < n && find(t[x + 1]) == one) {
      if (bad[x + 1]) now--;
      bad[x + 1] = false;
    }
  }
}
void solve() {
  cin >> n >> m;
  t.resize(n);
  for (long long i = 0; i < n; i++) {
    cin >> t[i];
    t[i]--;
  }
  a.resize(m);
  for (long long i = 0; i < n; i++) {
    a[t[i]].push_back(i);
  }
  bad.resize(n);
  for (long long i = 1; i < n; i++) {
    if (t[i] != t[i - 1]) {
      now++;
      bad[i] = true;
    }
  }
  cout << now << '\n';
  build();
  for (long long i = 0; i < m - 1; i++) {
    long long one, two;
    cin >> one >> two;
    one--;
    two--;
    unite(one, two);
    cout << now << '\n';
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed;
  cout.precision(12);
  srand(time(0));
  long long t = 1;
  while (t--) solve();
}
