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;
const int sz = 4e5 + 10;
struct el {
  int l = 0, r = 0, su = 0, mi, ma, add = 0;
};
el dot[sz * 4];
void push(int nu) {
  dot[nu * 2].add += dot[nu].add;
  dot[nu * 2 + 1].add += dot[nu].add;
  dot[nu].add = 0;
}
void recalc(int nu) {
  int lmi = dot[nu * 2].mi + dot[nu * 2].add,
      rmi = dot[nu * 2 + 1].mi + dot[nu * 2 + 1].add;
  dot[nu].mi = min(lmi, rmi);
  dot[nu].ma = max(dot[nu * 2].ma, dot[nu * 2 + 1].ma);
  if (lmi < rmi) {
    dot[nu].l = dot[nu * 2].l;
    dot[nu].r = max(dot[nu * 2].r, dot[nu * 2 + 1].ma);
    dot[nu].su = dot[nu * 2].su;
  }
  if (lmi > rmi) {
    dot[nu].l = max(dot[nu * 2].ma, dot[nu * 2 + 1].l);
    dot[nu].r = dot[nu * 2 + 1].r;
    dot[nu].su = dot[nu * 2 + 1].su;
  }
  if (lmi == rmi) {
    dot[nu].l = dot[nu * 2].l;
    dot[nu].r = dot[nu * 2 + 1].r;
    dot[nu].su = dot[nu * 2].su + max(dot[nu * 2].r, dot[nu * 2 + 1].l) +
                 dot[nu * 2 + 1].su;
  }
}
int n, qq, ar[sz], q[sz];
void build(int nu, int l, int r) {
  if (l == r) {
    dot[nu].mi = ar[l], dot[nu].ma = q[l];
  } else {
    int mid = (l + r) >> 1;
    build(nu * 2, l, mid);
    build(nu * 2 + 1, mid + 1, r);
    recalc(nu);
  }
}
int lg, rg, va;
void cha(int nu, int l, int r) {
  if (l >= lg and r <= rg)
    dot[nu].add += va;
  else if (!(l > rg or r < lg)) {
    int mid = (l + r) >> 1;
    push(nu);
    cha(nu * 2, l, mid);
    cha(nu * 2 + 1, mid + 1, r);
    recalc(nu);
  }
}
int p;
void cha2(int nu, int l, int r) {
  if (l == r)
    dot[nu].ma = q[l];
  else {
    int mid = (l + r) >> 1;
    push(nu);
    if (p <= mid)
      cha2(nu * 2, l, mid);
    else
      cha2(nu * 2 + 1, mid + 1, r);
    recalc(nu);
  }
}
set<int> se[sz];
void f(int i, int ty) {
  if (se[i].size()) {
    int l = *se[i].begin();
    auto it = se[i].end();
    it--;
    int r = *it;
    lg = l * 2 + 1, rg = r * 2 + 1, va = ty, cha(1, 0, n * 2);
    q[l * 2 + 1] = (ty == 1) ? se[i].size() : 0, p = l * 2 + 1,
              cha2(1, 0, n * 2);
  }
}
int main() {
  cin >> n >> qq;
  int ch[n];
  for (int a = 0; a < n; a++) {
    scanf("%d", &ch[a]);
    se[ch[a]].insert(a);
  }
  for (int a = 0; a < sz; a++) {
    if (se[a].size()) {
      int l = *se[a].begin();
      auto it = se[a].end();
      it--;
      int r = *it;
      ar[l * 2 + 1]++, ar[r * 2 + 2]--;
      q[l * 2 + 1] = se[a].size();
    }
  }
  int cu = 0;
  for (int a = 0; a <= n * 2; a++) cu += ar[a], ar[a] = cu;
  build(1, 0, n * 2);
  printf("%d\n", n - dot[1].su);
  for (int a = 0; a < qq; a++) {
    int i, x;
    scanf("%d%d", &i, &x);
    i--;
    f(ch[i], -1);
    se[ch[i]].erase(i);
    f(ch[i], 1);
    ch[i] = x;
    f(ch[i], -1);
    se[ch[i]].insert(i);
    f(ch[i], 1);
    printf("%d\n", n - dot[1].su);
  }
}