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;
int a[200000];
struct node {
  int max, lmax, rmax, smax, min;
};
node tree[1 << 19];
int lazy[1 << 19];
node com(node a, node b) {
  node c;
  c.max = max(a.max, b.max), c.min = min(a.min, b.min);
  c.lmax = (c.min == a.min) ? a.lmax : max(a.max, b.lmax);
  c.rmax = (c.min == b.min) ? b.rmax : max(b.max, a.rmax);
  if ((c.min == a.min) && (c.min == b.min))
    c.smax = a.smax + b.smax - min(a.rmax, b.lmax);
  else if (c.min == a.min)
    c.smax = a.smax + b.max - min(a.rmax, b.max);
  else
    c.smax = b.smax + a.max - min(b.lmax, a.max);
  return c;
}
int prop(int s, int e, int i) {
  tree[i].min += lazy[i];
  if (s != e) lazy[2 * i + 1] += lazy[i], lazy[2 * i + 2] += lazy[i];
  lazy[i] = 0;
  return 0;
}
int update(int s, int e, int as, int ae, int i, int num) {
  prop(s, e, i);
  if ((s > ae) || (e < as))
    return 0;
  else if ((s >= as) && (e <= ae)) {
    lazy[i] += num;
    prop(s, e, i);
    return 0;
  }
  int mid = (s + e) / 2;
  update(s, mid, as, ae, 2 * i + 1, num),
      update(mid + 1, e, as, ae, 2 * i + 2, num);
  tree[i] = com(tree[2 * i + 1], tree[2 * i + 2]);
  return 0;
}
int update2(int s, int e, int ai, int i, int num) {
  prop(s, e, i);
  if ((s > ai) || (e < ai - 1))
    return 0;
  else if (s == e) {
    if (s == ai)
      tree[i].lmax = num;
    else
      tree[i].rmax = num;
    tree[i].max = max(tree[i].lmax, tree[i].rmax),
    tree[i].smax = tree[i].lmax + tree[i].rmax;
    return 0;
  }
  int mid = (s + e) / 2;
  update2(s, mid, ai, 2 * i + 1, num), update2(mid + 1, e, ai, 2 * i + 2, num);
  tree[i] = com(tree[2 * i + 1], tree[2 * i + 2]);
  return 0;
}
set<int> pos[200001];
int main() {
  int i;
  int n, q, p, x;
  scanf("%d %d", &n, &q);
  for (i = 0; i < n; i++) scanf("%d", &a[i]), pos[a[i]].insert(i);
  if (n == 1) {
    for (i = 0; i <= q; i++) printf("1\n");
    return 0;
  }
  for (i = 1; i <= 200000; i++) {
    if (!pos[i].empty()) {
      update2(0, n - 2, *pos[i].begin(), 0, pos[i].size());
      if (pos[i].size() >= 2)
        update(0, n - 2, *pos[i].begin(), *(--pos[i].end()) - 1, 0, 1);
    }
  }
  prop(0, n - 2, 0);
  printf("%d\n", n - ((tree[0].min == 0) ? tree[0].smax : tree[0].max));
  for (i = 0; i < q; i++) {
    scanf("%d %d", &p, &x), p--;
    update2(0, n - 2, *pos[a[p]].begin(), 0, 0);
    if (pos[a[p]].size() >= 2)
      update(0, n - 2, *pos[a[p]].begin(), *(--pos[a[p]].end()) - 1, 0, -1);
    pos[a[p]].erase(p);
    if (!pos[a[p]].empty()) {
      update2(0, n - 2, *pos[a[p]].begin(), 0, pos[a[p]].size());
      if (pos[a[p]].size() >= 2)
        update(0, n - 2, *pos[a[p]].begin(), *(--pos[a[p]].end()) - 1, 0, 1);
    }
    a[p] = x;
    if (!pos[a[p]].empty()) {
      update2(0, n - 2, *pos[a[p]].begin(), 0, 0);
      if (pos[a[p]].size() >= 2)
        update(0, n - 2, *pos[a[p]].begin(), *(--pos[a[p]].end()) - 1, 0, -1);
    }
    pos[a[p]].insert(p);
    update2(0, n - 2, *pos[a[p]].begin(), 0, pos[a[p]].size());
    if (pos[a[p]].size() >= 2)
      update(0, n - 2, *pos[a[p]].begin(), *(--pos[a[p]].end()) - 1, 0, 1);
    prop(0, n - 2, 0);
    printf("%d\n", n - ((tree[0].min == 0) ? tree[0].smax : tree[0].max));
  }
  return 0;
}