#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int n;
	cin >> n;

	vector<int> seq;
	for (int i = 0; i < n; ++i) {
		int m;
		cin >> m;
		seq.push_back(m);
	}

	int max = 0;

	for (int i = 0; i < n - 1; ++i) {
		int max1 = i;
		int max2 = -1;
		for (int j = i + 1; j < n; ++j) {
			if (seq[j] > max1) {
				max2 = max1;
				max1 = seq[j];
			}
			else if (seq[j] > max2) {
				max2 = seq[j];
			}
			int cal = max2 - j + i - 1;

			if (cal > max)
				max = cal;
		}
	}

	cout << max;
}