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 long long maxn = 4e5 + 7, mod = 998244353, MAXN = 1e6 + 7;
const double eps = 1e-9;
const long long inf = 1e18;
mt19937 rnd(time(0));
struct node {
  long long left_mx;
  long long right_mx;
  long long mn;
  long long mx;
  long long sum;
  long long push;
};
long long len = 1;
vector<node> tree;
void build() {
  while (len < maxn) len <<= 1;
  tree.resize(len + len - 1, {0, 0, 0, 0, 0, 0});
}
node merge(node L, node R) {
  node M;
  if (L.mn < R.mn) {
    M.left_mx = L.left_mx;
    M.right_mx = max(L.right_mx, R.mx);
    M.mn = L.mn;
    M.mx = max(L.mx, R.mx);
    M.sum = L.sum - L.right_mx + max(L.right_mx, R.mx);
    M.push = 0;
  } else if (L.mn > R.mn) {
    M.left_mx = max(R.left_mx, L.mx);
    M.right_mx = R.right_mx;
    M.mn = R.mn;
    M.mx = max(L.mx, R.mx);
    M.sum = R.sum - R.left_mx + max(R.left_mx, L.mx);
    M.push = 0;
  } else {
    M.left_mx = L.left_mx;
    M.right_mx = R.right_mx;
    M.mn = L.mn;
    M.mx = max(L.mx, R.mx);
    M.sum = L.sum + R.sum - L.right_mx - R.left_mx + max(L.right_mx, R.left_mx);
    M.push = 0;
  }
  return M;
}
void push(long long rt, long long l, long long r) {
  long long push = tree[rt].push;
  tree[rt].push = 0;
  tree[rt].mn += push;
  if (l != r) {
    tree[rt * 2 + 1].push += push;
    tree[rt * 2 + 2].push += push;
  }
}
void add(long long l, long long r, long long l1, long long r1, long long rt,
         long long x) {
  push(rt, l, r);
  if (l >= l1 && r <= r1) {
    tree[rt].push += x;
    push(rt, l, r);
    return;
  }
  if (max(l, l1) > min(r, r1)) return;
  long long t = (l + r) / 2;
  add(l, t, l1, r1, rt * 2 + 1, x);
  add(t + 1, r, l1, r1, rt * 2 + 2, x);
  tree[rt] = merge(tree[rt * 2 + 1], tree[rt * 2 + 2]);
}
void change(long long l, long long r, long long l1, long long r1, long long rt,
            long long x) {
  push(rt, l, r);
  if (l >= l1 && r <= r1) {
    tree[rt] = {0, 0, tree[rt].mn, x, 0, 0};
    tree[rt].mx = x;
    return;
  }
  if (max(l, l1) > min(r, r1)) return;
  long long t = (l + r) / 2;
  change(l, t, l1, r1, rt * 2 + 1, x);
  change(t + 1, r, l1, r1, rt * 2 + 2, x);
  tree[rt] = merge(tree[rt * 2 + 1], tree[rt * 2 + 2]);
}
long long get() {
  push(0, 0, len - 1);
  if (tree[0].mn == 0) return tree[0].sum;
  return 0;
}
long long n, q;
long long a[maxn];
set<long long> value[maxn];
long long L[maxn], R[maxn];
void solve() {
  for (long long i = 0; i < maxn; ++i) {
    L[i] = inf;
    R[i] = -inf;
  }
  build();
  cin >> n >> q;
  for (long long i = 0; i < n; ++i) cin >> a[i];
  for (long long i = 0; i < n; ++i) {
    value[a[i]].insert(i);
    L[a[i]] = min(L[a[i]], i);
    R[a[i]] = i;
  }
  for (long long i = 0; i < maxn; ++i) {
    if (L[i] != inf) {
      change(0, len - 1, 2 * L[i] + 1, 2 * L[i] + 1, 0, value[i].size());
      add(0, len - 1, 2 * L[i] + 1, 2 * R[i] + 1, 0, 1);
    }
  }
  cout << n - get() << '\n';
  for (long long test = 0; test < q; ++test) {
    long long id, x;
    cin >> id >> x, --id;
    long long xx = a[id];
    a[id] = x;
    if (x == xx) {
      cout << n - get() << '\n';
      continue;
    }
    add(0, len - 1, 2 * L[xx] + 1, 2 * R[xx] + 1, 0, -1);
    change(0, len - 1, 2 * L[xx] + 1, 2 * L[xx] + 1, 0, 0);
    add(0, len - 1, 2 * L[x] + 1, 2 * R[x] + 1, 0, -1);
    change(0, len - 1, 2 * L[x] + 1, 2 * L[x] + 1, 0, 0);
    value[x].insert(id);
    value[xx].erase(id);
    L[xx] = inf;
    R[xx] = -inf;
    L[x] = inf;
    R[x] = -inf;
    if (value[xx].size()) {
      L[xx] = *value[xx].begin();
      R[xx] = *(--value[xx].end());
    }
    if (value[x].size()) {
      L[x] = *value[x].begin();
      R[x] = *(--value[x].end());
    }
    if (L[xx] != inf) {
      add(0, len - 1, 2 * L[xx] + 1, 2 * R[xx] + 1, 0, 1);
      change(0, len - 1, 2 * L[xx] + 1, 2 * L[xx] + 1, 0, value[xx].size());
    }
    if (L[x] != inf) {
      add(0, len - 1, 2 * L[x] + 1, 2 * R[x] + 1, 0, 1);
      change(0, len - 1, 2 * L[x] + 1, 2 * L[x] + 1, 0, value[x].size());
    }
    cout << n - get() << '\n';
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(20);
  cout << fixed;
  long long t = 1;
  for (long long i = 0; i < t; ++i) {
    solve();
  }
  return 0;
}