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, class U>
void ckmin(T &a, U b) {
  if (a > b) a = b;
}
template <class T, class U>
void ckmax(T &a, U b) {
  if (a < b) a = b;
}
int N, Q;
int arr[200013];
set<int> pos[200013];
int spred[200013], smx[200013];
int ans;
struct node {
  int lazy, pred, sum, mxl, mxr, mx;
};
node seg[200013 * 3];
node comb(node l, node r) {
  node res;
  res.lazy = 0;
  if (l.pred < r.pred) {
    res.pred = l.pred;
    res.sum = l.sum;
    res.mxl = l.mxl;
    res.mxr = max(l.mxr, r.mx);
  } else if (l.pred > r.pred) {
    res.pred = r.pred;
    res.sum = r.sum;
    res.mxl = max(l.mx, r.mxl);
    res.mxr = r.mxr;
  } else {
    res.pred = l.pred;
    res.sum = l.sum + max(l.mxr, r.mxl) + r.sum;
    res.mxl = l.mxl;
    res.mxr = r.mxr;
  }
  res.mx = max(l.mx, r.mx);
  return res;
}
void push(int w, int L, int R) {
  if (seg[w].lazy == 0) return;
  seg[w].pred += seg[w].lazy;
  if (L != R) {
    seg[w << 1].lazy += seg[w].lazy;
    seg[w << 1 | 1].lazy += seg[w].lazy;
  }
  seg[w].lazy = 0;
  return;
}
void build(int w, int L, int R) {
  if (L == R) {
    seg[w].lazy = 0;
    seg[w].pred = spred[L];
    seg[w].sum = 0;
    seg[w].mxl = smx[L];
    seg[w].mxr = 0;
    seg[w].mx = smx[L];
    return;
  }
  int mid = (L + R) >> 1;
  build(w << 1, L, mid);
  build(w << 1 | 1, mid + 1, R);
  seg[w] = comb(seg[w << 1], seg[w << 1 | 1]);
}
void update(int w, int L, int R, int a, int b, bool flag, int v) {
  push(w, L, R);
  if (b < L || R < a) return;
  if (a <= L && R <= b) {
    if (flag) {
      seg[w].lazy += v;
    } else {
      seg[w].mxr = 0;
      seg[w].mx = v;
      seg[w].mxl = v;
    }
    push(w, L, R);
    return;
  }
  int mid = (L + R) >> 1;
  update(w << 1, L, mid, a, b, flag, v);
  update(w << 1 | 1, mid + 1, R, a, b, flag, v);
  seg[w] = comb(seg[w << 1], seg[w << 1 | 1]);
}
void setv(int idx, int v) {
  int lt, rt;
  int u = arr[idx];
  lt = *pos[u].begin();
  rt = *pos[u].rbegin();
  update(1, 0, N - 1, lt, rt - 1, 1, -1);
  update(1, 0, N - 1, lt, lt, 0, 0);
  pos[u].erase(idx);
  if (!pos[u].empty()) {
    lt = *pos[u].begin();
    rt = *pos[u].rbegin();
    update(1, 0, N - 1, lt, rt - 1, 1, 1);
    update(1, 0, N - 1, lt, lt, 0, ((int)((pos[u]).size())));
  }
  if (!pos[v].empty()) {
    lt = *pos[v].begin();
    rt = *pos[v].rbegin();
    update(1, 0, N - 1, lt, rt - 1, 1, -1);
    update(1, 0, N - 1, lt, lt, 0, 0);
  }
  pos[v].insert(idx);
  lt = *pos[v].begin();
  rt = *pos[v].rbegin();
  update(1, 0, N - 1, lt, rt - 1, 1, 1);
  update(1, 0, N - 1, lt, lt, 0, ((int)((pos[v]).size())));
  arr[idx] = v;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> Q;
  for (auto i = (0); i < (N); i++) {
    cin >> arr[i];
    arr[i]--;
    pos[arr[i]].insert(i);
  }
  for (auto i = (0); i < (200000); i++) {
    if (pos[i].empty()) continue;
    spred[*pos[i].begin()]++;
    spred[*pos[i].rbegin()]--;
    smx[*pos[i].begin()] = ((int)((pos[i]).size()));
  }
  for (auto i = (1); i < (N); i++) {
    spred[i] += spred[i - 1];
  }
  build(1, 0, N - 1);
  ans = seg[1].mxl + seg[1].sum;
  ans = N - ans;
  cout << ans << '\n';
  while (Q--) {
    int idx, v;
    cin >> idx >> v;
    idx--;
    v--;
    setv(idx, v);
    push(1, 0, N - 1);
    ans = seg[1].mxl + seg[1].sum;
    ans = N - ans;
    cout << ans << '\n';
  }
  return 0;
}