// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const ll MOD = 998244353, INF = 3e18 + 7;
const int maxn = 1e5 + 10;
int n;
int a[maxn];
ll pref[maxn], suff[maxn];
ll d[maxn];
void solve(){
cin >> n;
for(int i=1; i<=n; ++i) cin >> a[i];
for(int i=1; i<n; ++i) d[i] = a[i + 1] - a[i];
for(int i=1; i<n; ++i){
pref[i] = pref[i - 1];
if(d[i] <= 0){
pref[i] += 1 - d[i];
}
}
for(int i=n-1; i>=1; --i){
suff[i] = suff[i + 1];
if(d[i] >= 0){
suff[i] += d[i] + 1;
}
}
ll ans = min(pref[n - 1], suff[1]);
for(int i=1; i<n-1; ++i){
ans = min(ans, pref[i] + suff[i + 1]);
}
cout << ans << '\n';
}
signed main(){
// freopen("inp.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}