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 N = 200005;
int n, Q, a[N];
set<int> S[N];
int tg[N * 4], t[N * 4], mn[N * 4], mx[N * 4], lw[N * 4], rw[N * 4];
void pst(int k1, int k2) { tg[k1] += k2, mn[k1] += k2; }
void psd(int k1) {
  if (tg[k1]) {
    pst(k1 * 2, tg[k1]);
    pst(k1 * 2 + 1, tg[k1]);
    tg[k1] = 0;
  }
}
void upd(int k1) {
  mx[k1] = max(mx[k1 * 2], mx[k1 * 2 + 1]);
  mn[k1] = min(mn[k1 * 2], mn[k1 * 2 + 1]);
  if (mn[k1 * 2] < mn[k1 * 2 + 1]) {
    lw[k1] = lw[k1 * 2];
    rw[k1] = max(rw[k1 * 2], mx[k1 * 2 + 1]);
    t[k1] = t[k1 * 2];
  } else if (mn[k1 * 2] > mn[k1 * 2 + 1]) {
    lw[k1] = max(mx[k1 * 2], lw[k1 * 2 + 1]);
    rw[k1] = rw[k1 * 2 + 1];
    t[k1] = t[k1 * 2 + 1];
  } else {
    lw[k1] = lw[k1 * 2];
    rw[k1] = rw[k1 * 2 + 1];
    t[k1] = t[k1 * 2] + t[k1 * 2 + 1] + max(rw[k1 * 2], lw[k1 * 2 + 1]);
  }
}
void bud(int k1, int k2, int k3) {
  if (k2 == k3) {
    return;
  }
  int mid = (k2 + k3) >> 1;
  bud(k1 * 2, k2, mid), bud(k1 * 2 + 1, mid + 1, k3);
  upd(k1);
}
void chg(int k1, int k2, int k3, int k4, int k5) {
  if (k2 == k3) {
    mx[k1] = k5;
    lw[k1] = k5;
    rw[k1] = 0;
    t[k1] = 0;
    return;
  }
  psd(k1);
  int mid = (k2 + k3) >> 1;
  k4 <= mid ? chg(k1 * 2, k2, mid, k4, k5)
            : chg(k1 * 2 + 1, mid + 1, k3, k4, k5);
  upd(k1);
}
void add(int k1, int k2, int k3, int k4, int k5, int k6) {
  if (k2 > k5 || k3 < k4) return;
  if (k4 <= k2 && k3 <= k5) {
    pst(k1, k6);
    return;
  }
  psd(k1);
  int mid = (k2 + k3) >> 1;
  add(k1 * 2, k2, mid, k4, k5, k6), add(k1 * 2 + 1, mid + 1, k3, k4, k5, k6);
  upd(k1);
}
void ps(int x, int y) {
  if (((int)(S[x]).size()) == 0) return;
  chg(1, 1, n, *S[x].begin(), y > 0 ? ((int)(S[x]).size()) : 0);
  add(1, 1, n, *S[x].begin(), *--S[x].end() - 1, y);
}
void out(int k1, int k2, int k3) {
  if (k2 == k3) return;
  psd(k1);
  int mid = (k2 + k3) >> 1;
  out(k1 * 2, k2, mid), out(k1 * 2 + 1, mid + 1, k3);
}
int main() {
  scanf("%d%d", &n, &Q);
  for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]), S[a[i]].insert(i);
  bud(1, 1, n);
  for (int i = (1); i <= (N - 1); ++i) ps(i, 1);
  out(1, 1, n);
  for (int i = (0); i <= (Q); ++i) {
    if (i) {
      int pos, x;
      scanf("%d%d", &pos, &x);
      ps(a[pos], -1), S[a[pos]].erase(pos), ps(a[pos], 1);
      a[pos] = x;
      ps(a[pos], -1), S[a[pos]].insert(pos), ps(a[pos], 1);
    }
    printf("%d\n", n - t[1] - lw[1] - rw[1]);
  }
  return 0;
}