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;
int n, i, a[200005], lazy[1000005], mn[1000005], sum[1000005], mx[1000005],
    mxr[1000005], mxl[1000005], x, y, q;
set<int> p[200005];
void pushup(int i) {
  if (mn[i << 1] < mn[i << 1 | 1]) {
    mn[i] = mn[i << 1];
    sum[i] = sum[i << 1] + max(mxr[i << 1], mx[i << 1 | 1]) - mxr[i << 1];
    mxr[i] = max(mxr[i << 1], mx[i << 1 | 1]);
    mxl[i] = mxl[i << 1];
  }
  if (mn[i << 1] > mn[i << 1 | 1]) {
    mn[i] = mn[i << 1 | 1];
    sum[i] =
        sum[i << 1 | 1] - mxl[i << 1 | 1] + max(mxl[i << 1 | 1], mx[i << 1]);
    mxl[i] = max(mxl[i << 1 | 1], mx[i << 1]);
    mxr[i] = mxr[i << 1 | 1];
  }
  if (mn[i << 1] == mn[i << 1 | 1]) {
    mn[i] = mn[i << 1];
    mxl[i] = mxl[i << 1];
    mxr[i] = mxr[i << 1 | 1];
    sum[i] = sum[i << 1] + sum[i << 1 | 1] - mxl[i << 1 | 1] - mxr[i << 1] +
             max(mxl[i << 1 | 1], mxr[i << 1]);
  }
  mx[i] = max(mx[i << 1], mx[i << 1 | 1]);
}
void pushdown(int i) {
  lazy[i << 1] += lazy[i];
  lazy[i << 1 | 1] += lazy[i];
  mn[i << 1] += lazy[i];
  mn[i << 1 | 1] += lazy[i];
  lazy[i] = 0;
}
void modify(int i, int l, int r, int ll, int rr, int x) {
  if (l >= ll && r <= rr) {
    lazy[i] += x;
    mn[i] += x;
    return;
  }
  pushdown(i);
  int mid = l + r >> 1;
  if (mid >= ll) modify(i << 1, l, mid, ll, rr, x);
  if (mid < rr) modify(i << 1 | 1, mid + 1, r, ll, rr, x);
  pushup(i);
}
void Add(int i, int l, int r, int x, int y) {
  if (l == r) {
    mx[i] += y;
    mxl[i] += y;
    sum[i] += y;
    return;
  }
  pushdown(i);
  int mid = l + r >> 1;
  if (mid >= x)
    Add(i << 1, l, mid, x, y);
  else
    Add(i << 1 | 1, mid + 1, r, x, y);
  pushup(i);
}
int main() {
  scanf("%d %d", &n, &q);
  for (i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    p[a[i]].insert(i);
  }
  for (i = 1; i <= 200000; ++i)
    if (!p[i].empty()) {
      if (p[i].size() != 1)
        modify(1, 1, n, *p[i].begin(), *(--p[i].end()) - 1, 1);
      Add(1, 1, n, *p[i].begin(), p[i].size());
    }
  printf("%d\n", n - sum[1]);
  while (q--) {
    scanf("%d %d", &x, &y);
    if (p[a[x]].size() > 1)
      modify(1, 1, n, *p[a[x]].begin(), *(--p[a[x]].end()) - 1, -1);
    if (p[y].size() > 1)
      modify(1, 1, n, *p[y].begin(), *(--p[y].end()) - 1, -1);
    Add(1, 1, n, *p[a[x]].begin(), -p[a[x]].size());
    if (!p[y].empty()) Add(1, 1, n, *p[y].begin(), -p[y].size());
    p[a[x]].erase(x);
    p[y].insert(x);
    if (p[a[x]].size() > 1)
      modify(1, 1, n, *p[a[x]].begin(), *(--p[a[x]].end()) - 1, 1);
    if (p[y].size() > 1) modify(1, 1, n, *p[y].begin(), *(--p[y].end()) - 1, 1);
    if (!p[a[x]].empty()) Add(1, 1, n, *p[a[x]].begin(), p[a[x]].size());
    Add(1, 1, n, *p[y].begin(), p[y].size());
    printf("%d\n", n - sum[1]);
    a[x] = y;
  }
}