#include <bits/stdc++.h>
using namespace std;
const int MM = 200013;
vector<int> S[MM];
int dic[MM];
int T[MM], curr = 0;
void Merge(int a, int b) {
  if (S[dic[b]].size() > S[dic[a]].size()) swap(dic[a], dic[b]);
  a = dic[a];
  b = dic[b];
  for (const int &v : S[b]) {
    if (T[v + 1] == a) curr--;
    if (T[v - 1] == a) curr--;
    S[a].push_back(v);
  }
  for (const int &v : S[b]) T[v] = a;
  S[b].clear();
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = (1); i <= (n); ++i) {
    cin >> T[i];
    S[T[i]].push_back(i);
    dic[i] = i;
  }
  for (int i = (1); i <= (n - 1); ++i)
    if (T[i + 1] != T[i]) curr++;
  cout << curr << "\n";
  for (int i = (1); i <= (m - 1); ++i) {
    int a, b;
    cin >> a >> b;
    Merge(a, b);
    cout << curr << "\n";
  }
}
