#include <bits/stdc++.h>
using namespace std;
template <typename T>
string to_string(const vector<T>& vc, int w) {
  if (vc.empty()) return "";
  if (w + 1 == vc.size()) return to_string(vc[w]);
  return to_string(vc[w]) + "," + to_string(vc, w + 1);
}
template <typename T>
string to_string(const vector<T>& vc) {
  return "{" + to_string(vc, 0) + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
class DebugStream {
} LOG;
template <typename T>
DebugStream& operator<<(DebugStream& s, const T&) {
  return s;
}
const int MAX = 1001000;
int t[MAX];
int p[MAX], sz[MAX], tower[MAX];
int total;
void init(int n) {
  total = n;
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    sz[i] = 1;
    tower[i] = t[i];
  }
}
int getS(int u) { return (u == p[u] ? u : (p[u] = getS(p[u]))); }
void join(int u, int v) {
  u = getS(u);
  v = getS(v);
  if (u == v) return;
  p[u] = v;
  sz[v] += sz[u];
  total--;
}
vector<int> TS[MAX];
int idxs[MAX];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", t + i);
    TS[t[i]].push_back(i);
  }
  for (int i = 1; i <= m; i++) idxs[i] = i;
  init(n);
  for (int i = 2; i <= n; i++) {
    if (tower[i - 1] == tower[i]) {
      join(i - 1, i);
    }
  }
  printf("%d\n", total - 1);
  for (int i = 0; i < int(m - 1); i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    int x = idxs[a];
    int y = idxs[b];
    if (TS[x].size() > TS[y].size()) swap(x, y);
    for (int z : TS[x]) {
      TS[y].push_back(z);
      tower[z] = y;
      if (z > 1 && tower[z - 1] == tower[z]) {
        join(z - 1, z);
      }
      if (z < n && tower[z + 1] == tower[z]) {
        join(z, z + 1);
      }
    }
    idxs[a] = y;
    printf("%d\n", total - 1);
  }
}
