#include <bits/stdc++.h>
using namespace std;
template <typename T>
void pr(vector<T> &v) {
  for (int i = 0; i < (int)(v).size(); i++) cout << v[i] << " ";
  cout << endl;
}
template <typename T>
void pr(vector<vector<T>> &v) {
  for (int i = 0; i < (int)(v).size(); i++) {
    pr(v[i]);
  }
}
template <typename T>
void re(T &first) {
  cin >> first;
}
template <typename T>
void re(vector<T> &a) {
  for (int i = 0; i < (int)(a).size(); i++) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &...rest) {
  re(first);
  re(rest...);
}
template <typename T>
void pr(T first) {
  cout << first << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &...rest) {
  cout << first << " ";
  pr(rest...);
  cout << endl;
}
void ps() { cout << endl; }
template <class T, class... Ts>
void ps(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts)) cout << " ";
  ps(ts...);
}
const int N = 200043;
const int L = 20;
vector<int> chil[2 * N];
int p[2 * N];
int up[2 * N][L];
int psum[N];
int ind[N];
int tin[2 * N];
int tout[2 * N];
int T = 0;
void dfs(int first, int second) {
  tin[first] = T++;
  p[first] = second;
  up[first][0] = second;
  for (int i = 1; i < L; i++) up[first][i] = up[up[first][i - 1]][i - 1];
  for (auto z : chil[first]) dfs(z, first);
  tout[first] = T++;
}
bool is_ancestor(int first, int second) {
  return tin[first] <= tin[second] && tout[first] >= tout[second];
}
int lca(int first, int second) {
  if (is_ancestor(first, second)) return first;
  for (int i = L - 1; i >= 0; i--)
    if (!is_ancestor(up[first][i], second)) first = up[first][i];
  return p[first];
}
int main() {
  int n, m, a, b;
  re(n, m);
  vector<int> t(n);
  re(t);
  for (int i = 0; i < n; i++) {
    t[i]--;
  }
  for (int i = 0; i < m; i++) {
    ind[i] = i;
  }
  int cur = m;
  for (int i = 0; i < m - 1; i++) {
    int first, second;
    re(first, second);
    first--;
    second--;
    chil[cur].push_back(ind[first]);
    chil[cur].push_back(ind[second]);
    ind[first] = cur;
    cur++;
  }
  int root = m * 2 - 2;
  dfs(root, root);
  for (int i = 0; i < n - 1; i++) {
    int a = lca(t[i], t[i + 1]);
    if (a < m)
      psum[0]++;
    else
      psum[a - m + 1]++;
  }
  for (int i = 0; i < m - 1; i++) {
    psum[i + 1] += psum[i];
  }
  for (int i = 0; i < m; i++) {
    pr(n - 1 - psum[i]);
  }
}
