#include<bits/stdc++.h>
using namespace std;
int a[1000001];
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int maxans=-10000;
    for(int i=0;i<n-1;i++){
        int curr1=max(a[i],a[i+1]);
        int curr2=min(a[i],a[i+1]);
        maxans=max(maxans,curr2-2);
        for(int j=i+2;j<n;j++){
            if(a[j]>=curr1){
                curr2=curr1;
                curr1=a[j];
            }
            if(a[j]<curr1 && a[j]>curr2){
                curr2=a[j];
            }
            if(curr1>a[i] and curr2>a[i]){
                break;
            }
            maxans=max(maxans,curr2-(j-i+1));
        }
    }
    cout<<maxans;
    return 0;
}
