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;
struct no {
  int mn, l, r, mx, a, d;
} a[1000010];
set<int> s[1000010 / 5];
int n, q, x[1000010];
no operator+(no x, no y) {
  no r;
  r.d = 0;
  r.mx = max(x.mx, y.mx);
  r.mn = min(x.mn, y.mn);
  if (x.mn < y.mn) {
    r.l = x.l;
    r.r = max(x.r, y.mx);
    r.a = x.a;
  } else if (x.mn > y.mn) {
    r.l = max(x.mx, y.l);
    r.r = y.r;
    r.a = y.a;
  } else {
    r.l = x.l;
    r.r = y.r;
    r.a = x.a + y.a + max(x.r, y.l);
  }
  return r;
}
void pd(int o) {
  int t = a[o].d;
  a[o * 2].d += t;
  a[o * 2 + 1].d += t;
  a[o * 2].mn += t;
  a[o * 2 + 1].mn += t;
  a[o].d = 0;
}
void ad(int o, int l, int r, int x, int y, int v) {
  if (r < x || y < l) return;
  if (x <= l && r <= y) {
    a[o].d += v;
    a[o].mn += v;
    return;
  }
  pd(o);
  int md = (l + r) / 2;
  ad(o * 2, l, md, x, y, v);
  ad(o * 2 + 1, md + 1, r, x, y, v);
  a[o] = a[o * 2] + a[o * 2 + 1];
}
void mod(int o, int l, int r, int x, int y) {
  if (l == r) {
    a[o].l = a[o].r = a[o].mx = y;
    return;
  }
  pd(o);
  int md = (l + r) / 2;
  x <= md ? mod(o * 2, l, md, x, y) : mod(o * 2 + 1, md + 1, r, x, y);
  a[o] = a[o * 2] + a[o * 2 + 1];
}
void cg(int v, int d) {
  if (!s[v].empty()) {
    if (s[v].size() > 1) ad(1, 0, n, *s[v].begin(), *s[v].rbegin() - 1, d);
    mod(1, 0, n, *s[v].begin(), (d == -1) ? 0 : s[v].size());
  }
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", x + i);
    s[x[i]].insert(i);
  }
  for (int i = 0; i < 1000010 / 5; i++) cg(i, 1);
  printf("%d\n", n - a[1].a);
  while (q--) {
    int c, d;
    scanf("%d%d", &c, &d);
    cg(x[c], -1);
    s[x[c]].erase(c);
    cg(x[c], 1);
    x[c] = d;
    cg(d, -1);
    s[d].insert(c);
    cg(d, 1);
    printf("%d\n", n - a[1].a);
  }
}