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;
struct Tree {
  int mn, mx;
  int val;
  int premx;
  int sufmx;
} T[800010];
int lazy[800010];
Tree operator+(const Tree& A, const Tree& B) {
  Tree ans;
  ans.mn = min(A.mn, B.mn);
  ans.mx = max(A.mx, B.mx);
  if (A.mn == ans.mn) {
    if (B.mn == ans.mn) {
      ans.val = A.val + B.val + max(A.sufmx, B.premx);
      ans.premx = A.premx;
      ans.sufmx = B.sufmx;
    } else {
      ans.val = A.val;
      ans.premx = A.premx;
      ans.sufmx = max(A.sufmx, B.mx);
    }
  } else {
    ans.val = B.val;
    ans.premx = max(A.mx, B.premx);
    ans.sufmx = B.sufmx;
  }
  return ans;
}
void update(int root) {
  T[root] = T[root << 1] + T[root << 1 | 1];
  return;
}
void pushdown(int root) {
  lazy[root << 1] += lazy[root];
  lazy[root << 1 | 1] += lazy[root];
  T[root << 1].mn += lazy[root];
  T[root << 1 | 1].mn += lazy[root];
  lazy[root] = 0;
  return;
}
void build(int root, int l, int r) {
  if (l == r) {
    T[root].mn = -l;
    T[root].mx = 0;
    T[root].premx = 0;
    T[root].sufmx = 0;
    T[root].val = 0;
    return;
  }
  int mid = (l + r) >> 1;
  build(root << 1, l, mid);
  build(root << 1 | 1, mid + 1, r);
  update(root);
  return;
}
void add(int root, int l, int r, int x, int y, int ad) {
  if (x <= l && r <= y) {
    lazy[root] += ad;
    T[root].mn += ad;
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(root);
  if (x <= mid) add(root << 1, l, mid, x, y, ad);
  if (y > mid) add(root << 1 | 1, mid + 1, r, x, y, ad);
  update(root);
  return;
}
void change(int root, int l, int r, int x, int y) {
  if (l == r) {
    T[root].mx = y;
    T[root].premx = y;
    T[root].sufmx = 0;
    T[root].val = 0;
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(root);
  if (x <= mid)
    change(root << 1, l, mid, x, y);
  else
    change(root << 1 | 1, mid + 1, r, x, y);
  update(root);
  return;
}
set<int> S[200010];
int n, q, M;
void Add(int x, int pos) {
  if (S[x].size() == 0)
    M++;
  else {
    int num = *(S[x].begin());
    int cnt = S[x].size();
    add(1, 1, n, num, n, -cnt);
    change(1, 1, n, num, 0);
  }
  S[x].insert(pos);
  int num = *(S[x].begin());
  int cnt = S[x].size();
  add(1, 1, n, num, n, cnt);
  change(1, 1, n, num, cnt);
  return;
}
void Del(int x, int pos) {
  int num = *(S[x].begin());
  int cnt = S[x].size();
  add(1, 1, n, num, n, -cnt);
  change(1, 1, n, num, 0);
  S[x].erase(pos);
  if (S[x].size() == 0)
    M--;
  else {
    num = *(S[x].begin());
    cnt = S[x].size();
    add(1, 1, n, num, n, cnt);
    change(1, 1, n, num, cnt);
  }
  return;
}
int a[200010];
int main() {
  scanf("%d %d", &n, &q);
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    Add(a[i], i);
  }
  printf("%d\n", n - T[1].val - T[1].premx);
  for (int i = 1; i <= q; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    Del(a[x], x);
    a[x] = y;
    Add(a[x], x);
    printf("%d\n", n - T[1].val - T[1].premx);
  }
  return 0;
}