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;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
const int INF = 2147483600;
const int MAXN = 400010;
int N, Q;
long long mn[MAXN << 2], mx[MAXN << 2], lmx[MAXN << 2], rmx[MAXN << 2],
    ans[MAXN << 2];
long long tag[MAXN << 2];
inline void update(int rt) {
  int ls = (rt << 1), rs = (rt << 1 | 1);
  mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
  if (mn[ls] < mn[rs]) {
    mn[rt] = mn[ls];
    lmx[rt] = lmx[ls];
    rmx[rt] = max(rmx[ls], mx[rs]);
    ans[rt] = ans[ls];
  } else if (mn[ls] > mn[rs]) {
    mn[rt] = mn[rs];
    rmx[rt] = rmx[rs];
    lmx[rt] = max(mx[ls], lmx[rs]);
    ans[rt] = ans[rs];
  } else {
    mn[rt] = mn[ls];
    ans[rt] = ans[ls] + ans[rs] + max(rmx[ls], lmx[rs]);
    lmx[rt] = lmx[ls];
    rmx[rt] = rmx[rs];
  }
  return;
}
inline void tage_lazy(int rt) {
  if (tag[rt]) {
    mn[rt << 1] += tag[rt];
    mn[rt << 1 | 1] += tag[rt];
    tag[rt << 1] += tag[rt];
    tag[rt << 1 | 1] += tag[rt];
    tag[rt] = 0;
  }
  return;
}
inline void Add(int rt, int l, int r, int L, int R, int x) {
  if (L <= l && R >= r) {
    tag[rt] += x;
    mn[rt] += x;
    return;
  }
  int mid = (l + r) >> 1;
  tage_lazy(rt);
  if (L <= mid) Add(rt << 1, l, mid, L, R, x);
  if (R > mid) Add(rt << 1 | 1, mid + 1, r, L, R, x);
  update(rt);
}
inline void mdf(int rt, int l, int r, int k, int x) {
  if (l == r) {
    lmx[rt] = x, rmx[rt] = (mn[rt] == 0 ? 0 : x), mx[rt] = x;
    return;
  }
  int mid = (l + r) >> 1;
  tage_lazy(rt);
  if (k <= mid)
    mdf(rt << 1, l, mid, k, x);
  else
    mdf(rt << 1 | 1, mid + 1, r, k, x);
  update(rt);
  return;
}
set<int> S[MAXN + 1];
inline void Erase(int k) {
  if (!S[k].size()) return;
  int l = *S[k].begin(), r = *S[k].rbegin();
  if (l <= r - 1) Add(1, 1, N - 1, l, r - 1, -1);
  mdf(1, 1, N - 1, l, 0);
}
inline void Insert(int k) {
  if (!S[k].size()) return;
  int l = *S[k].begin(), r = *S[k].rbegin();
  if (l <= r - 1) Add(1, 1, N - 1, l, r - 1, 1);
  mdf(1, 1, N - 1, l, S[k].size());
}
int a[MAXN + 1];
int main() {
  N = read(), Q = read();
  for (int i = 1; i <= N; i++) a[i] = read(), S[a[i]].insert(i);
  for (int i = 1; i <= N; i++) Insert(i);
  printf("%I64d\n", N - (mn[1] > 0 ? mx[1] : ans[1] + lmx[1] + rmx[1]) -
                        (S[a[N]].size() == 1));
  while (Q--) {
    int x = read(), v = read();
    int T = a[x];
    Erase(a[x]);
    Erase(v);
    S[a[x]].erase(S[a[x]].find(x));
    a[x] = v;
    S[v].insert(x);
    Insert(T);
    Insert(v);
    printf("%I64d\n", N - (mn[1] > 0 ? mx[1] : ans[1] + lmx[1] + rmx[1]) -
                          (S[a[N]].size() == 1));
  }
  return 0;
}