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 MAX_N = 200000;
int n, q;
int a[MAX_N];
set<int> pos[MAX_N];
int lmx[4 * MAX_N];
int rmx[4 * MAX_N];
int smx[4 * MAX_N];
int mx[4 * MAX_N];
int mn[4 * MAX_N];
int lazy[4 * MAX_N];
void pushDown(int p) {
  mn[p << 1] += lazy[p];
  lazy[p << 1] += lazy[p];
  mn[p << 1 | 1] += lazy[p];
  lazy[p << 1 | 1] += lazy[p];
  lazy[p] = 0;
}
void pushUp(int p) {
  if (mn[p << 1] == mn[p << 1 | 1]) {
    lmx[p] = lmx[p << 1];
    rmx[p] = rmx[p << 1 | 1];
    smx[p] = smx[p << 1] + smx[p << 1 | 1] + max(rmx[p << 1], lmx[p << 1 | 1]);
  } else if (mn[p << 1] < mn[p << 1 | 1]) {
    lmx[p] = lmx[p << 1];
    rmx[p] = max(rmx[p << 1], mx[p << 1 | 1]);
    smx[p] = smx[p << 1];
  } else {
    lmx[p] = max(mx[p << 1], lmx[p << 1 | 1]);
    rmx[p] = rmx[p << 1 | 1];
    smx[p] = smx[p << 1 | 1];
  }
  mx[p] = max(mx[p << 1], mx[p << 1 | 1]);
  mn[p] = min(mn[p << 1], mn[p << 1 | 1]);
}
void modify(int p, int l, int r, int x, int y) {
  if (r - l == 1) {
    rmx[p] = y;
    mx[p] = y;
    return;
  }
  int mid = (l + r) >> 1;
  pushDown(p);
  if (x < mid) {
    modify(p << 1, l, mid, x, y);
  } else {
    modify(p << 1 | 1, mid, r, x, y);
  }
  pushUp(p);
}
void rangeAdd(int p, int l, int r, int x, int y, int z) {
  if (l >= y || r <= x) {
    return;
  }
  if (l >= x && r <= y) {
    mn[p] += z;
    lazy[p] += z;
    return;
  }
  int mid = (l + r) >> 1;
  pushDown(p);
  rangeAdd(p << 1, l, mid, x, y, z);
  rangeAdd(p << 1 | 1, mid, r, x, y, z);
  pushUp(p);
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    --a[i];
    pos[a[i]].insert(i);
  }
  for (int i = 0; i < 200000; ++i) {
    if (!pos[i].empty()) {
      modify(1, 0, n + 1, *pos[i].begin(), pos[i].size());
      rangeAdd(1, 0, n + 1, *pos[i].begin() + 1, *pos[i].rbegin() + 1, 1);
    }
  }
  printf("%d\n", n - smx[1]);
  for (int i = 0; i < q; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    --x;
    --y;
    modify(1, 0, n + 1, *pos[a[x]].begin(), 0);
    rangeAdd(1, 0, n + 1, *pos[a[x]].begin() + 1, *pos[a[x]].rbegin() + 1, -1);
    pos[a[x]].erase(x);
    if (!pos[a[x]].empty()) {
      modify(1, 0, n + 1, *pos[a[x]].begin(), pos[a[x]].size());
      rangeAdd(1, 0, n + 1, *pos[a[x]].begin() + 1, *pos[a[x]].rbegin() + 1, 1);
    }
    a[x] = y;
    if (!pos[y].empty()) {
      modify(1, 0, n + 1, *pos[y].begin(), 0);
      rangeAdd(1, 0, n + 1, *pos[y].begin() + 1, *pos[y].rbegin() + 1, -1);
    }
    pos[y].insert(x);
    modify(1, 0, n + 1, *pos[y].begin(), pos[y].size());
    rangeAdd(1, 0, n + 1, *pos[y].begin() + 1, *pos[y].rbegin() + 1, 1);
    printf("%d\n", n - smx[1]);
  }
  return 0;
}