#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 60;
const int M = 1e3 + 10;
int t, n, m, k, l, r, op, x, y;
int f[N];
int me[M][N][2];
int dfs(int k, int l, bool ste) {
if (me[k][l][ste] != -1)return me[k][l][ste];
if (l == n + 1)return me[k][l][ste] = 0;
int ans = dfs(k, l + 1, ste) - 1;
if ((f[l] - k) * (ste ? 1 : -1) > 0) {
ans = max(ans, dfs(f[l], l + 1, !ste));
}
return me[k][l][ste] = ans;
}
void solve() {
memset(me, -1, sizeof(me));
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> f[i];
}
cout << n+(max(dfs(f[1], 2, true), dfs(f[1], 2, false)));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}