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>
template <typename _Tp>
void read(_Tp &x) {
  char ch(getchar());
  bool f(false);
  while (!isdigit(ch)) f |= ch == 45, ch = getchar();
  x = ch & 15, ch = getchar();
  while (isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar();
  if (f) x = -x;
}
template <typename _Tp, typename... Args>
void read(_Tp &t, Args &...args) {
  read(t);
  read(args...);
}
const int N = 200005;
int a[N], d[N], cnt[N], v[N], n, q;
int tag[N << 2], mn[N << 2], mx1[N << 2], mx2[N << 2], ans[N << 2], mx[N << 2];
inline void pushup(int u) {
  int x = ((u) << 1), y = ((u) << 1 | 1);
  mn[u] = std::min(mn[x], mn[y]), mx[u] = std::max(mx[x], mx[y]);
  if (mn[x] == mn[y]) {
    ans[u] = ans[x] + ans[y] + std::max(mx2[x], mx1[y]);
    mx1[u] = mx1[x], mx2[u] = mx2[y];
  } else if (mn[x] < mn[y]) {
    ans[u] = ans[x];
    mx2[u] = std::max(mx2[x], mx[y]);
    mx1[u] = mx1[x];
  } else {
    ans[u] = ans[y];
    mx1[u] = std::max(mx1[y], mx[x]);
    mx2[u] = mx2[y];
  }
}
void build(int l, int r, int u) {
  if (l == r) {
    mn[u] = d[l], mx1[u] = v[l], mx2[u] = 0, ans[u] = 0, mx[u] = v[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, ((u) << 1)), build(mid + 1, r, ((u) << 1 | 1));
  pushup(u);
}
inline void upd(int u, int C) { mn[u] += C, tag[u] += C; }
inline void pushdown(int u) {
  if (tag[u]) upd(((u) << 1), tag[u]), upd(((u) << 1 | 1), tag[u]), tag[u] = 0;
}
void Update(int L, int R, int C, int l, int r, int u) {
  if (L <= l && r <= R) return upd(u, C);
  int mid = (l + r) >> 1;
  pushdown(u);
  if (L <= mid) Update(L, R, C, l, mid, ((u) << 1));
  if (R > mid) Update(L, R, C, mid + 1, r, ((u) << 1 | 1));
  pushup(u);
}
void Modify(int pos, int l, int r, int u) {
  if (l == r) {
    mx1[u] = v[l], mx2[u] = 0, ans[u] = 0, mx[u] = v[l];
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(u);
  pos <= mid ? Modify(pos, l, mid, ((u) << 1))
             : Modify(pos, mid + 1, r, ((u) << 1 | 1));
  pushup(u);
}
std::set<int> S[N];
void del(int x) {
  int l = *S[a[x]].begin(), r = *S[a[x]].rbegin();
  v[r] = 0, Modify(r, 0, n, 1);
  if (l < r) Update(l, r - 1, -1, 0, n, 1);
  S[a[x]].erase(S[a[x]].find(x)), --cnt[a[x]];
  if (!S[a[x]].empty()) {
    int l = *S[a[x]].begin(), r = *S[a[x]].rbegin();
    v[r] = cnt[a[x]], Modify(r, 0, n, 1);
    if (l < r) Update(l, r - 1, 1, 0, n, 1);
  }
}
void ins(int x) {
  if (!S[a[x]].empty()) {
    int l = *S[a[x]].begin(), r = *S[a[x]].rbegin();
    v[r] = 0, Modify(r, 0, n, 1);
    if (l < r) Update(l, r - 1, -1, 0, n, 1);
  }
  S[a[x]].insert(x), ++cnt[a[x]];
  int l = *S[a[x]].begin(), r = *S[a[x]].rbegin();
  v[r] = cnt[a[x]], Modify(r, 0, n, 1);
  if (l < r) Update(l, r - 1, 1, 0, n, 1);
}
int main() {
  read(n, q);
  for (int i = 1; i <= n; ++i) read(a[i]), S[a[i]].insert(i), ++cnt[a[i]];
  for (int i = 1; i <= 200000; ++i)
    if (!S[i].empty()) {
      int l = *S[i].begin(), r = *S[i].rbegin();
      ++d[l], --d[r], v[r] = cnt[i];
    }
  for (int i = 1; i <= n; ++i) d[i] += d[i - 1];
  build(0, n, 1);
  printf("%d\n", n - ans[1]);
  int x, y;
  while (q--) {
    read(x, y);
    del(x), a[x] = y, ins(x);
    printf("%d\n", n - ans[1]);
  }
  return 0;
}