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 n, q, a[200020];
set<int> occ[200020];
struct node {
  int mini, maxfreq, sum, l, r, lazy;
  node() : mini(0), maxfreq(0), sum(0), l(0), r(0), lazy(0) {}
};
node tree[8 * 200020];
void push_lazy(int id, int l, int r) {
  if (tree[id].lazy) {
    tree[id].mini += tree[id].lazy;
    if (l != r) {
      tree[id * 2].lazy += tree[id].lazy;
      tree[id * 2 + 1].lazy += tree[id].lazy;
    }
    tree[id].lazy = 0;
  }
}
node merge(node a, node b) {
  node c;
  if (a.mini == b.mini) {
    c.mini = a.mini;
    c.maxfreq = max(a.maxfreq, b.maxfreq);
    c.sum = a.sum + b.sum - min(a.r, b.l);
    c.l = a.l;
    c.r = b.r;
  } else if (a.mini < b.mini) {
    c.mini = a.mini;
    c.maxfreq = max(a.maxfreq, b.maxfreq);
    c.sum = a.sum - a.r + max(a.r, b.maxfreq);
    c.l = a.l;
    c.r = max(a.r, b.maxfreq);
  } else {
    c.mini = b.mini;
    c.maxfreq = max(a.maxfreq, b.maxfreq);
    c.sum = b.sum - b.l + max(b.l, a.maxfreq);
    c.l = max(b.l, a.maxfreq);
    c.r = b.r;
  }
  return c;
}
void upd_range(int id, int l, int r, int lq, int rq, int delta) {
  push_lazy(id, l, r);
  if (rq < l || r < lq) return;
  if (lq <= l && r <= rq) {
    tree[id].lazy += delta;
    push_lazy(id, l, r);
  } else {
    int m = (l + r) / 2;
    upd_range(id * 2, l, m, lq, rq, delta);
    upd_range(id * 2 + 1, m + 1, r, lq, rq, delta);
    tree[id] = merge(tree[id * 2], tree[id * 2 + 1]);
  }
}
void upd_freq(int id, int l, int r, int x, int val) {
  push_lazy(id, l, r);
  if (x < l || r < x) return;
  if (l == r) {
    tree[id].maxfreq = tree[id].sum = tree[id].l = tree[id].r = val;
  } else {
    int m = (l + r) / 2;
    upd_freq(id * 2, l, m, x, val);
    upd_freq(id * 2 + 1, m + 1, r, x, val);
    tree[id] = merge(tree[id * 2], tree[id * 2 + 1]);
  }
}
int query() {
  if (tree[1].mini == 0) return tree[1].sum;
  return tree[1].maxfreq;
}
int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    occ[a[i]].insert(i);
  }
  for (int i = 0; i < 200020; i++) {
    if (occ[i].size() >= 1) {
      upd_range(1, 1, 2 * n, 2 * *occ[i].begin(), 2 * *occ[i].rbegin(), 1);
      upd_freq(1, 1, 2 * n, 2 * *occ[i].begin(), occ[i].size());
    }
  }
  printf("%d\n", n - query());
  for (int i = 0; i < q; i++) {
    int p, x, y;
    scanf("%d %d", &p, &x);
    y = a[p];
    if (occ[y].size() >= 1) {
      upd_range(1, 1, 2 * n, 2 * *occ[y].begin(), 2 * *occ[y].rbegin(), -1);
      upd_freq(1, 1, 2 * n, 2 * *occ[y].begin(), 0);
    }
    occ[y].erase(p);
    if (occ[y].size() >= 1) {
      upd_range(1, 1, 2 * n, 2 * *occ[y].begin(), 2 * *occ[y].rbegin(), 1);
      upd_freq(1, 1, 2 * n, 2 * *occ[y].begin(), occ[y].size());
    }
    if (occ[x].size() >= 1) {
      upd_range(1, 1, 2 * n, 2 * *occ[x].begin(), 2 * *occ[x].rbegin(), -1);
      upd_freq(1, 1, 2 * n, 2 * *occ[x].begin(), 0);
    }
    occ[x].insert(p);
    if (occ[x].size() >= 1) {
      upd_range(1, 1, 2 * n, 2 * *occ[x].begin(), 2 * *occ[x].rbegin(), 1);
      upd_freq(1, 1, 2 * n, 2 * *occ[x].begin(), occ[x].size());
    }
    a[p] = x;
    printf("%d\n", n - query());
  }
  return 0;
}