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 MAXN = 400200;
int arr[MAXN];
set<int> occs[MAXN];
struct ST {
  ST *left, *right;
  int L, R;
  int minA, deltaA, firstMin, lastMin;
  int maxSuffix, maxPrefix, maxTotal;
  int sumBet;
  ST(int a, int b) {
    L = a, R = b;
    minA = 0, deltaA = 0, firstMin = L, lastMin = L;
    maxSuffix = 0, maxPrefix = 0, maxTotal = 0;
    sumBet = 0;
    if (L == R) {
      return;
    } else {
      int mid = (a + b) / 2;
      left = new ST(a, mid);
      right = new ST(mid + 1, b);
      merge();
    }
  }
  int getMin() { return deltaA + minA; }
  void prop() {
    if (deltaA != 0) return;
    left->deltaA += deltaA;
    right->deltaA += deltaA;
    deltaA = 0;
  }
  void merge() {
    if (left->getMin() == right->getMin()) {
      minA = left->getMin(), firstMin = left->firstMin;
      lastMin = right->lastMin, maxSuffix = right->maxSuffix;
      maxPrefix = left->maxPrefix,
      maxTotal = max(left->maxTotal, right->maxTotal);
      sumBet = left->sumBet + right->sumBet +
               (max(left->maxSuffix, right->maxPrefix));
    } else if (left->getMin() < right->getMin()) {
      minA = left->getMin(), firstMin = left->firstMin;
      lastMin = left->lastMin,
      maxSuffix = max(left->maxSuffix, right->maxTotal);
      maxPrefix = left->maxPrefix,
      maxTotal = max(left->maxTotal, right->maxTotal);
      sumBet = left->sumBet;
    } else {
      minA = right->getMin(), firstMin = right->firstMin;
      lastMin = right->lastMin, maxSuffix = right->maxSuffix;
      maxPrefix = max(left->maxTotal, right->maxPrefix),
      maxTotal = max(left->maxTotal, right->maxTotal);
      sumBet = right->sumBet;
    }
  }
  void rangeAdd(int a, int b, int val) {
    if (b < L || R < a) return;
    if (a <= L && R <= b) {
      deltaA += val;
      return;
    }
    prop();
    left->rangeAdd(a, b, val);
    right->rangeAdd(a, b, val);
    merge();
  }
  void pointUpdate(int idx, int val) {
    if (L == R) {
      maxSuffix = maxPrefix = sumBet = 0;
      maxTotal = val;
      return;
    }
    prop();
    if (idx <= left->R)
      left->pointUpdate(idx, val);
    else
      right->pointUpdate(idx, val);
    merge();
  }
};
void sol() {
  int N, Q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
    occs[arr[i]].insert(i);
  }
  ST *root = new ST(0, MAXN - 1);
  for (int i = 1; i <= 200000; i++)
    if ((int)occs[i].size()) {
      int L = *(occs[i].begin());
      int R = *(prev(occs[i].end()));
      root->pointUpdate(2 * L + 1, (int)occs[i].size());
      L = 2 * L + 1, R = 2 * R + 1;
      root->rangeAdd(L, R, 1);
    }
  cout << N - (root->sumBet) << '\n';
  auto removeElem = [&](int idx, int oldVal) {
    if (idx == *occs[oldVal].begin()) {
      root->pointUpdate(2 * idx + 1, 0);
      int L = 2 * idx + 1;
      occs[oldVal].erase(occs[oldVal].begin());
      int R = L;
      if ((int)occs[oldVal].size()) R = 2 * (*occs[oldVal].begin());
      root->rangeAdd(L, R, -1);
    } else if (idx == *(prev(occs[oldVal].end()))) {
      int R = 2 * idx + 1;
      occs[oldVal].erase(prev(occs[oldVal].end()));
      int L = R;
      if ((int)occs[oldVal].size()) {
        int newLast = *(prev(occs[oldVal].end()));
        L = 2 * newLast + 2;
      }
      root->rangeAdd(L, R, -1);
    } else {
      occs[oldVal].erase(occs[oldVal].lower_bound(idx));
    }
    if ((int)occs[oldVal].size()) {
      int beg = *occs[oldVal].begin();
      root->pointUpdate(2 * beg + 1, (int)occs[oldVal].size());
    }
  };
  auto addElem = [&](int idx, int val) {
    if (!(int)occs[val].size()) {
      occs[val].insert(idx);
      root->pointUpdate(2 * idx + 1, 1);
      root->rangeAdd(2 * idx + 1, 2 * idx + 1, 1);
      return;
    }
    int first = *occs[val].begin();
    int last = *prev(occs[val].end());
    occs[val].insert(idx);
    if (idx < first) {
      root->pointUpdate(2 * first + 1, 0);
      root->pointUpdate(2 * idx + 1, (int)occs[val].size());
      root->rangeAdd(2 * idx + 1, 2 * first, 1);
    } else if (idx > last) {
      root->pointUpdate(2 * first + 1, (int)occs[val].size());
      root->rangeAdd(2 * last + 2, 2 * idx + 1, 1);
    } else {
      root->pointUpdate(2 * first + 1, (int)occs[val].size());
    }
  };
  for (int qq = 1; qq <= Q; qq++) {
    int idx, nxtVal;
    cin >> idx >> nxtVal;
    idx--;
    int oldVal = arr[idx];
    removeElem(idx, oldVal);
    arr[idx] = nxtVal;
    addElem(idx, nxtVal);
    cout << N - (root->sumBet) << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  sol();
  return 0;
}