#include <bits/stdc++.h>
using namespace std;
vector<long long> sieve;
void Sieve(int N) {
  const long long maxn = N;
  sieve.resize(maxn);
  for (long long(i) = 0; (i) < (maxn); ++(i)) sieve[i] = i;
  sieve[1] = -1;
  sieve[0] = -1;
  for (long long(i) = 2; (i) <= (maxn); (i) += (1))
    if (i == sieve[i])
      for (long long j = 2 * i; j < maxn; j += i)
        if (sieve[j] == j) sieve[j] = i;
}
long long extended_GCD(long long a, long long b, long long &x, long long &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  long long x1, y1;
  long long gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
long long power(long long a, long long b, long long m = 1000000007) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long modinv(long long a, long long mod = 1000000007) {
  long long x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0) x += mod;
  return x;
}
long long parent[200099];
long long find_set(long long v) {
  if (v == parent[v]) return v;
  return parent[v] = find_set(parent[v]);
}
void solve() {
  long long n;
  cin >> n;
  long long m;
  cin >> m;
  long long a[n];
  vector<long long> v[m];
  for (int i = 0; i < m; ++i) {
    parent[i] = i;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    a[i]--;
    v[a[i]].push_back(i);
  }
  long long ans = n - 1;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < v[i].size() - 1; ++j) {
      if (v[i][j] == v[i][j + 1] - 1) {
        ans--;
      }
    }
  }
  cout << ans << "\n";
  for (int i = 0; i < m - 1; ++i) {
    long long x, y;
    cin >> x >> y;
    x--;
    y--;
    x = find_set(x);
    y = find_set(y);
    if (v[x].size() > v[y].size()) swap(x, y);
    for (int j = 0; j < v[x].size(); ++j) {
      if (v[x][j] != 0) {
        if (a[v[x][j] - 1] == y) ans--;
        if (a[v[x][j] + 1] == y) ans--;
      } else {
        if (a[v[x][j] + 1] == y) ans--;
      }
    }
    for (int j = 0; j < v[x].size(); ++j) {
      v[y].push_back(v[x][j]);
      a[v[x][j]] = y;
    }
    parent[x] = y;
    cout << ans << "\n";
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
