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>
#pragma GCC optimize("O3")
using namespace std;
template <class C>
void mini(C &a4, C b4) {
  a4 = min(a4, b4);
}
template <class C>
void maxi(C &a4, C b4) {
  a4 = max(a4, b4);
}
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V) os << vv << ",";
  return os << "]";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
const long long MAX = 200100;
set<long long> gd[MAX];
struct node {
  long long cal;
  long long naj;
  long long najpref;
  long long najsuf;
  long long pref;
  long long suf;
  bool ok;
} dp[1 << 19];
long long t[MAX];
long long n, nn, q;
void ak(long long v) {
  long long sze = 1;
  while (v != 1) {
    v /= 2;
    dp[v].naj = max(dp[v * 2].naj, dp[v * 2 + 1].naj);
    dp[v].cal = 0;
    dp[v].ok = 0;
    dp[v].najpref = 0;
    dp[v].najsuf = 0;
    dp[v].pref = dp[v * 2].pref;
    dp[v].suf = dp[v * 2 + 1].suf;
    if (dp[v * 2].suf > sze) {
      maxi(dp[v].suf, dp[v * 2].suf - sze);
      maxi(dp[v].najsuf, max(dp[v * 2 + 1].naj, dp[v * 2].najsuf));
    } else {
      if (dp[v * 2 + 1].pref <= sze) {
        if (dp[v * 2].ok && dp[v * 2 + 1].ok) {
          dp[v].cal += max(dp[v * 2 + 1].najpref, dp[v * 2].najsuf);
        }
        if (!dp[v * 2].ok) {
          maxi(dp[v].najpref, max(dp[v * 2 + 1].najpref, dp[v * 2].najsuf));
        }
        if (!dp[v * 2 + 1].ok) {
          maxi(dp[v].najsuf, max(dp[v * 2 + 1].najpref, dp[v * 2].najsuf));
        }
      }
      dp[v].ok |= dp[v * 2 + 1].ok;
      dp[v].cal += dp[v * 2 + 1].cal;
      maxi(dp[v].najsuf, dp[v * 2 + 1].najsuf);
    }
    if (dp[v * 2 + 1].pref > sze) {
      maxi(dp[v].pref, dp[v * 2 + 1].pref - sze);
      maxi(dp[v].najpref, max(dp[v * 2].naj, dp[v * 2 + 1].najpref));
    } else {
      dp[v].ok |= dp[v * 2].ok;
      dp[v].cal += dp[v * 2].cal;
      maxi(dp[v].najpref, dp[v * 2].najpref);
    }
    (v, dp[v].cal, dp[v].naj, dp[v].najpref, dp[v].najsuf, dp[v].pref,
     dp[v].suf, dp[v].ok);
    sze *= 2;
  }
}
void dodaj(long long i) {
  long long kr = ((long long)(gd[i]).size());
  if (!kr) return;
  long long a = *gd[i].begin();
  if (kr == 1) {
    dp[a + nn] = {1, 1, 0, 0, 0, 0, true};
    ak(a + nn);
  } else {
    long long b = *gd[i].rbegin();
    dp[a + nn] = {0, kr, 0, kr, 0, b - a, true};
    dp[b + nn] = {0, kr, kr, 0, b - a, 0, true};
    ak(a + nn);
    ak(b + nn);
  }
}
void us(long long i) {
  node empty = {0, 0, 0, 0, 0, 0, false};
  long long kr = ((long long)(gd[i]).size());
  if (!kr) return;
  long long a = *gd[i].begin();
  if (kr == 1) {
    dp[a + nn] = empty;
    ak(a + nn);
  } else {
    long long b = *gd[i].rbegin();
    dp[a + nn] = empty;
    dp[b + nn] = empty;
    ak(a + nn);
    ak(b + nn);
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  if (0) cout << fixed << setprecision(6);
  cin >> n >> q;
  nn = 1;
  while (nn <= n) nn *= 2;
  for (long long i = (0); i <= ((long long)(n)-1); i++) {
    cin >> t[i];
    gd[t[i]].insert(i);
  }
  for (long long i = (0); i <= ((long long)(MAX)-1); i++) dodaj(i);
  cout << n - dp[1].cal << "\n";
  for (long long i = (0); i <= ((long long)(q)-1); i++) {
    long long a, v;
    cin >> a >> v;
    a--;
    us(t[a]);
    gd[t[a]].erase(a);
    dodaj(t[a]);
    us(v);
    t[a] = v;
    gd[v].insert(a);
    dodaj(v);
    cout << n - dp[1].cal << "\n";
  }
}