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;
int gi() {
  int res = 0, w = 1;
  char ch = getchar();
  while (ch != '-' && !isdigit(ch)) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar();
  return res * w;
}
const int MAX_N = 2e5 + 5;
int N, Q, a[MAX_N];
set<int> S[MAX_N];
int sum[MAX_N << 2], mx[MAX_N << 2], lm[MAX_N << 2], rm[MAX_N << 2];
int tag[MAX_N << 2], mn[MAX_N << 2];
void puttag(int o, int v) { tag[o] += v, mn[o] += v; }
void pushdown(int o) {
  if (!tag[o]) return;
  puttag((o << 1), tag[o]), puttag((o << 1 | 1), tag[o]);
  tag[o] = 0;
}
void pushup(int o) {
  mx[o] = max(mx[(o << 1)], mx[(o << 1 | 1)]);
  mn[o] = min(mn[(o << 1)], mn[(o << 1 | 1)]);
  if (mn[(o << 1)] < mn[(o << 1 | 1)]) {
    sum[o] = sum[(o << 1)];
    lm[o] = lm[(o << 1)], rm[o] = max(mx[(o << 1 | 1)], rm[(o << 1)]);
  }
  if (mn[(o << 1)] > mn[(o << 1 | 1)]) {
    sum[o] = sum[(o << 1 | 1)];
    rm[o] = rm[(o << 1 | 1)], lm[o] = max(mx[(o << 1)], lm[(o << 1 | 1)]);
  }
  if (mn[(o << 1)] == mn[(o << 1 | 1)]) {
    sum[o] =
        sum[(o << 1)] + sum[(o << 1 | 1)] + max(rm[(o << 1)], lm[(o << 1 | 1)]);
    lm[o] = lm[(o << 1)], rm[o] = rm[(o << 1 | 1)];
  }
}
void modify(int ql, int qr, int v, int o = 1, int l = 1, int r = N) {
  if (ql > qr) return;
  if (ql <= l && r <= qr) return puttag(o, v);
  pushdown(o);
  int mid = (l + r) >> 1;
  if (ql <= mid) modify(ql, qr, v, (o << 1), l, mid);
  if (qr > mid) modify(ql, qr, v, (o << 1 | 1), mid + 1, r);
  pushup(o);
}
void upd(int pos, int v, int o = 1, int l = 1, int r = N) {
  if (l == r) return (void)(mx[o] = lm[o] = v);
  pushdown(o);
  int mid = (l + r) >> 1;
  if (pos <= mid)
    upd(pos, v, (o << 1), l, mid);
  else
    upd(pos, v, (o << 1 | 1), mid + 1, r);
  pushup(o);
}
void mdf(int v, int op) {
  if (!S[v].size()) return;
  upd(*S[v].begin(), op == 1 ? S[v].size() : 0);
  modify(*S[v].begin(), *S[v].rbegin() - 1, op);
}
int getans() { return N - sum[1] - lm[1] - rm[1]; }
int main() {
  N = gi(), Q = gi();
  for (int i = 1; i <= N; i++) a[i] = gi();
  for (int i = 1; i <= N; i++) S[a[i]].insert(i);
  for (int i = 1; i <= 2e5; i++) mdf(i, 1);
  printf("%d\n", getans());
  while (Q--) {
    int p = gi(), x = gi();
    mdf(a[p], -1), S[a[p]].erase(p), mdf(a[p], 1);
    a[p] = x;
    mdf(a[p], -1), S[a[p]].insert(p), mdf(a[p], 1);
    printf("%d\n", getans());
  }
  return 0;
}