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;
template <typename T>
void read(T &t) {
  t = 0;
  char ch = getchar();
  int f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  do {
    (t *= 10) += ch - '0';
    ch = getchar();
  } while ('0' <= ch && ch <= '9');
  t *= f;
}
template <typename T>
void write(T t) {
  if (t < 0) {
    putchar('-');
    write(-t);
    return;
  }
  if (t > 9) write(t / 10);
  putchar('0' + t % 10);
}
template <typename T>
void writeln(T t) {
  write(t);
  puts("");
}
const int maxn = (8e5) + 10;
int n, q, a[maxn], d[maxn];
set<int> s[maxn];
int len[maxn], cnt[maxn], ans[maxn];
int lazy[maxn], mx[maxn];
int mn[maxn], L[maxn], R[maxn];
void pushup(int root) {
  mx[root] = max(mx[root << 1], mx[root << 1 | 1]);
  mn[root] = min(mn[root << 1], mn[root << 1 | 1]);
  if (mn[root] == mn[root << 1])
    L[root] = L[root << 1];
  else
    L[root] = max(mx[root << 1], L[root << 1 | 1]);
  if (mn[root] == mn[root << 1 | 1])
    R[root] = R[root << 1 | 1];
  else
    R[root] = max(mx[root << 1 | 1], R[root << 1]);
  if (mn[root] != mn[root << 1 | 1])
    ans[root] = ans[root << 1], cnt[root] = cnt[root << 1] + len[root << 1 | 1];
  else if (mn[root] != mn[root << 1])
    ans[root] = ans[root << 1 | 1],
    cnt[root] = cnt[root << 1 | 1] + len[root << 1];
  else {
    ans[root] = ans[root << 1] + ans[root << 1 | 1],
    cnt[root] = cnt[root << 1] + cnt[root << 1 | 1];
    if (R[root << 1] != -1 || L[root << 1 | 1] != -1)
      ans[root] += max(R[root << 1], L[root << 1 | 1]), cnt[root]++;
  }
}
void build(int l, int r, int root) {
  len[root] = r - l + 1;
  L[root] = R[root] = -1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(l, mid, root << 1), build(mid + 1, r, root << 1 | 1);
  pushup(root);
}
void pushdown(int root) {
  if (lazy[root]) {
    mn[root << 1] += lazy[root], mn[root << 1 | 1] += lazy[root];
    lazy[root << 1] += lazy[root], lazy[root << 1 | 1] += lazy[root];
    lazy[root] = 0;
  }
}
void update(int x, int l, int r, int root) {
  if (l == r) {
    mx[root] = max(d[x], d[x + 1]);
    return;
  }
  pushdown(root);
  int mid = (l + r) >> 1;
  if (x <= mid)
    update(x, l, mid, root << 1);
  else
    update(x, mid + 1, r, root << 1 | 1);
  pushup(root);
}
void add(int L, int R, int l, int r, int root, int delta) {
  if (L <= l && r <= R) {
    mn[root] += delta;
    lazy[root] += delta;
    return;
  }
  pushdown(root);
  int mid = (l + r) >> 1;
  if (L <= mid) add(L, R, l, mid, root << 1, delta);
  if (mid < R) add(L, R, mid + 1, r, root << 1 | 1, delta);
  pushup(root);
}
void update(int x) {
  update(x - 1, 0, n, 1);
  update(x, 0, n, 1);
}
void del(int x) {
  int l = *s[a[x]].begin(), r = *s[a[x]].rbegin();
  add(l, r - 1, 0, n, 1, -1);
  d[l] = 0;
  update(l);
  s[a[x]].erase(x);
  if (!s[a[x]].empty()) {
    l = *s[a[x]].begin(), r = *s[a[x]].rbegin();
    d[l] = (int)s[a[x]].size();
    add(l, r - 1, 0, n, 1, 1);
    update(l);
  }
}
void insert(int x) {
  if (!s[a[x]].empty()) {
    int l = *s[a[x]].begin(), r = *s[a[x]].rbegin();
    add(l, r - 1, 0, n, 1, -1);
    d[l] = 0;
    update(l);
  }
  s[a[x]].insert(x);
  int l = *s[a[x]].begin(), r = *s[a[x]].rbegin();
  add(l, r - 1, 0, n, 1, 1);
  d[l] = (int)s[a[x]].size();
  update(l);
}
int main() {
  read(n), read(q);
  for (int i = 1; i <= n; i++) read(a[i]);
  build(0, n, 1);
  for (int i = 1; i <= n; i++) insert(i);
  printf("%d\n", cnt[1] - ans[1]);
  int x, y;
  while (q--) {
    read(x), read(y);
    del(x);
    a[x] = y;
    insert(x);
    printf("%d\n", cnt[1] - ans[1]);
  }
  return 0;
}