#include <bits/stdc++.h>
#define fst first
#define snd second
#define pb push_back
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define forn(i,a,b) for(int i = a; i<b; i++)
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
vector<pair<ll,ll>> solve(vector<ll> v){
vector<pair<ll,ll>> ret(SZ(v),{0,0});
ret[0].snd=v[0];
forn(i,1,SZ(v)){
ret[i].fst+=ret[i-1].fst;
ret[i].fst+=max((ll)0, (v[i-1]+1)-v[i]);
ret[i].snd=v[i]+ret[i].fst;
}
return ret;
}
int main(){
ll n; cin>>n;
vector<ll> v(n); forn(i,0,n) cin>>v[i];
vector<pair<ll,ll>> left = solve(v);
reverse(ALL(v));
vector<pair<ll,ll>> right = solve(v);
reverse(ALL(right));
ll res = min(left[n-1].fst,right[0].fst);
forn(i,0,n-1){
//cout<<i<<" "<<max(left[i].fst,right[i+1].fst)<<" "<<(left[i].snd==right[i+1].snd?1:0)<<'\n';
res=min( res , max(left[i].fst,right[i+1].fst) + (left[i].snd==right[i+1].snd?1:0));
}
cout<<res<<'\n';
return 0;
}