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>
#pragma GCC optimize "-O3"
#pragma GCC optimize("Ofast,no-stack-protector")
using namespace std;
int a[200200], b[200200];
pair<int, int> p[200200];
set<int> s[200200];
int vst[200200];
int id[200200], R[200200], val[200200];
int q[200200];
int fir[200200], last[200200];
int mxx[200200][18];
int cnt[200200];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i), s[a[i]].insert(i);
    cnt[a[i]]++;
    if (!fir[a[i]]) fir[a[i]] = i;
    last[a[i]] = i;
  }
  for (int i = 0; i < m; i++) scanf("%d %d", &p[i].first, &p[i].second);
  int M = 900, runs = 0, ok = 0;
  if (m == 0) p[m++] = pair<int, int>(1, a[1]), ok = 1;
  for (int i = 0; i < m; i += M) {
    int ed = min(m, i + M);
    for (int j = 1; j <= n; j++) b[j] = a[j];
    runs++;
    register int nn = 0;
    for (int j = i; j < ed; j++) {
      vst[b[p[j].first]] = runs;
      q[nn++] = b[p[j].first];
      vst[b[p[j].first] = p[j].second] = runs;
      q[nn++] = p[j].second;
    }
    int cur = 0, pre = 0, now = 0, mx = 0, pregood = 0, good;
    for (int i = 1, j = 1; i <= n; i = j, pregood = pre) {
      if (vst[a[i]] == runs) {
        id[i] = -cur - 1;
        j++;
        continue;
      }
      mx = 0, now = i, cur++;
      good = 1;
      while (j <= now) {
        id[j] = cur;
        if (vst[a[j]] != runs) {
          mx = max(mx, cnt[a[j]]);
          now = max(now, last[a[j]]);
        } else
          good = 0;
        j++;
      }
      if (pregood && good) {
        cur--;
        val[cur] = max(val[cur], mx);
        for (int k = R[cur] + 1; k <= now; k++) id[k] = cur;
        R[cur] = now;
        continue;
      }
      val[cur] = mx;
      R[cur] = now;
    }
    for (int i = 1; i <= cur; i++) mxx[i][0] = val[i];
    int L = 31 - __builtin_clz(cur);
    for (int j = 1; j <= L; j++) {
      for (int i = 1; i <= cur; i++) {
        mxx[i][j] = max(mxx[i][j - 1], mxx[min(i + (1 << j - 1), cur)][j - 1]);
      }
    }
    for (int i = 1; i <= cur; i++) val[i] += val[i - 1];
    sort(q, q + nn);
    nn = unique(q, q + nn) - q;
    sort(q, q + nn, [&](int a, int b) { return fir[a] < fir[b]; });
    if (i == 0) {
      int sum = 0;
      int pre = 0;
      for (register int i = 0, j = 0; i < nn; i = j) {
        if (fir[q[i]] == 0) {
          j++;
          continue;
        }
        int now = last[q[i]], mx = 0;
        while (1) {
          while (j < nn && now >= fir[q[j]])
            now = max(now, last[q[j]]), mx = max(mx, cnt[q[j]]), j++;
          if (id[now] < 0 || now == R[id[now]]) break;
          now = R[id[now]];
        }
        int st = id[fir[q[i]]] < 0 ? -id[fir[q[i]]] : id[fir[q[i]]];
        int edd = id[now] < 0 ? -id[now] - 1 : id[now];
        sum += val[st - 1] - val[pre];
        if (edd >= st) {
          int l = 31 - __builtin_clz(edd - st + 1);
          mx = max(mx, max(mxx[st][l], mxx[edd - (1 << l) + 1][l]));
        }
        sum += mx;
        pre = edd;
      }
      sum += val[cur] - val[pre];
      printf("%d\n", n - sum);
    }
    if (ok) return 0;
    for (register int j = i; j < ed; j++) {
      int pos = p[j].first, va = p[j].second;
      s[a[pos]].erase(pos);
      cnt[a[pos]]--;
      int pp = fir[a[pos]];
      if (s[a[pos]].empty())
        fir[a[pos]] = last[a[pos]] = 0;
      else
        fir[a[pos]] = *s[a[pos]].begin(), last[a[pos]] = *s[a[pos]].rbegin();
      if (pp != fir[a[pos]]) {
        int k;
        for (k = 0; k < nn; k++)
          if (q[k] == a[pos]) break;
        while (k + 1 < nn && fir[q[k + 1]] < fir[q[k]])
          swap(q[k], q[k + 1]), k++;
        while (k && fir[q[k - 1]] > fir[q[k]]) swap(q[k], q[k - 1]), k--;
      }
      a[pos] = va;
      s[va].insert(pos);
      cnt[va]++;
      pp = fir[va];
      fir[va] = *s[va].begin(), last[va] = *s[va].rbegin();
      if (pp != fir[va]) {
        int k;
        for (k = 0; k < nn; k++)
          if (q[k] == va) break;
        while (k + 1 < nn && fir[q[k + 1]] < fir[q[k]])
          swap(q[k], q[k + 1]), k++;
        while (k && fir[q[k - 1]] > fir[q[k]]) swap(q[k], q[k - 1]), k--;
      }
      int sum = 0;
      int pre = 0;
      for (register int i = 0, j = 0; i < nn; i = j) {
        if (fir[q[i]] == 0) {
          j++;
          continue;
        }
        int now = last[q[i]], mx = 0;
        while (1) {
          while (j < nn && now >= fir[q[j]])
            now = max(now, last[q[j]]), mx = max(mx, cnt[q[j]]), j++;
          if (id[now] < 0 || now == R[id[now]]) break;
          now = R[id[now]];
        }
        int st = id[fir[q[i]]] < 0 ? -id[fir[q[i]]] : id[fir[q[i]]];
        int edd = id[now] < 0 ? -id[now] - 1 : id[now];
        sum += val[st - 1] - val[pre];
        if (edd >= st) {
          int l = 31 - __builtin_clz(edd - st + 1);
          mx = max(mx, max(mxx[st][l], mxx[edd - (1 << l) + 1][l]));
        }
        sum += mx;
        pre = edd;
      }
      sum += val[cur] - val[pre];
      printf("%d\n", n - sum);
    }
  }
  return 0;
}