Bubble Sort

Bubble sort is one of the algorithms for sorting columns. Suppose you want to sort the sequence A of length N in ascending order. Bubble sort exchanges the positions of two adjacent numbers if the magnitude relationship is broken. This is done while scanning the sequence from the front. That is, if there is a place where Ai> Ai + 1, the two numbers are exchanged once for i = 1, 2, ..., N − 1 in this order. It is a scan of. It is known that the sequence can be sorted in ascending order by repeating this scan N − 1 time.

The number of exchanges by bubble sort in sequence A is the number of times integers are exchanged when the above algorithm is applied to sequence A. (Algorithms and implementations known as bubble sort may have small differences in loop order, range, end conditions, etc. However, the number of integer exchanges when applied to the same sequence depends on those differences. It is known that it does not change.)

For example, the following program describes a function that sorts an array a of integers of length n by bubble sort in C language.


void bubble_sort (int * a, int n) {
int i, j;
for (i = 0; i <n -1; ++ i) {
for (j = 0; j <n -1; ++ j) {
if (a [j]> a [j + 1]) {
/ * The following 3 lines are equivalent to one integer exchange * /
int x = a [j];
a [j] = a [j + 1];
a [j + 1] = x;
}
}
}
}


Task

Given a sequence A of length N. Suppose you create a sequence A'by exchanging two integers anywhere in the sequence A only once. Create a program that finds the minimum number of exchanges by bubble sort in the sequence A'. (Note that the first two integers to be exchanged do not necessarily have to be next to each other.)

Limits

* 1 ≤ N ≤ 100 000 Length of sequence A
* 1 ≤ Ai ≤ 1 000 000 000 The size of the numbers in the sequence A



input

Read the following data from standard input.

* The integer N is written on the first line. N represents the length of the sequence A.
* The integer Ai is written on the i-th line (1 ≤ i ≤ N) of the following N lines. This represents the i-th integer in the sequence A.



output

Output to the standard output an integer representing the minimum number of exchanges by bubble sort in the sequence A'on one line.

Scoring criteria

* Of the scoring data, 10% of the points are satisfied with N ≤ 1 000, and Ai ≠ Aj is satisfied with any i, j (1 ≤ i <j ≤ N).
* Of the scoring data, 30% of the points are satisfied with N ≤ 5 000, and Ai ≠ Aj is satisfied for any i, j (1 ≤ i <j ≤ N).
* Of the scoring data, 80% of the points are given by satisfying Ai ≠ Aj for any i, j (1 ≤ i <j ≤ N).



Input / output example

Input example 1


Five
Ten
3
6
8
1


Output example 1


0


If we decide to exchange the first 10 and the last 1 of the sequence A, the sequence A'will be a sorted column and the number of bubble sort exchanges will be 0.




Input example 2


Five
3
1
7
9
Five


Output example 2


2


By exchanging the third 7 and the last 5 of the sequence A, the sequence A'becomes 3,1,5,9,7. The number of exchanges of A'by bubble sort is 2.




Input example 3


3
1
2
3


Output example 3


1


Even if the sequence A is sorted from the beginning, it must be exchanged when creating the sequence A'.

The question text and the data used for the automatic referee are the question text and the test data for scoring, which are created and published by the Japan Committee for Information Olympics.





Example

Input

5
10
3
6
8
1


Output

0
#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define INF 2e+9
using namespace std;

typedef pair<int,int> P;
typedef pair<int,P> PP;

int siz,segMax[300000],segAdd[300000],bit[300010];

void b_add(int i,int x){
	while(i <= siz){
		bit[i] += x;
		i += i & -i;
	}
}

int sum(int i){
	int s = 0;
	while(i){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

void add(int a,int b,int x,int k = 0,int l = 0,int r = siz){
	if(b <= l || r <= a) return;
	if(a <= l && r <= b){
		segAdd[k] += x;
		return;
	}
	add(a,b,x,k * 2 + 1,l,(l + r) / 2);
	add(a,b,x,k * 2 + 2,(l + r) / 2,r);
	segMax[k] = max(segMax[k * 2 + 1] + segAdd[k * 2 + 1],segMax[k * 2 + 2] + segAdd[k * 2 + 2]);
}

int get(int a,int b,int k = 0,int l = 0,int r = siz){
	if(b <= l || r <= a) return 0;
	if(a <= l && r <= b) return segMax[k] + segAdd[k];
	return max(get(a,b,k * 2 + 1,l,(l + r) / 2),get(a,b,k * 2 + 2,(l + r) / 2,r)) + segAdd[k];
}

signed main(){
	int n,a[100000],ma = -1,mi = INF,tent = 0,ans = 0;
	bool used[100000] = {};
	vector<int> ls,rs,la,ra,as;
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> a[i];
		as.push_back(a[i]);
	}
	sort(as.begin(),as.end());
	as.erase(unique(as.begin(),as.end()),as.end());
	for(int i = 0;i < n;i++) a[i] = lower_bound(as.begin(),as.end(),a[i]) - as.begin();
	for(int i = 0;i < n;i++){
		if(ma < a[i]){
			ma = a[i];
			ls.push_back(i);
			la.push_back(a[i]);
			used[i] = true;
		}
	}
	for(int i = n - 1;i >= 0;i--){
		if(mi > a[i]){
			mi = a[i];
			rs.push_back(i);
			ra.push_back(a[i]);
			used[i] = true;
		}
	}
	reverse(rs.begin(),rs.end());
	reverse(ra.begin(),ra.end());
	siz = 1;
	while(siz < n) siz *= 2;
	for(int i = 0;i < n;i++){
		tent += i - sum(a[i] + 1);
		b_add(a[i] + 1,1);
	}
	vector<PP> evt1[100001],evt2[100001];
	for(int i = 0;i < n;i++){
		if(!used[i]){
			int l1 = upper_bound(la.begin(),la.end(),a[i]) - la.begin();
			int l2 = upper_bound(ls.begin(),ls.end(),i) - ls.begin() - 1;
			int r1 = upper_bound(rs.begin(),rs.end(),i) - rs.begin();
			int r2 = lower_bound(ra.begin(),ra.end(),a[i]) - ra.begin() - 1;
			if(l1 <= l2 && r1 <= r2){
				evt1[r1].push_back(mp(2,mp(l1,l2)));
				evt2[r2].push_back(mp(-2,mp(l1,l2)));
			}
			if(l1 > 0 && la[l1 - 1] == a[i]){
				evt1[r1].push_back(mp(1,mp(l1 - 1,l1 - 1)));
				evt2[r2].push_back(mp(-1,mp(l1 - 1,l1 - 1)));
			}
			if(r2 < (int)rs.size() - 1 && ra[r2 + 1] == a[i]){
				evt1[r2 + 1].push_back(mp(1,mp(l1,l2)));
				evt2[r2 + 1].push_back(mp(-1,mp(l1,l2)));
			}
		}
	}
	for(int i = 0;i < n;i++){
		for(PP p : evt1[i]) add(p.second.first,p.second.second + 1,p.first);
		ans = max(ans,get(0,siz));
		for(PP p : evt2[i]) add(p.second.first,p.second.second + 1,p.first);
	}
	if(!tent) cout << ((ls.size() == n) ? 1 : 0) << endl;
	else cout << tent - ans - 1 << endl;
	return 0;
}