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;
const int INF = 1000000001;
const long double EPS = 1e-9;
const int MOD = 1000000007;
const long long LLINF = 1000000000000000001;
const int maxn = (1 << 18);
struct treeNode {
  long long sum;
  int myChange;
  int minVal;
  int bestAll;
  int bestFirst;
  int bestLast;
  treeNode()
      : sum(0), myChange(0), minVal(0), bestAll(0), bestFirst(0), bestLast(0) {}
  void fromMerge(treeNode a, treeNode b) {
    minVal = min(a.minVal, b.minVal) + myChange;
    bestAll = max(a.bestAll, b.bestAll);
    if (a.minVal == b.minVal) {
      bestFirst = a.bestFirst;
      bestLast = b.bestLast;
      sum = a.sum - a.bestLast + b.sum - b.bestFirst +
            max(a.bestLast, b.bestFirst);
    } else if (a.minVal < b.minVal) {
      bestFirst = a.bestFirst;
      bestLast = max(a.bestLast, b.bestAll);
      sum = a.sum - a.bestLast + bestLast;
    } else {
      bestFirst = max(a.bestAll, b.bestFirst);
      bestLast = b.bestLast;
      sum = b.sum - b.bestFirst + bestFirst;
    }
  }
  void setBest(int a) {
    sum = a;
    bestAll = a;
    bestFirst = a;
  }
};
treeNode tree[2 * maxn];
void setBestAll(int a, int p) {
  p += maxn;
  tree[p].setBest(a);
  while (p > 1) {
    p /= 2;
    tree[p].fromMerge(tree[p * 2], tree[p * 2 + 1]);
  }
}
void addOne(int v, int p) {
  tree[p].myChange += v;
  if (p < maxn)
    tree[p].fromMerge(tree[p * 2], tree[p * 2 + 1]);
  else
    tree[p].minVal += v;
}
void segAdd(int v, int a, int b) {
  a += maxn;
  b += maxn;
  if (b < a) return;
  addOne(v, a);
  if (b != a) addOne(v, b);
  while (a / 2 != b / 2) {
    if (a % 2 == 0) addOne(v, a + 1);
    if (b % 2 == 1) addOne(v, b - 1);
    a /= 2;
    b /= 2;
    tree[a].fromMerge(tree[a * 2], tree[a * 2 + 1]);
    tree[b].fromMerge(tree[b * 2], tree[b * 2 + 1]);
  }
  while (a > 1) {
    a /= 2;
    tree[a].fromMerge(tree[a * 2], tree[a * 2 + 1]);
  }
}
int a[maxn];
set<int> positions[maxn];
void addNum(int v) {
  segAdd(1, *positions[v].begin(), *positions[v].rbegin() - 1);
  setBestAll(((int)(positions[v]).size()), *positions[v].rbegin());
}
void reverseNum(int v) {
  segAdd(-1, *positions[v].begin(), *positions[v].rbegin() - 1);
  setBestAll(0, *positions[v].rbegin());
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    positions[a[i]].insert(i);
  }
  segAdd(INF, 0, 0);
  segAdd(INF, n + 1, maxn - 1);
  for (int i = 1; i <= 200000; i++)
    if (!positions[i].empty()) addNum(i);
  cout << n - tree[1].sum << '\n';
  for (int i = 1; i <= q; i++) {
    int p, x;
    cin >> p >> x;
    reverseNum(a[p]);
    positions[a[p]].erase(p);
    if (!positions[a[p]].empty()) addNum(a[p]);
    if (!positions[x].empty()) reverseNum(x);
    positions[x].insert(p);
    addNum(x);
    a[p] = x;
    cout << n - tree[1].sum << '\n';
  }
}