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>
const int maxn = 200200;
const int N = 200000;
using std::cin;
using std::cout;
int n, q;
int a[maxn];
namespace lct {
int max[maxn << 2], Max[maxn << 2];
struct mes {
  int pre_max, sum, suf_max, EP;
} sum[maxn << 2];
inline int val(const mes& x) { return x.pre_max + x.sum + x.suf_max; }
inline void up(int& x, int y) {
  if (x < y) {
    x = y;
  }
}
inline mes operator+(const mes& x, const mes& y) {
  if (x.EP && y.EP) return (mes){std::max(x.pre_max, y.pre_max), 0, 0, 1};
  if (x.EP) return (mes){std::max(x.pre_max, y.pre_max), y.sum, y.suf_max, 0};
  if (y.EP) return (mes){x.pre_max, x.sum, std::max(x.suf_max, y.pre_max), 0};
  return (mes){x.pre_max, x.sum + std::max(x.suf_max, y.pre_max) + y.sum,
               y.suf_max, 0};
}
inline mes go(int cur, int v, int l = 1, int r = n) {
  if (l == r) return max[cur] >= v ? sum[cur] : mes{Max[cur], 0, 0, 1};
  int mid = l + r >> 1;
  if (max[cur << 1] < v) {
    mes res = go(cur << 1 | 1, v, mid + 1, r);
    up(res.pre_max, Max[cur << 1]);
    return res;
  } else {
    return go(cur << 1, v, l, mid) + sum[cur];
  }
}
inline void upt(int pos, int M, int S, int E, int cur = 1, int l = 1,
                int r = n) {
  if (l == r)
    return max[cur] = M, Max[cur] = S, sum[cur] = {S, 0, 0, E}, void();
  int mid = l + r >> 1;
  pos <= mid ? upt(pos, M, S, E, cur << 1, l, mid)
             : upt(pos, M, S, E, cur << 1 | 1, mid + 1, r);
  Max[cur] = std::max(Max[cur << 1], Max[cur << 1 | 1]);
  max[cur] = std::max(max[cur << 1], max[cur << 1 | 1]);
  sum[cur] = go(cur << 1 | 1, max[cur << 1], mid + 1, r);
}
}  // namespace lct
std::set<int> set[maxn];
inline void ins(int c) {
  if (set[c].size()) {
    lct::upt(*set[c].begin(), *set[c].rbegin(), 0, 1);
    lct::upt(*set[c].rbegin(), *set[c].rbegin(), set[c].size(), 0);
  }
}
inline void del(int c) {
  if (set[c].size()) {
    lct::upt(*set[c].begin(), 0, 0, 1);
    lct::upt(*set[c].rbegin(), 0, 0, 1);
  }
}
int main() {
  std::ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    set[a[i]].emplace(i);
  }
  for (int i = 1; i <= N; ++i) ins(i);
  cout << n - val(lct::go(1, 0)) << '\n';
  for (int i = 1, pos, c; i <= q; ++i) {
    cin >> pos >> c;
    if (a[pos] != c) {
      del(a[pos]);
      del(c);
      set[a[pos]].erase(pos);
      set[c].insert(pos);
      ins(a[pos]);
      ins(c);
      a[pos] = c;
    }
    cout << n - val(lct::go(1, 0)) << '\n';
  }
}