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>
const int N = 200005;
using namespace std;
set<int> col[N];
int seg[N * 3];
int mnv[N * 3];
int mxt[N * 3];
int tag[N * 3];
int lcon[N * 3];
int rcon[N * 3];
inline void _down(int rt) {
  if (tag[rt]) {
    mnv[(rt << 1)] += tag[rt];
    mnv[(rt << 1 | 1)] += tag[rt];
    tag[(rt << 1)] += tag[rt];
    tag[(rt << 1 | 1)] += tag[rt];
    tag[rt] = 0;
  }
}
inline void _up(int rt) {
  int ls = rt << 1;
  int rs = rt << 1 | 1;
  mxt[rt] = max(mxt[ls], mxt[rs]);
  mnv[rt] = min(mnv[ls], mnv[rs]);
  if (mnv[ls] < mnv[rs]) {
    seg[rt] = seg[ls];
    lcon[rt] = lcon[ls];
    rcon[rt] = max(mxt[rs], rcon[ls]);
  } else if (mnv[ls] > mnv[rs]) {
    seg[rt] = seg[rs];
    rcon[rt] = rcon[rs];
    lcon[rt] = max(mxt[ls], lcon[rs]);
  } else {
    lcon[rt] = lcon[ls];
    rcon[rt] = rcon[rs];
    seg[rt] = seg[ls] + seg[rs] + max(lcon[rs], rcon[ls]);
  }
}
void upd(int rt, int l, int r, int ul, int ur, int v) {
  if (ul > ur) return;
  if (ul <= l && r <= ur) return mnv[rt] += v, void(tag[rt] += v);
  int mid = (l + r) >> 1;
  _down(rt);
  if (ul <= mid) upd((rt << 1), l, mid, ul, ur, v);
  if (ur > mid) upd((rt << 1 | 1), mid + 1, r, ul, ur, v);
  _up(rt);
}
void cov(int rt, int l, int r, int p, int v) {
  if (l == r) return void(mxt[rt] = lcon[rt] = v);
  int mid = (l + r) >> 1;
  _down(rt);
  if (p <= mid)
    cov((rt << 1), l, mid, p, v);
  else
    cov((rt << 1 | 1), mid + 1, r, p, v);
  _up(rt);
}
int n, q;
int base[N];
void mdf(int c, int mk) {
  int w;
  if (!(w = col[c].size())) return;
  cov(1, 1, n, *col[c].begin(), mk > 0 ? w : 0);
  upd(1, 1, n, *col[c].begin(), *col[c].rbegin() - 1, mk);
}
int val_it() { return n - seg[1] - lcon[1] - rcon[1]; }
int main() {
  int x, y, z;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i) scanf("%d", &base[i]), col[base[i]].insert(i);
  for (int i = 1; i < N; ++i) mdf(i, 1);
  printf("%d\n", val_it());
  while (q--) {
    scanf("%d%d", &x, &y);
    z = base[x];
    mdf(z, -1);
    col[z].erase(x);
    mdf(z, 1);
    mdf(y, -1);
    col[y].insert(x);
    mdf(y, 1);
    printf("%d\n", val_it());
    base[x] = y;
  }
  return 0;
}