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;
const int N = 2e5 + 100;
int a[N], w[N], b[N], n, q, p, x;
bool vis[N];
set<int> st[N];
struct node {
  int tag, mn, mx, l, r, all, cnt;
} tr[1 << 19];
void pushup(int k, int l, int r) {
  int lk = k * 2 + 1, rk = k * 2 + 2, mid = (l + r) >> 1;
  tr[k].mn = min(tr[lk].mn, tr[rk].mn);
  tr[k].mx = max(tr[lk].mx, tr[rk].mx);
  tr[k].l = (tr[k].mn == tr[lk].mn) ? tr[lk].l : max(tr[lk].mx, tr[rk].l);
  tr[k].r = (tr[k].mn == tr[rk].mn) ? tr[rk].r : max(tr[lk].r, tr[rk].mx);
  if (tr[k].mn != tr[lk].mn) {
    tr[k].cnt = tr[rk].cnt;
    tr[k].all = tr[rk].all + mid - l + 1;
  } else if (tr[k].mn != tr[rk].mn) {
    tr[k].cnt = tr[lk].cnt;
    tr[k].all = tr[lk].all + r - mid;
  } else {
    tr[k].cnt = tr[lk].cnt + tr[rk].cnt;
    tr[k].all = tr[lk].all + tr[rk].all;
    if (tr[lk].r != -1 || tr[rk].l != -1) {
      tr[k].cnt += max(tr[lk].r, tr[rk].l);
      tr[k].all++;
    }
  }
}
void pushdown(int k) {
  if (tr[k].tag) {
    int lk = k * 2 + 1, rk = k * 2 + 2;
    tr[lk].tag += tr[k].tag;
    tr[rk].tag += tr[k].tag;
    tr[lk].mn += tr[k].tag;
    tr[rk].mn += tr[k].tag;
    tr[k].tag = 0;
  }
}
void build(int k, int l, int r) {
  tr[k].tag = 0;
  if (l == r) {
    tr[k].all = tr[k].cnt = 0;
    tr[k].l = tr[k].r = -1;
    tr[k].mn = b[l];
    tr[k].mx = max(w[l], w[l + 1]);
    return;
  }
  int lk = k * 2 + 1, rk = k * 2 + 2, mid = (l + r) >> 1;
  build(lk, l, mid);
  build(rk, mid + 1, r);
  pushup(k, l, r);
}
void update(int p, int k, int l, int r) {
  if (p < l || p > r) return;
  if (l == r) {
    tr[k].mx = max(w[l], w[l + 1]);
    return;
  }
  int lk = k * 2 + 1, rk = k * 2 + 2, mid = (l + r) >> 1;
  pushdown(k);
  update(p, lk, l, mid);
  update(p, rk, mid + 1, r);
  pushup(k, l, r);
}
void modify(int a, int b, int c, int k, int l, int r) {
  if (a > r || b < l) return;
  if (a <= l && r <= b) {
    tr[k].tag += c;
    tr[k].mn += c;
    return;
  }
  int lk = k * 2 + 1, rk = k * 2 + 2, mid = (l + r) >> 1;
  pushdown(k);
  modify(a, b, c, lk, l, mid);
  modify(a, b, c, rk, mid + 1, r);
  pushup(k, l, r);
}
void add(int p, int x) {
  if (!st[x].empty()) {
    int l = *st[x].begin(), r = *st[x].rbegin();
    if (l < r) modify(l, r - 1, -1, 0, 0, n);
    w[l] = 0;
    update(l - 1, 0, 0, n);
    update(l, 0, 0, n);
  }
  st[x].insert(p);
  int l = *st[x].begin(), r = *st[x].rbegin();
  if (l < r) modify(l, r - 1, 1, 0, 0, n);
  w[l] = st[x].size();
  update(l - 1, 0, 0, n);
  update(l, 0, 0, n);
}
void del(int p, int x) {
  int l = *st[x].begin(), r = *st[x].rbegin();
  if (l < r) modify(l, r - 1, -1, 0, 0, n);
  w[l] = 0;
  update(l - 1, 0, 0, n);
  update(l, 0, 0, n);
  st[x].erase(p);
  if (!st[x].empty()) {
    int l = *st[x].begin(), r = *st[x].rbegin();
    if (l < r) modify(l, r - 1, 1, 0, 0, n);
    w[l] = st[x].size();
    update(l - 1, 0, 0, n);
    update(l, 0, 0, n);
  }
}
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    st[a[i]].insert(i);
    vis[a[i]] = false;
  }
  for (int i = 1; i <= n; ++i) {
    if (vis[a[i]]) continue;
    vis[a[i]] = true;
    int l = *st[a[i]].begin(), r = *st[a[i]].rbegin();
    w[l] = st[a[i]].size();
    if (l <= r - 1) b[l]++, b[r]--;
  }
  for (int i = 1; i <= n; ++i) b[i] += b[i - 1];
  build(0, 0, n);
  printf("%d\n", tr[0].all - tr[0].cnt);
  while (q--) {
    scanf("%d %d", &p, &x);
    del(p, a[p]);
    a[p] = x;
    add(p, a[p]);
    printf("%d\n", tr[0].all - tr[0].cnt);
  }
}