#include <bits/stdc++.h>
using namespace std;
int32_t seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline int32_t rnd(int32_t l = 0, int32_t r = numeric_limits<int32_t>::max()) {
  return uniform_int_distribution<int32_t>(l, r)(rng);
}
template <typename T, typename G>
ostream &operator<<(ostream &out, const pair<T, G> &a) {
  return out << "( " << a.first << ", " << a.second << ")";
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[ ";
  for (const auto &i : a) out << i << ", ";
  return out << "]" << endl;
}
template <typename T>
ostream &operator<<(ostream &out, const set<T> &a) {
  out << "{ ";
  for (const auto &i : a) out << i << ", ";
  return out << "}" << endl;
}
template <typename T, typename G>
ostream &operator<<(ostream &out, const map<T, G> &a) {
  out << "< ";
  for (const auto &i : a) out << i << ", ";
  return out << ">" << endl;
}
template <typename T, size_t N>
typename enable_if<!is_same<typename remove_cv<T>::type, char>::value,
                   ostream &>::type
operator<<(ostream &out, T (&a)[N]) {
  out << "[ ";
  for (const auto &i : a) out << i << ", ";
  return out << "]" << endl;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << (H) << ' ';
  debug_out(T...);
}
class dsu {
 private:
  vector<int32_t> par, sz;
  void find_par(int32_t u) {
    if (par[par[u]] == u) return;
    find_par(par[u]);
    par[u] = par[par[u]];
  }

 public:
  int32_t connected;
  dsu() {
    par.resize(100005);
    sz.assign(100005, 1);
    iota(par.begin(), par.end(), 0);
    connected = 100005;
  }
  dsu(int32_t n) {
    par.resize(n + 1);
    sz.assign(n + 1, 1);
    iota(par.begin(), par.end(), 0);
    connected = n;
  }
  int32_t get_par(int32_t u) {
    find_par(u);
    return par[u];
  }
  int32_t get_size(int32_t u) { return sz[get_par(u)]; }
  void union_all(int32_t u, int32_t v) {
    if (get_par(u) != get_par(v)) {
      connected--;
      sz[par[u]] += sz[par[par[v]]];
      sz[par[par[v]]] = 0;
    }
    par[par[v]] = par[u];
  }
};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int32_t n, m, ans = 0;
  cin >> n >> m;
  vector<int32_t> id(n + 1, 0);
  vector<vector<int32_t> > discs(m + 1);
  for (int32_t i = 0; i < n; ++i) {
    cin >> id[i + 1];
    if (i) ans += (id[i] != id[i + 1]);
    discs[id[i + 1]].push_back(i + 1);
  }
  dsu tower(m);
  cout << ans << '\n';
  for (int32_t q = 1; q < m; ++q) {
    int32_t a, b;
    cin >> a >> b;
    a = tower.get_par(a);
    b = tower.get_par(b);
    if (tower.get_size(a) < tower.get_size(b)) swap(a, b);
    for (auto &pos : discs[b]) {
      if (pos < n) ans -= (id[pos + 1] == a);
      ans -= (id[pos - 1] == a);
    }
    for (auto &pos : discs[b]) {
      discs[a].push_back(pos);
      id[pos] = a;
    }
    tower.union_all(a, b);
    cout << ans << '\n';
  }
  return 0;
}
