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 main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int m = 1 << 19;
  vector<int> tree_lazy((size_t)m * 2 + 1, 0);
  vector<int> tree_min((size_t)m * 2 + 1, 0);
  vector<int> tree_count((size_t)m * 2 + 1, 0);
  vector<int> tree_max((size_t)m * 2 + 1, 0);
  vector<int> tree_sum((size_t)m * 2 + 1, 0);
  vector<int> tree_left((size_t)m * 2 + 1, 0);
  vector<int> tree_right((size_t)m * 2 + 1, 0);
  function<void(int, int, int)> tree_build = [&](int tree_v, int tree_l,
                                                 int tree_r) {
    tree_count[tree_v] = tree_r - tree_l + 1;
    if (tree_l != tree_r) {
      tree_build(tree_v * 2 + 0, tree_l, tree_l + (tree_r - tree_l) / 2);
      tree_build(tree_v * 2 + 1, tree_r - (tree_r - tree_l) / 2, tree_r);
    }
  };
  tree_build(1, 0, m - 1);
  vector<int> c((size_t)m, 0), d((size_t)m, 0);
  function<void(int, int, int, int, int, int, int, int)> tree_update =
      [&](int tree_v, int tree_l, int tree_r, int l, int r, int x, int y,
          int z) {
        if (tree_l != tree_r) {
          tree_min[tree_v * 2 + 0] += tree_lazy[tree_v];
          tree_min[tree_v * 2 + 1] += tree_lazy[tree_v];
          tree_lazy[tree_v * 2 + 0] += tree_lazy[tree_v];
          tree_lazy[tree_v * 2 + 1] += tree_lazy[tree_v];
          tree_lazy[tree_v] = 0;
        }
        if (tree_l >= l && tree_r <= r) {
          tree_min[tree_v] += x;
          tree_lazy[tree_v] += x;
          if (y != -1) {
            tree_right[tree_v] = tree_left[tree_v] = tree_sum[tree_v] =
                tree_max[tree_v] = y;
          }
        } else if (tree_l <= r && tree_r >= l) {
          tree_update(tree_v * 2 + 0, tree_l, tree_l + (tree_r - tree_l) / 2, l,
                      r, x, y, z);
          tree_update(tree_v * 2 + 1, tree_r - (tree_r - tree_l) / 2, tree_r, l,
                      r, x, y, z);
          tree_min[tree_v] =
              min(tree_min[tree_v * 2 + 0], tree_min[tree_v * 2 + 1]);
          tree_max[tree_v] =
              max(tree_max[tree_v * 2 + 0], tree_max[tree_v * 2 + 1]);
          if (tree_min[tree_v * 2 + 0] < tree_min[tree_v * 2 + 1]) {
            tree_count[tree_v] = tree_count[tree_v * 2 + 0];
            tree_sum[tree_v] =
                (tree_sum[tree_v * 2 + 0] - tree_right[tree_v * 2 + 0]) +
                max(tree_right[tree_v * 2 + 0], tree_max[tree_v * 2 + 1]);
            tree_left[tree_v] = tree_left[tree_v * 2 + 0];
            tree_right[tree_v] =
                max(tree_right[tree_v * 2 + 0], tree_max[tree_v * 2 + 1]);
          } else if (tree_min[tree_v * 2 + 1] < tree_min[tree_v * 2 + 0]) {
            tree_count[tree_v] = tree_count[tree_v * 2 + 1];
            tree_sum[tree_v] =
                max(tree_max[tree_v * 2 + 0], tree_left[tree_v * 2 + 1]) +
                (tree_sum[tree_v * 2 + 1] - tree_left[tree_v * 2 + 1]);
            tree_left[tree_v] =
                max(tree_max[tree_v * 2 + 0], tree_left[tree_v * 2 + 1]);
            tree_right[tree_v] = tree_right[tree_v * 2 + 1];
          } else {
            tree_count[tree_v] =
                tree_count[tree_v * 2 + 0] + tree_count[tree_v * 2 + 1];
            tree_sum[tree_v] =
                (tree_sum[tree_v * 2 + 0] - tree_right[tree_v * 2 + 0]) +
                max(tree_right[tree_v * 2 + 0], tree_left[tree_v * 2 + 1]) +
                (tree_sum[tree_v * 2 + 1] - tree_left[tree_v * 2 + 1]);
            tree_left[tree_v] = tree_left[tree_v * 2 + 0];
            tree_right[tree_v] = tree_right[tree_v * 2 + 1];
          }
        }
      };
  int n, q;
  cin >> n >> q;
  vector<int> a((size_t)n);
  vector<set<int>> b((size_t)200000 + 1);
  for (int i = 0; i < n; i++) {
    b[0].insert(i);
  }
  tree_update(1, 0, m - 1, 0, (n - 1) * 2, 1, -1, 0);
  tree_update(1, 0, m - 1, n * 2, m - 1, 1 << 18, -1, 0);
  tree_update(1, 0, m - 1, 0, 0, 0, n, 0);
  auto update = [&](int x, int y) {
    int z = a[x];
    for (int w : {y, z}) {
      if (!b[w].empty()) {
        tree_update(1, 0, m - 1, *b[w].begin() * 2, *b[w].rbegin() * 2, -1, -1,
                    0);
        tree_update(1, 0, m - 1, *b[w].begin() * 2, *b[w].begin() * 2, 0, 0, 0);
      }
    }
    b[z].erase(x);
    a[x] = y;
    b[y].insert(x);
    for (int w : {y, z}) {
      if (!b[w].empty()) {
        tree_update(1, 0, m - 1, *b[w].begin() * 2, *b[w].rbegin() * 2, 1, -1,
                    0);
        tree_update(1, 0, m - 1, *b[w].begin() * 2, *b[w].begin() * 2, 0,
                    b[w].size(), 0);
      }
    }
  };
  for (int i = 0; i < n; i++) {
    int j;
    cin >> j;
    update(i, j);
  }
  cout << n - tree_sum[1] << "\n";
  for (int t = 0; t < q; t++) {
    int x, y;
    cin >> x >> y;
    x--;
    update(x, y);
    cout << n - tree_sum[1] << "\n";
  }
  return 0;
}