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;
namespace SGT {
int addv[800000], minn[800000];
int maxl[800000], maxr[800000], maxn[800000];
int sumv[800000];
inline void pushdown(int o) {
  if (addv[o]) {
    addv[o * 2] += addv[o];
    addv[o * 2 + 1] += addv[o];
    minn[o * 2] += addv[o];
    minn[o * 2 + 1] += addv[o];
    addv[o] = 0;
  }
}
inline void pushup(int o) {
  minn[o] = min(minn[o * 2], minn[o * 2 + 1]);
  maxn[o] = max(maxn[o * 2], maxn[o * 2 + 1]);
  if (minn[o * 2] < minn[o * 2 + 1]) {
    maxl[o] = maxl[o * 2];
    maxr[o] = max(maxr[o * 2], maxn[o * 2 + 1]);
    sumv[o] = sumv[o * 2];
  } else if (minn[o * 2 + 1] < minn[o * 2]) {
    maxl[o] = max(maxn[o * 2], maxl[o * 2 + 1]);
    maxr[o] = maxr[o * 2 + 1];
    sumv[o] = sumv[o * 2 + 1];
  } else {
    maxl[o] = maxl[o * 2];
    maxr[o] = maxr[o * 2 + 1];
    sumv[o] = sumv[o * 2] + sumv[o * 2 + 1] + max(maxr[o * 2], maxl[o * 2 + 1]);
  }
}
void update1(int l, int r, int o, int lx, int rx, int p) {
  if (l >= lx && r <= rx) {
    addv[o] += p;
    minn[o] += p;
  } else {
    pushdown(o);
    int m = ((l + r) >> 1);
    if (m >= lx) update1(l, m, o * 2, lx, rx, p);
    if (m < rx) update1(m + 1, r, o * 2 + 1, lx, rx, p);
    pushup(o);
  }
}
void update2(int l, int r, int o, int p, int q) {
  if (l == r)
    maxn[o] = maxl[o] = q;
  else {
    pushdown(o);
    int m = ((l + r) >> 1);
    if (m >= p)
      update2(l, m, o * 2, p, q);
    else
      update2(m + 1, r, o * 2 + 1, p, q);
    pushup(o);
  }
}
int query() { return sumv[1] + maxl[1]; }
}  // namespace SGT
set<int> st[200005];
int n;
void erase(int x) {
  if (st[x].empty()) return;
  int u = *(st[x].begin()), v = *(st[x].rbegin());
  SGT::update2(1, n, 1, u, 0);
  if (u < v) SGT::update1(1, n, 1, u, v - 1, -1);
}
void insert(int x) {
  if (st[x].empty()) return;
  int u = *(st[x].begin()), v = *(st[x].rbegin());
  SGT::update2(1, n, 1, u, st[x].size());
  if (u < v) SGT::update1(1, n, 1, u, v - 1, 1);
}
int num[200005];
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    st[x].insert(i);
    num[i] = x;
  }
  for (int i = 1; i <= 2e5; i++) insert(i);
  printf("%d\n", n - SGT::query());
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    erase(num[x]);
    st[num[x]].erase(x);
    insert(num[x]);
    erase(y);
    st[y].insert(x);
    insert(y);
    num[x] = y;
    printf("%d\n", n - SGT::query());
  }
  return 0;
}