#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int inf = 1e9 + 10;
const long long INF = 1e18;
const long double EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
struct UnionFind {
  vector<long long> par;
  vector<long long> siz;
  UnionFind(long long sz_) : par(sz_), siz(sz_, 1) {
    for (long long i = 0; i < sz_; ++i) par[i] = i;
  }
  void init(long long sz_) {
    par.clear();
    par.resize(sz_);
    siz.clear();
    siz.resize(sz_, 1);
    for (long long i = 0; i < sz_; ++i) par[i] = i;
  }
  long long root(long long x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(long long x, long long y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (siz[x] < siz[y]) swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }
  bool issame(long long x, long long y) { return root(x) == root(y); }
  long long size(long long x) { return siz[root(x)]; }
};
set<int> s[202020];
UnionFind uf(202020);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(25);
  int n, m;
  cin >> n >> m;
  vector<int> t(n);
  for (int i = 0; i < (n); ++i) {
    cin >> t[i];
    t[i]--;
    s[t[i]].insert(i);
  }
  int ans = 0;
  for (int i = 0; i < (n - 1); ++i)
    if (t[i] != t[i + 1]) ans++;
  cout << ans << '\n';
  for (int _ = 0; _ < (m - 1); ++_) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    a = uf.root(a);
    b = uf.root(b);
    if (s[a].size() > s[b].size()) swap(a, b);
    for (auto x : s[a]) {
      if (s[b].count(x - 1)) ans--;
      if (s[b].count(x + 1)) ans--;
    }
    for (auto x : s[a]) s[b].insert(x);
    s[a].clear();
    uf.merge(a, b);
    if (uf.root(a) == a) swap(s[a], s[b]);
    cout << ans << '\n';
  }
  return 0;
}
