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 p1 = 1000000, p2 = 0;
char buf[1000005], wb[1000005];
int gc() {
  if (p1 >= 1000000) fread(buf, 1, 1000000, stdin), p1 = 0;
  return buf[p1++];
}
long long getint() {
  long long ret = 0, flag = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') flag = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    ret = (ret << 3) + (ret << 1) + c - '0';
    c = getchar();
  }
  return ret * flag;
}
void pc(char x) {
  if (p2 >= 1000000) fwrite(wb, 1, 1000000, stdout), p2 = 0;
  wb[p2++] = x;
}
void wrt(long long x) {
  if (x < 0) pc('-'), x = -x;
  int c[24] = {0};
  if (!x) return pc('0'), void();
  while (x) c[++c[0]] = x % 10, x /= 10;
  while (c[0]) pc(c[c[0]--] + '0');
}
int n, Q;
int a[200005];
set<int> s[200005];
namespace S {
struct node {
  int l, r;
  int stag, mi, mx;
  int lb, rb, lmx, rmx, sum;
} v[800005];
void push_up(int rt) {
  v[rt].mi = min(v[rt << 1].mi, v[rt << 1 | 1].mi);
  v[rt].mx = max(v[rt << 1].mx, v[rt << 1 | 1].mx);
  v[rt].sum = v[rt].lmx = v[rt].rmx = 0;
  v[rt].lb = v[rt].mi == v[rt << 1].mi
                 ? (v[rt].sum += v[rt << 1].sum, v[rt].lmx = v[rt << 1].lmx,
                    v[rt << 1].lb)
                 : (v[rt].lmx = max(v[rt << 1].mx, v[rt << 1 | 1].lmx),
                    v[rt << 1 | 1].lb);
  v[rt].rb =
      v[rt].mi == v[rt << 1 | 1].mi
          ? (v[rt].sum += v[rt << 1 | 1].sum, v[rt].rmx = v[rt << 1 | 1].rmx,
             v[rt << 1 | 1].rb)
          : (v[rt].rmx = max(v[rt << 1 | 1].mx, v[rt << 1].rmx), v[rt << 1].rb);
  v[rt].sum += (v[rt << 1].mi == v[rt << 1 | 1].mi)
                   ? max(v[rt << 1 | 1].lmx, v[rt << 1].rmx)
                   : 0;
}
void push_down(int rt) {
  if (v[rt].stag) {
    v[rt << 1].mi += v[rt].stag, v[rt << 1].stag += v[rt].stag;
    v[rt << 1 | 1].mi += v[rt].stag, v[rt << 1 | 1].stag += v[rt].stag;
    v[rt].stag = 0;
  }
}
void build(int rt, int l, int r) {
  v[rt].l = l, v[rt].r = r;
  if (l == r) return v[rt].lb = v[rt].rb = v[rt].l, void();
  int mid = (l + r) >> 1;
  build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r);
  push_up(rt);
}
void add(int rt, int l, int r, int val) {
  if (l > r) return;
  if (l <= v[rt].l && v[rt].r <= r)
    return v[rt].stag += val, v[rt].mi += val, void();
  int mid = v[rt << 1].r;
  push_down(rt);
  if (l <= mid) add(rt << 1, l, r, val);
  if (mid < r) add(rt << 1 | 1, l, r, val);
  push_up(rt);
}
void modify(int rt, int l, int r, int val) {
  if (l > r) return;
  if (l <= v[rt].l && v[rt].r <= r)
    return v[rt].lmx = v[rt].mx = val, v[rt].rmx = 0, void();
  int mid = v[rt << 1].r;
  push_down(rt);
  if (l <= mid) modify(rt << 1, l, r, val);
  if (mid < r) modify(rt << 1 | 1, l, r, val);
  push_up(rt);
}
};  // namespace S
void del(int c) {
  if (s[c].size()) {
    S::add(1, *s[c].begin(), *s[c].rbegin() - 1, -1);
    S::modify(1, *s[c].begin(), *s[c].begin(), 0);
  }
}
void add(int c) {
  if (s[c].size()) {
    S::add(1, *s[c].begin(), *s[c].rbegin() - 1, +1);
    S::modify(1, *s[c].begin(), *s[c].begin(), s[c].size());
  }
}
int main() {
  n = getint(), Q = getint();
  for (int i = 1; i <= n; i++) a[i] = getint();
  for (int i = 1; i <= n; i++) s[a[i]].insert(i);
  S::build(1, 1, n);
  for (int i = 1; i <= 200000; i++)
    if (s[i].size()) add(i);
  wrt(S::v[1].mi == 0 ? n - S::v[1].sum - S::v[1].lmx - S::v[1].rmx
                      : n - S::v[1].mx),
      pc('\n');
  for (int i = 1; i <= Q; i++) {
    int p = getint(), c = getint(), lasc = a[p];
    del(lasc), del(c), s[a[p] = lasc].erase(p), s[a[p] = c].insert(p),
        add(lasc), add(c);
    wrt(S::v[1].mi == 0 ? n - S::v[1].sum - S::v[1].lmx - S::v[1].rmx
                        : n - S::v[1].mx),
        pc('\n');
  }
  fwrite(wb, 1, p2, stdout);
  return true xor true;
}