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 segTree {
  int l, r;
  segTree *tl, *tr;
  int vv, lz, vx, vl, vr, vs;
  segTree(int l, int r) : l(l), r(r) {
    vv = lz = vx = vl = vr = vs = 0;
    if (l != r) {
      int mid = (l + r) / 2;
      tl = new segTree(l, mid);
      tr = new segTree(mid + 1, r);
    }
  }
  void upd(int v) { vv += v, lz += v; }
  void psh() { tl->upd(lz), tr->upd(lz), lz = 0; }
  void pul() {
    vv = min(tl->vv, tr->vv), vx = max(tl->vx, tr->vx);
    int x = tl->vv == vv, y = tr->vv == vv;
    vl = x ? tl->vl : max(tl->vx, tr->vl);
    vr = y ? tr->vr : max(tl->vr, tr->vx);
    vs = x * tl->vs + y * tr->vs + (x && y) * max(tl->vr, tr->vl);
  }
  void ad1(int x, int v) {
    if (x < l || r < x) return;
    if (l == r) return (void)(vx += v, vl += v, vr += v);
    psh();
    tl->ad1(x, v), tr->ad1(x, v);
    pul();
  }
  void ad2(int a, int b, int v) {
    if (b < l || r < a) return;
    if (a <= l && r <= b) return upd(v);
    psh();
    tl->ad2(a, b, v), tr->ad2(a, b, v);
    pul();
  }
  void dbg() {
    cout << l << " " << r << '\n';
    cout << vv << " " << lz << '\n';
    cout << vx << " " << vl << " " << vr << " " << vs << '\n';
    cout << '\n';
    if (l != r) psh(), tl->dbg(), tr->dbg();
  }
};
const int m = 400000;
int n, q;
int a[m];
set<int> second[m];
segTree tre(0, m);
void add(int x, int y) {
  if (!second[x].empty()) {
    tre.ad1(*second[x].begin(), y * second[x].size());
    tre.ad2(*second[x].begin(), *prev(second[x].end()), y);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    int x = (i << 1) | 1;
    cin >> a[x];
    second[--a[x]].insert(x);
  }
  for (int i = 0; i < m; i++) add(i, 1);
  cout << (n - tre.vs) << '\n';
  while (q--) {
    int x, y;
    cin >> x >> y;
    x = ((--x) << 1) | 1, y--;
    if (a[x] != y) {
      add(a[x], -1), add(y, -1);
      second[a[x]].erase(x);
      swap(a[x], y);
      second[a[x]].insert(x);
      add(a[x], 1), add(y, 1);
    }
    cout << (n - tre.vs) << '\n';
  }
  return 0;
}