/*
Codeforces Round #699 (Div. 2) - E. Sorting Books
https://codeforces.com/contest/1481/problem/E
*/

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
 
double EPS=1e-9;
int INF=1000000005;
long long INFF=1000000000000000005ll;
double PI=acos(-1);
int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 };
ll MOD = 1000000007;
 
// ll sum() { return 0; }
// template<typename T, typename... Args>
// T sum(T a, Args... args) { return a + sum(args...); }
 
#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define OUT(x) cout << x << endl
#define OUTH(x) cout << x << " "
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define READ(x) for(auto &(z):x) cin >> z;
#define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a))
#define EACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a=max(a, b)
#define MIN(a, b) a=min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define what_is(x) cerr << #x << " is " << x << endl


void solve() {
	int n; cin >> n;
	vi a(n), freq(n);
	vii v(n, {-1, -1}); 
	REP(i, n) {
		cin >> a[i];
		a[i]--;
		freq[a[i]]++;
		// update leftmost occurrence
		if(v[a[i]].fi == -1) v[a[i]] = {i, i}; 
		// update rightmost occurence
		v[a[i]].se = i;
	}
	// dp[i] : max unmoved elements in the suffix [i, n]
	vi dp(n + 1);
	// cnt[a[i]]: number of elements with color a[i] in range [i, n]
	vi cnt(n);
	FORD(i, n - 1, 0) {
		dp[i] = dp[i + 1];
		cnt[a[i]]++;
		// leave all a[i] in [l, r] unmoved n move everything else within [l, r] to the end
		if(i == v[a[i]].fi) MAX(dp[i], dp[v[a[i]].se + 1] + freq[a[i]]);
		// keep all a[i] in [i, n] unmoved n move everything else 
		else MAX(dp[i], cnt[a[i]]);
	}
	OUT(n - dp[0]);
}
 
 
 
int main()
{
    FAST_INP;
//    #ifndef ONLINE_JUDGE
//    freopen("input.txt","r", stdin);
//    freopen("output.txt","w", stdout);
//    #endif
 
    // int tc; cin >> tc;
    // TC(tc) solve();
    solve();
    return 0;
}
### Prompt

Please provide a cpp coded solution to the problem described below:

One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first.

<image>

There are n books standing in a row on the shelf, the i-th book has color a_i.

You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other.

In one operation you can take one book from any position on the shelf and move it to the right end of the shelf.

What is the minimum number of operations you need to make the shelf beautiful?

Input

The first line contains one integer n (1 ≤ n ≤ 5 ⋅ 10^5) — the number of books.

The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — the book colors.

Output

Output the minimum number of operations to make the shelf beautiful.

Examples

Input


5
1 2 2 1 3


Output


2


Input


5
1 2 2 1 1


Output


1

Note

In the first example, we have the bookshelf [1, 2, 2, 1, 3] and can, for example: 

  1. take a book on position 4 and move to the right end: we'll get [1, 2, 2, 3, 1]; 
  2. take a book on position 1 and move to the right end: we'll get [2, 2, 3, 1, 1]. 



In the second example, we can move the first book to the end of the bookshelf and get [2,2,1,1,1].

### Response

```cpp
/*
Codeforces Round #699 (Div. 2) - E. Sorting Books
https://codeforces.com/contest/1481/problem/E
*/

#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
 
double EPS=1e-9;
int INF=1000000005;
long long INFF=1000000000000000005ll;
double PI=acos(-1);
int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 };
ll MOD = 1000000007;
 
// ll sum() { return 0; }
// template<typename T, typename... Args>
// T sum(T a, Args... args) { return a + sum(args...); }
 
#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define OUT(x) cout << x << endl
#define OUTH(x) cout << x << " "
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define READ(x) for(auto &(z):x) cin >> z;
#define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a))
#define EACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a=max(a, b)
#define MIN(a, b) a=min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define what_is(x) cerr << #x << " is " << x << endl


void solve() {
	int n; cin >> n;
	vi a(n), freq(n);
	vii v(n, {-1, -1}); 
	REP(i, n) {
		cin >> a[i];
		a[i]--;
		freq[a[i]]++;
		// update leftmost occurrence
		if(v[a[i]].fi == -1) v[a[i]] = {i, i}; 
		// update rightmost occurence
		v[a[i]].se = i;
	}
	// dp[i] : max unmoved elements in the suffix [i, n]
	vi dp(n + 1);
	// cnt[a[i]]: number of elements with color a[i] in range [i, n]
	vi cnt(n);
	FORD(i, n - 1, 0) {
		dp[i] = dp[i + 1];
		cnt[a[i]]++;
		// leave all a[i] in [l, r] unmoved n move everything else within [l, r] to the end
		if(i == v[a[i]].fi) MAX(dp[i], dp[v[a[i]].se + 1] + freq[a[i]]);
		// keep all a[i] in [i, n] unmoved n move everything else 
		else MAX(dp[i], cnt[a[i]]);
	}
	OUT(n - dp[0]);
}
 
 
 
int main()
{
    FAST_INP;
//    #ifndef ONLINE_JUDGE
//    freopen("input.txt","r", stdin);
//    freopen("output.txt","w", stdout);
//    #endif
 
    // int tc; cin >> tc;
    // TC(tc) solve();
    solve();
    return 0;
}
```