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 = 200007;
int seq[maxn];
set<int> pos[maxn];
int cnt[maxn];
int n;
class node {
 public:
  int tag;
  int l;
  int r;
  int valb;
  int lmx;
  int rmx;
  int mx;
  int ans;
  int len;
  node() {}
};
class Segtree {
 public:
  node tre[maxn << 2];
  inline int getlen(int p) { return tre[p].r - tre[p].l + 1; }
  void push_up(int p) {
    int nl = tre[p].l;
    int nr = tre[p].r;
    if (nr == nl) return;
    tre[p].mx = max(tre[(p << 1)].mx, tre[(p << 1 | 1)].mx);
    tre[p].valb = min(tre[(p << 1)].valb, tre[(p << 1 | 1)].valb);
    if (tre[p].valb == tre[(p << 1)].valb)
      tre[p].lmx = tre[(p << 1)].lmx;
    else
      tre[p].lmx = max(tre[(p << 1)].mx, tre[(p << 1 | 1)].lmx);
    if (tre[p].valb == tre[(p << 1 | 1)].valb)
      tre[p].rmx = tre[(p << 1 | 1)].rmx;
    else
      tre[p].rmx = max(tre[(p << 1 | 1)].mx, tre[(p << 1)].rmx);
    if (tre[p].valb != tre[(p << 1 | 1)].valb) {
      tre[p].ans = tre[(p << 1)].ans;
      tre[p].len = tre[(p << 1)].len + getlen((p << 1 | 1));
    } else if (tre[p].valb != tre[(p << 1)].valb) {
      tre[p].ans = tre[(p << 1 | 1)].ans;
      tre[p].len = tre[(p << 1 | 1)].len + getlen((p << 1));
    } else {
      tre[p].ans = tre[(p << 1)].ans + tre[(p << 1 | 1)].ans;
      tre[p].len = tre[(p << 1)].len + tre[(p << 1 | 1)].len;
      if (~tre[(p << 1)].rmx || ~tre[(p << 1 | 1)].lmx)
        tre[p].ans += max(tre[(p << 1)].rmx, tre[(p << 1 | 1)].lmx),
            tre[p].len++;
    }
  }
  void build_tree(int l = 0, int r = n, int p = 1) {
    tre[p].l = l;
    tre[p].r = r;
    tre[p].tag = 0;
    if (l == r) {
      tre[p].mx = 0;
      tre[p].ans = tre[p].len = 0;
      tre[p].valb = 0;
      tre[p].lmx = tre[p].rmx = -1;
      return;
    }
    int mid = l + r >> 1;
    build_tree(l, mid, (p << 1)), build_tree(mid + 1, r, (p << 1 | 1));
    push_up(p);
  }
  inline void push_down(int p) {
    tre[(p << 1)].tag += tre[p].tag;
    tre[(p << 1 | 1)].tag += tre[p].tag;
    tre[(p << 1)].valb += tre[p].tag;
    tre[(p << 1 | 1)].valb += tre[p].tag;
    tre[p].tag = 0;
  }
  void modify(const int l, const int r, const int v, int p = 1) {
    int nl = tre[p].l;
    int nr = tre[p].r;
    if (l > r) return;
    if (nl > r || nr < l) return;
    if (nl >= l && r >= nr) {
      tre[p].valb += v;
      tre[p].tag += v;
      return;
    }
    push_down(p);
    modify(l, r, v, (p << 1)), modify(l, r, v, (p << 1 | 1));
    push_up(p);
  }
  void update(const int x, int p = 1) {
    int nl = tre[p].l;
    int nr = tre[p].r;
    if (x < nl || x > nr) return;
    if (nl == nr) {
      tre[p].mx = max(cnt[x], cnt[x + 1]);
      return;
    }
    push_down(p);
    update(x, (p << 1)), update(x, (p << 1 | 1));
    push_up(p);
  }
};
Segtree t;
void remove(int x) {
  int oi = seq[x];
  int l = *pos[oi].begin();
  int r = *pos[oi].rbegin();
  t.modify(l, r - 1, -1);
  cnt[l] = 0;
  t.update(l - 1), t.update(l);
  pos[oi].erase(x);
  if (pos[oi].size()) {
    l = *pos[oi].begin();
    r = *pos[oi].rbegin();
    t.modify(l, r - 1, 1);
    cnt[l] = pos[oi].size();
    t.update(l - 1), t.update(l);
  }
}
void add(int x) {
  int oi = seq[x];
  if (pos[oi].size()) {
    int l = *pos[oi].begin();
    int r = *pos[oi].rbegin();
    t.modify(l, r - 1, -1);
    cnt[l] = 0;
    t.update(l - 1), t.update(l);
  }
  pos[oi].insert(x);
  int l = *pos[oi].begin();
  int r = *pos[oi].rbegin();
  t.modify(l, r - 1, 1);
  cnt[l] = pos[oi].size();
  t.update(l - 1), t.update(l);
}
set<int> vv;
int cc[maxn];
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> n >> q;
  t.build_tree();
  for (int i = (1); i <= (n); ++i) {
    cin >> seq[i];
    cc[seq[i]]++;
    pos[seq[i]].insert(i);
    vv.insert(seq[i]);
  }
  for (auto x : vv) {
    int l = *pos[x].begin();
    int r = *pos[x].rbegin();
    cnt[l] = cc[x];
    t.modify(l, r - 1, 1);
    t.update(l - 1), t.update(l);
  }
  cout << t.tre[1].len - t.tre[1].ans << endl;
  while (q--) {
    int x, v;
    cin >> x >> v;
    remove(x);
    seq[x] = v;
    add(x);
    cout << t.tre[1].len - t.tre[1].ans << endl;
  }
  return 0;
}