#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector", 2, 3)
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
template <typename T>
void read(T &a) {
  T x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  a = x * f;
}
template <typename T>
void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x < 10) return (void)putchar(x + '0');
  write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void writeln(T x) {
  write(x);
  putchar('\n');
}
template <typename T>
void writes(T x) {
  write(x);
  putchar(' ');
}
template <typename T, typename... Args>
void read(T &t, Args &...args) {
  read(t);
  read(args...);
}
template <typename T, typename... Args>
void writes(T t, Args... args) {
  writes(t);
  writes(args...);
}
template <typename T, typename... Args>
void writeln(T t, Args... args) {
  writes(t);
  writes(args...);
  putchar('\n');
}
long long n, m, a[200005], fa[200005], ans = 0;
std::vector<long long> v[200005];
long long find(long long x) {
  long long u = x;
  while (u != fa[u]) {
    u = fa[u] = fa[fa[u]];
  }
  return u;
}
signed main() {
  read(n, m);
  for (long long i = 1; i <= n; i++) read(a[i]), v[a[i]].push_back(i);
  for (long long i = 1; i < n; i++)
    if (a[i] != a[i + 1]) ans++;
  for (long long i = 1; i <= m; i++) fa[i] = i;
  writeln(ans);
  for (long long i = 1; i < m; i++) {
    long long x, y;
    read(x, y);
    x = find(x);
    y = find(y);
    if (v[x].size() > v[y].size()) swap(x, y);
    for (std::vector<long long>::iterator it = v[x].begin(); it != v[x].end();
         it++) {
      if (find(a[*it - 1]) == y) ans--;
      if (find(a[*it + 1]) == y) ans--;
      v[y].push_back(*it);
    }
    fa[x] = y;
    writeln(ans);
  }
  return 0;
}
