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 cn[200010 << 2], mx[200010 << 2], val[200010 << 2], tag[200010 << 2],
    lc[200010 << 2], rc[200010 << 2];
void set_tag(int u, int v) {
  cn[u] += v;
  tag[u] += v;
}
void push_down(int u) {
  if (tag[u]) set_tag(u << 1, tag[u]), set_tag(u << 1 | 1, tag[u]);
  tag[u] = 0;
}
void update(int u) {
  cn[u] = min(cn[u << 1], cn[u << 1 | 1]),
  mx[u] = max(mx[u << 1], mx[u << 1 | 1]);
  lc[u] = lc[u << 1], rc[u] = rc[u << 1 | 1];
  if (cn[u << 1] < cn[u << 1 | 1])
    val[u] = val[u << 1], rc[u] = max(mx[u << 1 | 1], rc[u << 1]);
  else if (cn[u << 1] > cn[u << 1 | 1])
    val[u] = val[u << 1 | 1], lc[u] = max(mx[u << 1], lc[u << 1 | 1]);
  else
    val[u] = val[u << 1] + val[u << 1 | 1] + max(lc[u << 1 | 1], rc[u << 1]);
}
void insert(int u, int l, int r, int L, int R, int v) {
  if (L > R) return;
  if (L <= l && r <= R) {
    set_tag(u, v);
    return;
  }
  int mid = (l + r) >> 1;
  push_down(u);
  if (L <= mid) insert(u << 1, l, mid, L, R, v);
  if (R > mid) insert(u << 1 | 1, mid + 1, r, L, R, v);
  update(u);
}
void change(int u, int l, int r, int p, int v) {
  if (l == r) {
    lc[u] = mx[u] = v;
    return;
  }
  int mid = (l + r) >> 1;
  push_down(u);
  if (p <= mid)
    change(u << 1, l, mid, p, v);
  else
    change(u << 1 | 1, mid + 1, r, p, v);
  update(u);
}
int col[200010], n;
set<int> s[200010];
void del(int c) {
  if (!s[c].empty())
    change(1, 1, n, *s[c].begin(), 0),
        insert(1, 1, n, *s[c].begin(), *s[c].rbegin() - 1, -1);
}
void add(int c) {
  if (!s[c].empty())
    change(1, 1, n, *s[c].begin(), s[c].size()),
        insert(1, 1, n, *s[c].begin(), *s[c].rbegin() - 1, 1);
}
void chg(int x, int v) {
  int c = col[x];
  del(c);
  s[c].erase(x);
  add(c);
  col[x] = c = v;
  del(c);
  s[c].insert(x);
  add(c);
}
int x[200010], y[200010];
int main() {
  int q, m = 0;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &col[i]), s[col[i]].insert(i), m = max(m, col[i]);
  for (int i = 1; i <= q; i++) scanf("%d%d", &x[i], &y[i]), m = max(m, y[i]);
  for (int i = 1; i <= m + 1; i++) add(i);
  printf("%d\n", n - val[1] - lc[1] - rc[1]);
  for (int i = 1; i <= q; i++)
    chg(x[i], y[i]), printf("%d\n", n - val[1] - lc[1] - rc[1]);
  return 0;
}