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 a[200005], n, q;
set<int> s[200005];
int c[200005];
int cnt[200005];
struct Tsegtree1 {
  int mn[4 * 200005], noMin[4 * 200005], leaf[200005], lazy[4 * 200005],
      mx[4 * 200005];
  int lMin[4 * 200005], rMin[4 * 200005], maxleftl[4 * 200005],
      maxrightr[4 * 200005], summax[4 * 200005];
  void minAgain(int x, int lo, int hi) {
    mx[x] = max(mx[2 * x], mx[2 * x + 1]);
    int mid = (lo + hi) / 2;
    mx[x] = max(mx[x], c[mid]);
    if (mn[2 * x] < mn[2 * x + 1]) {
      mn[x] = mn[2 * x];
      noMin[x] = noMin[2 * x];
      lMin[x] = lMin[2 * x];
      rMin[x] = rMin[2 * x];
      summax[x] = summax[2 * x];
      maxleftl[x] = maxleftl[2 * x];
      maxrightr[x] = max(maxrightr[2 * x], mx[2 * x + 1]);
      maxrightr[x] = max(maxrightr[x], c[mid]);
    } else if (mn[2 * x] > mn[2 * x + 1]) {
      mn[x] = mn[2 * x + 1];
      noMin[x] = noMin[2 * x + 1];
      lMin[x] = lMin[2 * x + 1];
      rMin[x] = rMin[2 * x + 1];
      summax[x] = summax[2 * x + 1];
      maxrightr[x] = maxrightr[2 * x + 1];
      maxleftl[x] = max(maxleftl[2 * x + 1], mx[2 * x]);
      maxleftl[x] = max(maxleftl[x], c[mid]);
    } else {
      mn[x] = mn[2 * x];
      noMin[x] = noMin[2 * x] + noMin[2 * x + 1];
      lMin[x] = lMin[2 * x];
      rMin[x] = rMin[2 * x + 1];
      maxleftl[x] = maxleftl[2 * x];
      maxrightr[x] = maxrightr[2 * x + 1];
      summax[x] = summax[2 * x] + summax[2 * x + 1];
      int val = max(maxrightr[2 * x], maxleftl[2 * x + 1]);
      summax[x] += max(val, c[mid]);
    }
  }
  void Build(int x, int lo, int hi) {
    if (lo == hi) {
      leaf[lo] = x;
      mn[x] = 0;
      noMin[x] = 1;
      lMin[x] = lo;
      rMin[x] = lo;
      maxleftl[x] = 0;
      maxrightr[x] = 0;
      summax[x] = 0;
      mx[x] = 0;
      return;
    }
    int mid = (lo + hi) / 2;
    Build(2 * x, lo, mid);
    Build(2 * x + 1, mid + 1, hi);
    minAgain(x, lo, hi);
  }
  void Down(int x) {
    mn[2 * x] += lazy[x];
    mn[2 * x + 1] += lazy[x];
    lazy[2 * x] += lazy[x];
    lazy[2 * x + 1] += lazy[x];
    lazy[x] = 0;
  }
  void Upd(int x, int lo, int hi, int st, int en, int val) {
    if (lo > en || hi < st) return;
    if (st <= lo && hi <= en) {
      mn[x] += val;
      lazy[x] += val;
      return;
    }
    int mid = (lo + hi) / 2;
    Down(x);
    Upd(2 * x, lo, mid, st, en, val);
    Upd(2 * x + 1, mid + 1, hi, st, en, val);
    minAgain(x, lo, hi);
  }
  int qpos;
  void Wow(int x, int lo, int hi) {
    int mid = (lo + hi) / 2;
    Down(x);
    if (mid == qpos) {
      minAgain(x, lo, hi);
      return;
    }
    if (mid > qpos) {
      Wow(2 * x, lo, mid);
    }
    if (mid < qpos) {
      Wow(2 * x + 1, mid + 1, hi);
    }
    minAgain(x, lo, hi);
  }
  void Change(int pos) {
    qpos = pos;
    Wow(1, 1, n + 1);
  }
};
Tsegtree1 tree;
void scam(int x, int val) {
  int j = *s[x].begin(), k = *s[x].rbegin();
  if (j + 1 <= k) tree.Upd(1, 1, n + 1, j + 1, k, val);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  int i, j, k;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    s[a[i]].insert(i);
    cnt[a[i]]++;
  }
  for (i = 1; i < 200005; i++) {
    if (cnt[i] > 0) {
      j = *s[i].begin();
      c[j] = cnt[i];
    }
  }
  tree.Build(1, 1, n + 1);
  for (i = 1; i < 200005; i++) {
    if (cnt[i] > 1) {
      j = *s[i].begin();
      k = *s[i].rbegin();
      if (j + 1 <= k) tree.Upd(1, 1, n + 1, j + 1, k, 1);
    }
  }
  cout << n - tree.summax[1] << '\n';
  int poss, val1, val2;
  for (i = 1; i <= q; i++) {
    cin >> poss >> val1;
    val2 = a[poss];
    cnt[val1]++;
    cnt[val2]--;
    a[poss] = val1;
    int wat = *s[val2].begin();
    c[wat] = 0;
    tree.Change(wat);
    scam(val2, -1);
    s[val2].erase(s[val2].find(poss));
    if (!s[val2].empty()) {
      wat = *s[val2].begin();
      c[wat] = cnt[val2];
      tree.Change(wat);
      scam(val2, 1);
    }
    if (!s[val1].empty()) {
      wat = *s[val1].begin();
      c[wat] = 0;
      tree.Change(wat);
      scam(val1, -1);
    }
    s[val1].insert(poss);
    wat = *s[val1].begin();
    c[wat] = cnt[val1];
    tree.Change(wat);
    scam(val1, 1);
    cout << n - tree.summax[1] << '\n';
  }
}