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;
inline long long read() {
  char c = getchar();
  long long x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
long long n, Q;
long long val[200005 << 2], tag[200005 << 2], cnt[200005 << 2];
long long lmx[200005 << 2], rmx[200005 << 2], sum[200005 << 2];
inline void pushup(long long p) {
  long long ls = p << 1, rs = p << 1 | 1;
  val[p] = min(val[ls], val[rs]);
  cnt[p] = max(cnt[ls], cnt[rs]);
  if (val[ls] < val[rs]) {
    sum[p] = sum[ls];
    lmx[p] = lmx[ls], rmx[p] = max(rmx[ls], cnt[rs]);
  } else if (val[ls] > val[rs]) {
    sum[p] = sum[rs];
    rmx[p] = rmx[rs], lmx[p] = max(cnt[ls], lmx[rs]);
  } else {
    lmx[p] = lmx[ls], rmx[p] = rmx[rs];
    sum[p] = sum[ls] + sum[rs] + max(lmx[rs], rmx[ls]);
  }
}
inline void pusht(long long p, long long v) { val[p] += v, tag[p] += v; }
inline void pushdown(long long p) {
  if (tag[p]) pusht(p << 1, tag[p]), pusht(p << 1 | 1, tag[p]), tag[p] = 0;
}
void mdf_val(long long p, long long l, long long r, long long nl, long long nr,
             long long v) {
  if (nl > nr) return;
  if (l >= nl && r <= nr) return pusht(p, v);
  long long mid = l + r >> 1;
  pushdown(p);
  if (nl <= mid) mdf_val(p << 1, l, mid, nl, nr, v);
  if (nr > mid) mdf_val(p << 1 | 1, mid + 1, r, nl, nr, v);
  pushup(p);
}
void mdf_cnt(long long p, long long l, long long r, long long x, long long v) {
  if (l == r) return cnt[p] = lmx[p] = v, void();
  long long mid = l + r >> 1;
  pushdown(p);
  x <= mid ? mdf_cnt(p << 1, l, mid, x, v)
           : mdf_cnt(p << 1 | 1, mid + 1, r, x, v);
  pushup(p);
}
long long a[200005];
set<long long> s[200005];
inline void mdf(long long c, long long o) {
  if (!s[c].size()) return;
  long long l = *s[c].begin(), r = *(--s[c].end());
  mdf_cnt(1, 1, n, l, (o > 0) * (s[c].size()));
  mdf_val(1, 1, n, l, r - 1, o);
}
inline long long orz() { return n - sum[1] - lmx[1] - rmx[1]; }
signed main() {
  n = read(), Q = read();
  for (register long long i = (1); i <= (n); ++i)
    a[i] = read(), s[a[i]].insert(i);
  for (register long long i = (1); i <= (200000); ++i) mdf(i, 1);
  printf("%d\n", orz());
  for (register long long _ = (1); _ <= (Q); ++_) {
    long long x = read(), y = read(), z = a[x];
    mdf(z, -1), s[z].erase(x), mdf(z, 1);
    mdf(y, -1), s[y].insert(x), mdf(y, 1);
    a[x] = y;
    printf("%d\n", orz());
  }
  return 0;
}