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;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
struct f {
  int mn, mx, mod, lx, rx, sx;
  f operator+(f x) {
    if (mn < x.mn) return {mn, max(mx, x.mx), 0, lx, max(rx, x.mx), sx};
    if (mn > x.mn) return {x.mn, max(mx, x.mx), 0, max(mx, x.lx), x.rx, x.sx};
    return {mn, max(mx, x.mx), 0, lx, x.rx, sx + x.sx + max(rx, x.lx)};
  }
};
const int N = 1 << 18;
int a[N], ps[N], n;
set<int> ls[N];
f t[N * 2];
void push(int v) {
  t[v].mn += t[v].mod;
  if (v < N) {
    t[v * 2].mod += t[v].mod;
    t[v * 2 + 1].mod += t[v].mod;
  }
  t[v].mod = 0;
}
void rel(int v) { t[v] = t[v * 2] + t[v * 2 + 1]; }
void add(int cl, int cr, int d, int v = 1, int l = 0, int r = N) {
  if (cr <= l || r <= cl) {
    push(v);
    return;
  }
  if (cl <= l && r <= cr) {
    t[v].mod += d;
    push(v);
    return;
  }
  push(v);
  add(cl, cr, d, v * 2, l, (l + r) / 2);
  add(cl, cr, d, v * 2 + 1, (l + r) / 2, r);
  rel(v);
}
int qu() { return n - (t[1].lx + t[1].rx + t[1].sx); }
void cx(int p, int x, int v = 1, int l = 0, int r = N) {
  push(v);
  if (r - l == 1) {
    t[v].lx = t[v].mx = x;
    return;
  }
  if (p < (l + r) / 2) {
    cx(p, x, v * 2, l, (l + r) / 2);
    push(v * 2 + 1);
  } else {
    push(v * 2);
    cx(p, x, v * 2 + 1, (l + r) / 2, r);
  }
  rel(v);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int q, p, x;
  cin >> n >> q;
  for (int i = 0; i < (N); i++) {
    if (i < n)
      cin >> a[i];
    else
      a[i] = N - 1;
    ls[a[i]].insert(i);
  }
  n = N;
  for (int i = 0; i < (n); i++)
    if (i == *ls[a[i]].begin()) t[N + i].lx = t[N + i].mx = ls[a[i]].size();
  for (int i = N - 1; i; i--) rel(i);
  for (int i = 0; i < (N); i++)
    if (!ls[i].empty()) add(*ls[i].begin(), *--ls[i].end(), 1);
  cout << qu() << '\n';
  for (int z = 0; z < (q); z++) {
    cin >> p >> x;
    p--;
    cx(*ls[a[p]].begin(), 0);
    add(*ls[a[p]].begin(), *--ls[a[p]].end(), -1);
    ls[a[p]].erase(p);
    if (!ls[a[p]].empty()) {
      add(*ls[a[p]].begin(), *--ls[a[p]].end(), 1);
      cx(*ls[a[p]].begin(), ls[a[p]].size());
    }
    if (!ls[x].empty()) {
      add(*ls[x].begin(), *--ls[x].end(), -1);
      cx(*ls[x].begin(), 0);
    }
    ls[x].insert(p);
    add(*ls[x].begin(), *--ls[x].end(), 1);
    cx(*ls[x].begin(), ls[x].size());
    a[p] = x;
    cout << qu() << '\n';
  }
  for (int i = 1; i < N; i++) push(i);
}