#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class T>
inline void amin(T &x, const T &y) {
  if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
  if (x < y) x = y;
}
template <class Iter>
void rprintf(const char *fmt, Iter begin, Iter end) {
  for (bool sp = 0; begin != end; ++begin) {
    if (sp)
      putchar(' ');
    else
      sp = true;
    printf(fmt, *begin);
  }
  putchar('\n');
}
int N, M;
vector<int> S[200011];
int C[200011];
int name[200011];
void MAIN() {
  scanf("%d%d", &N, &M);
  for (int i = 0, i_len = (N); i < i_len; ++i) {
    int a;
    scanf("%d", &a);
    a--;
    S[a].push_back(i);
    C[i] = a;
  }
  int diff = 0;
  for (int i = 0, i_len = (N - 1); i < i_len; ++i)
    if (C[i] != C[i + 1]) diff++;
  for (int i = 0, i_len = (N); i < i_len; ++i) name[i] = i;
  printf("%d\n", diff);
  for (int $ = 0, $_len = (M - 1); $ < $_len; ++$) {
    int a_, b_;
    scanf("%d%d", &a_, &b_);
    a_--;
    b_--;
    int a = name[a_];
    int b = name[b_];
    if (S[a].size() < S[b].size()) {
      swap(a, b);
    }
    for (__typeof((S[b]).begin()) e = (S[b]).begin(), e_end = (S[b]).end();
         e != e_end; ++e) {
      if (*e && C[*e - 1] != C[*e]) diff--;
      if (*e + 1 < N && C[*e + 1] != C[*e]) diff--;
      C[*e] = a;
      if (*e && C[*e - 1] != C[*e]) diff++;
      if (*e + 1 < N && C[*e + 1] != C[*e]) diff++;
    }
    S[a].insert(S[a].end(), S[b].begin(), S[b].end());
    S[b] = vector<int>();
    name[a_] = a;
    printf("%d\n", diff);
  }
}
int main() {
  int TC = 1;
  for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN();
  return 0;
}
