Problem: This is a harder version of the problem. In this version q ≤ 200 000.

A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal.

Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value.

You are given a sequence of integers a_1, a_2, …, a_n and q updates.

Each update is of form "i x" — change a_i to x. Updates are not independent (the change stays for the future).

Print the difficulty of the initial sequence and of the sequence after every update.

Input

The first line contains integers n and q (1 ≤ n ≤ 200 000, 0 ≤ q ≤ 200 000), the length of the sequence and the number of the updates.

The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000), the initial sequence.

Each of the following q lines contains integers i_t and x_t (1 ≤ i_t ≤ n, 1 ≤ x_t ≤ 200 000), the position and the new value for this position.

Output

Print q+1 integers, the answer for the initial sequence and the answer after every update.

Example

Input


5 6
1 2 1 2 1
2 1
4 1
5 3
2 3
4 2
2 1


Output


2
1
0
0
2
3
0

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
struct TRes {
  int mina;
  int gmx, lmx, rmx, smx;
  TRes operator+(TRes r) const {
    if (mina < r.mina)
      return {mina, max(gmx, r.gmx), lmx, max(rmx, r.gmx), smx};
    else if (mina > r.mina)
      return {r.mina, max(gmx, r.gmx), max(gmx, r.lmx), r.rmx, r.smx};
    else
      return {mina, max(gmx, r.gmx), lmx, r.rmx, smx + max(rmx, r.lmx) + r.smx};
  }
};
struct TNode {
  int L, R;
  int l, r;
  int val;
  TRes res;
};
const int maxn = 410000;
int tc = 0;
TNode t[maxn];
int n, q;
int buildTree(int L, int R) {
  int node = tc++;
  TNode &n = t[node];
  n = {L, R, -1, -1, 0, {0, 0, 0, 0, 0}};
  if (R - L > 1) {
    int M = (L + R) / 2;
    n.l = buildTree(L, M);
    n.r = buildTree(M, R);
  }
  return node;
}
void addval(int node, int val) {
  TNode &n = t[node];
  n.val += val;
  n.res.mina += val;
}
void push(int node) {
  TNode &n = t[node];
  addval(n.l, n.val);
  addval(n.r, n.val);
  n.val = 0;
}
void addseg(int node, int L, int R, int delta) {
  TNode &n = t[node];
  if (max(L, n.L) >= min(R, n.R)) return;
  if (L <= n.L && n.R <= R) {
    addval(node, delta);
    return;
  }
  push(node);
  addseg(n.l, L, R, delta);
  addseg(n.r, L, R, delta);
  n.res = t[n.l].res + t[n.r].res;
}
void changenum(int node, int i, int x) {
  TNode &n = t[node];
  if (i < n.L || i >= n.R) return;
  if (n.R - n.L == 1) {
    n.res.lmx = n.res.gmx = x;
    return;
  }
  push(node);
  changenum(n.l, i, x);
  changenum(n.r, i, x);
  n.res = t[n.l].res + t[n.r].res;
}
int a[maxn];
set<int> occs[maxn];
int root;
void update_num(int x, bool undo) {
  if (occs[x].empty()) return;
  addseg(root, *occs[x].begin(), *occs[x].rbegin(), undo ? -1 : 1);
  changenum(root, *occs[x].begin(), undo ? 0 : occs[x].size());
}
void print(int node) {
  if (node == -1) return;
  TNode &n = t[node];
  cerr << n.L << ' ' << n.R << ' ' << n.res.mina << ' ' << n.res.gmx << ' '
       << n.res.lmx << ' ' << n.res.rmx << ' ' << n.res.smx << '\n';
  print(n.l);
  print(n.r);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  cin >> n >> q;
  for (int i = 1; i <= (int)(n); ++i) {
    cin >> a[i];
    occs[a[i]].insert(i);
  }
  root = buildTree(0, n + 2);
  for (int i = 0; i < (int)(maxn); ++i) update_num(i, false);
  cout << n - t[root].res.smx << '\n';
  for (int i = 0; i < (int)(q); ++i) {
    int p, x;
    cin >> p >> x;
    if (x != a[p]) {
      update_num(x, true);
      update_num(a[p], true);
      occs[a[p]].erase(p);
      occs[x].insert(p);
      update_num(x, false);
      update_num(a[p], false);
      a[p] = x;
    }
    auto r = t[root].res;
    assert(r.mina == 0 && r.lmx == 0 && r.rmx == 0);
    cout << n - t[root].res.smx << '\n';
  }
  return 0;
}