#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int N, M;
int a[maxn], par[maxn];
vector<int> vec[maxn];
inline int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M;
  for (int i = 1; i <= N; i++) cin >> a[i], vec[a[i]].push_back(i);
  for (int i = 1; i <= M; i++) par[i] = i;
  int ans = 0;
  for (int i = 2; i <= N; i++)
    if (a[i] != a[i - 1]) ans++;
  cout << ans << "\n";
  for (int i = 1; i <= M - 1; i++) {
    int x, y;
    cin >> x >> y;
    x = find(x), y = find(y);
    if (vec[x].size() > vec[y].size()) swap(x, y);
    par[x] = y;
    for (auto j : vec[x]) {
      if (a[j - 1] == y) ans--;
      if (a[j + 1] == y) ans--;
      vec[y].push_back(j);
    }
    for (auto j : vec[x]) a[j] = y;
    cout << ans << "\n";
  }
  return 0;
}
