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 <class T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T>
T gcd(T a, T b) {
  return !b ? a : gcd(b, a % b);
}
template <class T>
inline void umin(T &x, T y) {
  x = x < y ? x : y;
}
template <class T>
inline void umax(T &x, T y) {
  x = x > y ? x : y;
}
inline unsigned int R() {
  static unsigned int seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 233333;
int n, q, a[N];
set<int> Set[N];
struct node {
  int mn, lzy, mx, lmx, rmx, ans;
} tree[N << 2];
void upd(int k) {
  tree[k].mn = min(tree[k << 1].mn, tree[k << 1 | 1].mn);
  tree[k].mx = max(tree[k << 1].mx, tree[k << 1 | 1].mx);
  if (tree[k << 1].mn < tree[k << 1 | 1].mn) {
    tree[k].ans = tree[k << 1].ans;
    tree[k].lmx = tree[k << 1].lmx;
    tree[k].rmx = max(tree[k << 1].rmx, tree[k << 1 | 1].mx);
  } else if (tree[k << 1].mn > tree[k << 1 | 1].mn) {
    tree[k].ans = tree[k << 1 | 1].ans;
    tree[k].rmx = tree[k << 1 | 1].rmx;
    tree[k].lmx = max(tree[k << 1].mx, tree[k << 1 | 1].lmx);
  } else {
    tree[k].ans = tree[k << 1].ans + tree[k << 1 | 1].ans +
                  max(tree[k << 1].rmx, tree[k << 1 | 1].lmx);
    tree[k].lmx = tree[k << 1].lmx;
    tree[k].rmx = tree[k << 1 | 1].rmx;
  }
}
void pushdown(int k) {
  if (tree[k].lzy) {
    tree[k << 1].lzy += tree[k].lzy;
    tree[k << 1].mn += tree[k].lzy;
    tree[k << 1 | 1].lzy += tree[k].lzy;
    tree[k << 1 | 1].mn += tree[k].lzy;
    tree[k].lzy = 0;
  }
}
void mdy(int k, int l, int r, int x, int L, int R) {
  if (l == L && r == R) {
    tree[k].mn += x;
    tree[k].lzy += x;
    return;
  }
  int mid = (L + R) >> 1;
  pushdown(k);
  if (r <= mid)
    mdy(k << 1, l, r, x, L, mid);
  else if (l > mid)
    mdy(k << 1 | 1, l, r, x, mid + 1, R);
  else
    mdy(k << 1, l, mid, x, L, mid), mdy(k << 1 | 1, mid + 1, r, x, mid + 1, R);
  upd(k);
}
void modify(int k, int p, int x, int l, int r) {
  if (l == r) {
    tree[k].mx = tree[k].lmx = x;
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(k);
  if (p <= mid)
    modify(k << 1, p, x, l, mid);
  else
    modify(k << 1 | 1, p, x, mid + 1, r);
  upd(k);
}
void ins(int x, int t) {
  if (Set[x].empty()) return;
  if (Set[x].size() >= 2)
    mdy(1, *Set[x].begin(), (*Set[x].rbegin()) - 1, t, 0, n);
  modify(1, *Set[x].begin(), t > 0 ? ((int)Set[x].size()) : 0, 0, n);
}
int main() {
  read(n);
  read(q);
  for (register int i = (1); i <= (n); i++) read(a[i]), Set[a[i]].insert(i);
  for (register int i = (0); i <= (n); i++) modify(1, i, 0, 0, n);
  for (register int i = (1); i <= (2e5); i++) ins(i, 1);
  printf("%d\n", n - tree[1].ans);
  while (q--) {
    int x, y;
    read(x);
    read(y);
    ins(a[x], -1);
    Set[a[x]].erase(x);
    ins(a[x], 1);
    a[x] = y;
    ins(a[x], -1);
    Set[a[x]].insert(x);
    ins(a[x], 1);
    printf("%d\n", n - tree[1].ans);
  }
  return 0;
}