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 read() {
  int a = 0;
  char c = getchar();
  bool f = 0;
  while (!isdigit(c)) {
    f = c == '-';
    c = getchar();
  }
  while (isdigit(c)) {
    a = a * 10 + c - 48;
    c = getchar();
  }
  return f ? -a : a;
}
const int _ = 2e5 + 11;
struct DATA {
  int mn, mx, lftmx, rhtmx, sum, mrk;
  DATA() {
    mn = 0;
    lftmx = rhtmx = sum = mrk = 0;
  }
  friend DATA merge(DATA &p, DATA &q) {
    DATA t;
    t.mx = max(p.mx, q.mx);
    if (p.mn == q.mn) {
      t.lftmx = p.lftmx;
      t.rhtmx = q.rhtmx;
      t.mn = p.mn;
      t.sum = p.sum + q.sum + max(p.rhtmx, q.lftmx);
    } else if (p.mn < q.mn) {
      t.mn = p.mn;
      t.sum = p.sum;
      t.lftmx = p.lftmx;
      t.rhtmx = max(p.rhtmx, q.mx);
    } else {
      t.mn = q.mn;
      t.sum = q.sum;
      t.lftmx = max(p.mx, q.lftmx);
      t.rhtmx = q.rhtmx;
    }
    return t;
  }
  void mark(int x) {
    mrk += x;
    mn += x;
  }
};
namespace segt {
DATA now[_ << 3];
void down(int x) {
  now[(x << 1)].mark(now[x].mrk);
  now[(x << 1 | 1)].mark(now[x].mrk);
  now[x].mrk = 0;
}
void M1(int x, int l, int r, int L, int R, int val) {
  if (l >= L && r <= R) return now[x].mark(val);
  down(x);
  if (((l + r) >> 1) >= L) M1((x << 1), l, ((l + r) >> 1), L, R, val);
  if (((l + r) >> 1) < R) M1((x << 1 | 1), ((l + r) >> 1) + 1, r, L, R, val);
  now[x] = merge(now[(x << 1)], now[(x << 1 | 1)]);
}
void M2(int x, int l, int r, int tar, int val) {
  if (l == r) {
    now[x].mx = val;
    return;
  }
  down(x);
  ((l + r) >> 1) >= tar ? M2((x << 1), l, ((l + r) >> 1), tar, val)
                        : M2((x << 1 | 1), ((l + r) >> 1) + 1, r, tar, val);
  now[x] = merge(now[(x << 1)], now[(x << 1 | 1)]);
}
}  // namespace segt
int N, Q, arr[_];
set<int> pos[_];
int main() {
  N = read();
  Q = read();
  for (int i = 1; i <= N; ++i) pos[arr[i] = read()].insert(i);
  for (int i = 1; i <= 200000; ++i)
    if (pos[i].size()) {
      segt::M2(1, 1, 2 * N + 1, 2 * (*pos[i].begin()), pos[i].size());
      segt::M1(1, 1, 2 * N + 1, 2 * (*pos[i].begin()), 2 * (*--pos[i].end()),
               1);
    }
  printf("%d\n", N - segt::now[1].sum);
  while (Q--) {
    int p = read(), x = read(), t = arr[p];
    if (pos[t].size() != 1)
      if (p == *pos[t].begin()) {
        segt::M2(1, 1, 2 * N + 1, 2 * p, 0);
        segt::M2(1, 1, 2 * N + 1, 2 * (*++pos[t].begin()), pos[t].size() - 1);
        segt::M1(1, 1, 2 * N + 1, 2 * p, 2 * (*++pos[t].begin()) - 1, -1);
      } else {
        if (p == *--pos[t].end())
          segt::M1(1, 1, 2 * N + 1, 2 * (*-- --pos[t].end()) + 1,
                   2 * (*--pos[t].end()), -1);
        segt::M2(1, 1, 2 * N + 1, 2 * (*pos[t].begin()), pos[t].size() - 1);
      }
    else {
      segt::M2(1, 1, 2 * N + 1, 2 * p, 0);
      segt::M1(1, 1, 2 * N + 1, 2 * p, 2 * p, -1);
    }
    pos[t].erase(p);
    pos[arr[p] = x].insert(p);
    if (pos[x].size() != 1)
      if (p == *pos[x].begin()) {
        segt::M2(1, 1, 2 * N + 1, 2 * p, pos[x].size());
        segt::M2(1, 1, 2 * N + 1, 2 * (*++pos[x].begin()), 0);
        segt::M1(1, 1, 2 * N + 1, 2 * p, 2 * (*++pos[x].begin()) - 1, 1);
      } else {
        if (p == *--pos[x].end())
          segt::M1(1, 1, 2 * N + 1, 2 * (*-- --pos[x].end()) + 1,
                   2 * (*--pos[x].end()), 1);
        segt::M2(1, 1, 2 * N + 1, 2 * (*pos[x].begin()), pos[x].size());
      }
    else {
      segt::M2(1, 1, 2 * N + 1, 2 * p, 1);
      segt::M1(1, 1, 2 * N + 1, 2 * p, 2 * p, 1);
    }
    printf("%d\n", N - segt::now[1].sum);
  }
  return 0;
}