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 = 200005;
set<int> clr[N];
struct TNode {
  int l, r, v, mn, lp, rp, mx;
} t[N << 2];
int lazy[N << 2];
inline int lc(int pos) { return pos << 1; }
inline int rc(int pos) { return pos << 1 | 1; }
inline void pushdown(int pos) {
  t[lc(pos)].mn += lazy[pos];
  t[rc(pos)].mn += lazy[pos];
  lazy[lc(pos)] += lazy[pos];
  lazy[rc(pos)] += lazy[pos];
  lazy[pos] = 0;
}
void pushup(int pos) {
  t[pos].mx = max(t[lc(pos)].mx, t[rc(pos)].mx);
  t[pos].mn = min(t[lc(pos)].mn, t[rc(pos)].mn);
  if (t[lc(pos)].mn < t[rc(pos)].mn) {
    t[pos].v = t[lc(pos)].v;
    t[pos].lp = t[lc(pos)].lp;
    t[pos].rp = max(t[rc(pos)].mx, t[lc(pos)].rp);
  } else if (t[lc(pos)].mn > t[rc(pos)].mn) {
    t[pos].v = t[rc(pos)].v;
    t[pos].rp = t[rc(pos)].rp;
    t[pos].lp = max(t[lc(pos)].mx, t[rc(pos)].lp);
  } else {
    t[pos].lp = t[lc(pos)].lp;
    t[pos].rp = t[rc(pos)].rp;
    t[pos].v = t[lc(pos)].v + t[rc(pos)].v + max(t[lc(pos)].rp, t[rc(pos)].lp);
  }
}
void upd(int pos, int l, int r, int v) {
  if (t[pos].l == l && t[pos].r == r) {
    t[pos].mn += v;
    lazy[pos] += v;
    return;
  }
  int mid = (t[pos].l + t[pos].r) >> 1;
  pushdown(pos);
  if (r <= mid)
    upd(lc(pos), l, r, v);
  else if (l > mid)
    upd(rc(pos), l, r, v);
  else
    upd(lc(pos), l, mid, v), upd(rc(pos), mid + 1, r, v);
  pushup(pos);
}
void cov(int pos, int p, int v) {
  if (t[pos].l == t[pos].r) {
    t[pos].mx = t[pos].lp = v;
    return;
  }
  int mid = (t[pos].l + t[pos].r) >> 1;
  pushdown(pos);
  if (p <= mid)
    cov(lc(pos), p, v);
  else
    cov(rc(pos), p, v);
  pushup(pos);
}
void build(int pos, int l, int r) {
  t[pos].l = l;
  t[pos].r = r;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(lc(pos), l, mid);
  build(rc(pos), mid + 1, r);
}
int n, q, bs[N];
void modify(int c, int mk) {
  int w = clr[c].size();
  if (!w) return;
  cov(1, *clr[c].begin(), mk > 0 ? w : 0);
  if (w != 1) upd(1, *clr[c].begin(), *clr[c].rbegin() - 1, mk);
}
inline int ans() { return n - t[1].v - t[1].lp - t[1].rp; }
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) cin >> bs[i], clr[bs[i]].insert(i);
  build(1, 1, n);
  for (int i = 1; i < N; i++) modify(i, 1);
  cout << ans() << endl;
  while (q--) {
    int x, y, z;
    cin >> x >> y;
    z = bs[x];
    modify(z, -1);
    clr[z].erase(x);
    modify(z, 1);
    modify(y, -1);
    clr[y].insert(x);
    modify(y, 1);
    cout << ans() << endl;
    bs[x] = y;
  }
  return 0;
}