#include <iostream>
#include <vector>

using namespace std;

int maxIndex(int i, int j, vector<int> x) {

	int maxI = i;

	for (; i < j; i++) {
		if (x[maxI] < x[i]) {
			maxI = i;
		}
	}

	return maxI;
}


int main() {
	int N;
	cin >> N;
	int s;
	vector<int> a;

	for (int i = 0; i < N; i++) {
		cin >> s;

		a.insert(a.begin(), s);

	}

	int secondMax;
	int ans = 0;

	for (int size = 2; size <= N; size++) {
		for (int i = 0; i < N - size + 1; i++) {
			for (int j = i + 1; j < size + i; j++) {
				int maximum = maxIndex(i, j, a);

				int temp = a[maximum];
				a[maximum] = 0;

				secondMax = maxIndex(i, j, a);


				if (secondMax - j + i - 1 > ans) {
					ans = secondMax - j + i - 1;
				}
				a[maximum] = temp;
			}
		}
	}
	cout << ans;
	return 0;

}