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 MX = 200005;
struct {
  int L, R;
  int pl, pr;
  int left_maximum, right_maximum;
  int maximum;
  int sum_inside;
  int mn, debt;
} T[2 * MX];
void recalc(int cur) {
  if (T[T[cur].pl].mn == T[T[cur].pr].mn) {
    T[cur].left_maximum = T[T[cur].pl].left_maximum;
    T[cur].sum_inside =
        T[T[cur].pl].sum_inside + T[T[cur].pr].sum_inside +
        max(T[T[cur].pl].right_maximum, T[T[cur].pr].left_maximum);
    T[cur].right_maximum = T[T[cur].pr].right_maximum;
  } else if (T[T[cur].pl].mn < T[T[cur].pr].mn) {
    T[cur].left_maximum = T[T[cur].pl].left_maximum;
    T[cur].sum_inside = T[T[cur].pl].sum_inside;
    T[cur].right_maximum =
        max(T[T[cur].pl].right_maximum, T[T[cur].pr].maximum);
  } else {
    T[cur].left_maximum = max(T[T[cur].pl].maximum, T[T[cur].pr].left_maximum);
    T[cur].sum_inside = T[T[cur].pr].sum_inside;
    T[cur].right_maximum = T[T[cur].pr].right_maximum;
  }
  T[cur].mn = min(T[T[cur].pl].mn, T[T[cur].pr].mn);
  T[cur].maximum = max(T[T[cur].pl].maximum, T[T[cur].pr].maximum);
}
void push(int cur) {
  T[T[cur].pl].mn += T[cur].debt;
  T[T[cur].pl].debt += T[cur].debt;
  T[T[cur].pr].mn += T[cur].debt;
  T[T[cur].pr].debt += T[cur].debt;
  T[cur].debt = 0;
}
int A[MX];
void update(int cur) {
  int i = T[cur].L;
  T[cur].left_maximum = T[cur].maximum = A[i];
  T[cur].sum_inside = 0;
  T[cur].right_maximum = 0;
}
int buildTree(int L, int R) {
  static int tsz = 0;
  int cur = tsz++;
  T[cur].L = L;
  T[cur].R = R;
  if (L < R) {
    T[cur].pl = buildTree(L, (L + R) / 2);
    T[cur].pr = buildTree((L + R) / 2 + 1, R);
    recalc(cur);
  } else {
    update(cur);
  }
  return cur;
}
void add(int L, int R, int d, int cur) {
  if (L <= T[cur].L && R >= T[cur].R) {
    T[cur].mn += d;
    T[cur].debt += d;
    return;
  }
  push(cur);
  if (L <= T[T[cur].pl].R) add(L, R, d, T[cur].pl);
  if (R >= T[T[cur].pr].L) add(L, R, d, T[cur].pr);
  recalc(cur);
}
void upd(int i, int cur) {
  if (T[cur].L == T[cur].R) {
    update(cur);
    return;
  }
  push(cur);
  if (i <= T[T[cur].pl].R)
    upd(i, T[cur].pl);
  else
    upd(i, T[cur].pr);
  recalc(cur);
}
int a[MX];
set<int> positions[MX];
int rt;
void upd_interval(int v, int d) {
  if (!positions[v].empty()) {
    int L = *positions[v].begin();
    int R = *prev(positions[v].end()) - 1;
    if (L <= R) add(L, R, d, rt);
  }
}
int main() {
  int n, q;
  ignore = scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++) {
    ignore = scanf("%d", a + i);
    positions[a[i]].insert(i);
  }
  for (int v = 1; v < MX; v++) {
    if (!positions[v].empty()) {
      int L = *positions[v].begin();
      A[L] = (int)positions[v].size();
    }
  }
  rt = buildTree(1, n);
  for (int v = 1; v < MX; v++) {
    upd_interval(v, 1);
  }
  printf("%d\n", n - T[rt].sum_inside - T[rt].left_maximum);
  while (q--) {
    int i, x;
    ignore = scanf("%d %d", &i, &x);
    {
      upd_interval(a[i], -1);
      int L = *positions[a[i]].begin();
      A[L] = 0;
      upd(L, rt);
      positions[a[i]].erase(i);
      upd_interval(a[i], 1);
      if (!positions[a[i]].empty()) {
        L = *positions[a[i]].begin();
        A[L] = (int)positions[a[i]].size();
        upd(L, rt);
      }
    }
    a[i] = x;
    {
      upd_interval(x, -1);
      if (!positions[x].empty()) {
        int L = *positions[x].begin();
        A[L] = 0;
        upd(L, rt);
      }
      positions[x].insert(i);
      upd_interval(x, 1);
      int L = *positions[x].begin();
      A[L] = (int)positions[x].size();
      upd(L, rt);
    }
    printf("%d\n", n - T[rt].sum_inside - T[rt].left_maximum);
  }
}