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;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 3e5 + 7;
int a[N];
set<int> q[N];
int cnt[N];
struct segm {
  int l, r, x;
};
set<segm> e;
bool operator<(const segm &a, const segm &b) { return a.l < b.l; }
const int B = 300;
const int BL = B;
int suf[N];
int max_x[N];
int jump_r[N];
int cnt_here[N];
int sum_here[N];
vector<segm> orz[N];
int w[N];
int tr[N];
bool vis[N];
void rebuild_block(int l) {
  l -= l % B;
  static vector<segm> p;
  p.clear();
  for (int i = l; i < l + B && i < N; i++) {
    if (vis[i]) {
      p.push_back({i, tr[i], w[i]});
    }
  }
  segm ret;
  ret.l = 0;
  ret.r = -1;
  ret.x = 0;
  static vector<segm> ans;
  ans.clear();
  for (auto c : p) {
    if (c.l > ret.r) {
      if (ret.r != -1) {
        ans.push_back(ret);
      }
      ret = c;
    } else {
      ret.r = max(ret.r, c.r);
      ret.x = max(ret.x, c.x);
    }
  }
  if (ret.r != -1) {
    ans.push_back(ret);
  }
  orz[l] = ans;
  int j = -1;
  int mx = 0;
  int r = -1;
  int sum = 0;
  for (int i = l; i < l + B && i < N; i++) {
    while (j + 1 < (int)ans.size() && ans[j + 1].l <= i) {
      j++;
      mx = max(mx, ans[j].x);
      r = max(r, ans[j].r);
      sum += ans[j].x;
    }
    sum_here[i] = sum;
    cnt_here[i] = j + 1;
    max_x[i] = mx;
    jump_r[i] = r;
  }
}
void del(int x, int i) {
  int l = *q[x].begin(), r = *prev(q[x].end());
  vis[l] = false;
  e.erase({l, r, cnt[x]});
  cnt[x]--;
  q[x].erase(i);
  rebuild_block(l);
  if (cnt[x]) {
    int l = *q[x].begin(), r = *prev(q[x].end());
    vis[l] = true;
    w[l] = cnt[x];
    tr[l] = r;
    e.insert({l, r, cnt[x]});
    rebuild_block(l);
  }
}
void add(int x, int i) {
  if (cnt[x]) {
    int l = *q[x].begin(), r = *prev(q[x].end());
    vis[l] = false;
    e.erase({l, r, cnt[x]});
    rebuild_block(l);
  }
  cnt[x]++;
  q[x].insert(i);
  int l = *q[x].begin(), r = *prev(q[x].end());
  vis[l] = true;
  w[l] = cnt[x];
  tr[l] = r;
  e.insert({l, r, cnt[x]});
  rebuild_block(l);
}
int ans() {
  int vr = -1;
  int tot = 0;
  int gao = 0;
  for (int i = 0; i < 200000; i += B) {
    if (orz[i].empty()) continue;
    if (vr <= i) {
      tot += gao;
      tot += sum_here[i + BL - 1] - orz[i].back().x;
      gao = orz[i].back().x;
      vr = orz[i].back().r;
    } else {
      int value = min(vr, i + BL - 1);
      gao = max(gao, max_x[value]);
      vr = max(vr, jump_r[value]);
      if (cnt_here[value] != cnt_here[i + BL - 1]) {
        tot += gao;
        tot += sum_here[i + BL - 1] - sum_here[value] - orz[i].back().x;
        gao = orz[i].back().x;
        vr = orz[i].back().r;
      }
    }
  }
  return tot + gao;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n = 200000, tq = 200000;
  cin >> n >> tq;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    q[a[i]].insert(i);
    cnt[a[i]]++;
  }
  for (int i = 0; i < N; i++) {
    if (!q[i].empty()) {
      vis[*q[i].begin()] = true;
      w[*q[i].begin()] = cnt[i];
      tr[*q[i].begin()] = *q[i].rbegin();
      e.insert({*q[i].begin(), *q[i].rbegin(), cnt[i]});
    }
  }
  for (int i = 0; i < N; i += BL) {
    rebuild_block(i);
  }
  cout << n - ans() << '\n';
  for (int j = 0; j < tq; j++) {
    int i, x;
    cin >> i >> x;
    i--, x--;
    del(a[i], i);
    a[i] = x;
    add(a[i], i);
    cout << n - ans() << '\n';
  }
}