// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
const int N=200005;
struct edge{
	int u,v;
}a[N];
int dg[N];
int fa[N],siz[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void merge(int x,int y)
{
	if(siz[x]<siz[y])
	{
		fa[x]=y;
		siz[y]+=siz[x];
	}
	else
	{
		fa[y]=x;
		siz[x]+=siz[y];
	}
}
int main()
{
	int n,m,x,y;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;++i) fa[i]=i,siz[i]=1;
	for(int i=1;i<=m;++i)
	{
		scanf("%d %d",&x,&y);
		a[i]=(edge){x,y};
		++dg[x],++dg[y];
	}
	int pos=0,maxx=0;
	for(int i=1;i<=n;++i)
	{
		if(maxx<dg[i])
		{
			pos=i,maxx=dg[i];
		}
	}
	int cnt=0;
	for(int i=1;i<=m;++i)
	{
		if(a[i].u==pos||a[i].v==pos)
		{
			merge(find(a[i].u),find(a[i].v));
			++cnt;
			printf("%d %d\n",a[i].u,a[i].v);
		}
	}
	for(int i=1;i<=m&&cnt<n-1;++i)
	{
		if(a[i].u!=pos&&a[i].v!=pos)
		{
			x=find(a[i].u),y=find(a[i].v);
			if(x!=y)
			{
				merge(x,y);
				++cnt;
				printf("%d %d\n",a[i].u,a[i].v);
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n, m, d;
vector <int> g[200001];
bool done[200001];
queue <int> q;
void bfs(int u){
    q.push(u);
    done[u]=1;
    while(!q.empty()){
        u=q.front();
        q.pop_front();
        for(int v: g[u]) if(!done[v]){
            done[v]=1;
            q.push(v);
            cout<<u<<' '<<v<<'\n';
        }
    }
}
int main(){
    cin>>n>>m>>d;
    for(int i=1, u, v; i<=m; i++){
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int best=1;
    for(int i=1; i<=n; i++) if(g[i].size()>g[best].size()) best=i;
    bfs(best);
}


#include <bits/stdc++.h>
using namespace std;
int n, m;
vector <int> g[200001];
bool done[200001];
queue <int> q;
void bfs(int u){
    q.push(u);
    done[u]=1;
    while(!q.empty()){
        u=q.front();
        q.pop();
        for(int v: g[u]) if(!done[v]){
            done[v]=1;
            q.push(v);
            cout<<u<<' '<<v<<'\n';
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1, u, v; i<=m; i++){
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int best=1;
    for(int i=1; i<=n; i++) if(g[i].size()>g[best].size()) best=i;
    bfs(best);
}


#include <bits/stdc++.h>
using namespace std;
int n, m, d;
vector <int> g[200001];
bool done[200001];
queue <int> q;
void bfs(int u){
    q.push(u);
    done[u]=1;
    while(!q.empty()){
        u=q.front();
        q.pop();
        for(int v: g[u]) if(!done[v]){
            done[v]=1;
            q.push(v);
            cout<<u<<' '<<v<<'\n';
        }
    }
}
int main(){
    cin>>n>>m>>d;
    for(int i=1, u, v; i<=m; i++){
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int best=1;
    for(int i=1; i<=n; i++) if(g[i].size()>g[best].size()) best=i;
    bfs(best);
}


#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;

typedef pair <int, int> pii;

vector<int> mp[MAXN];
vector<pii> ans;

int in[MAXN]={0};
int vis[MAXN];

void bfs(int st)
{
    vis[st]=1;
    queue<int> que;
    que.push(st);
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        for(int i=0;i<mp[u].size();i++)
        {
            int v=mp[u][i];
            if(vis[v]==0)
            {
                vis[v]=1;
                que.push(v);
                ans.push_back(pii(u,v));
            }
        }
    }
}

int main()
{
    int n,m,u,v;
    scanf("%d%d",&n,&m);
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&u,&v);
        mp[u].push_back(v);
        mp[v].push_back(u);
        in[u]++;
        in[v]++;
    }
    int maxs=0,st;
    for(int i=1;i<=n;i++)
    {
        if(in[i]>maxs)
        {
            maxs=in[i];
            st=i;
        }
    }
    bfs(st);
    for(int i=0;i<ans.size();i++)
        printf("%d %d\n",ans[i].first,ans[i].second);

    return 0;
}


#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;

typedef pair <int, int> pii;

vector<int> mp[MAXN];
vector<pii> ans;

int in[MAXN]={0};
int vis[MAXN];

void bfs(int st)
{
    vis[st]=1;
    queue<int> que;
    que.push(st);
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        for(int i=0;i<mp[u].size();i++)
        {
            int v=mp[u][i];
            if(vis[v]==0)
            {
                vis[v]=1;
                que.push(v);
                ans.push_back(pii(u,v));
            }
        }
    }
}

int main()
{
    int n,m,u,v;
    scanf("%d%d",&n,&m);
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        mp[u].push_back(v);
        mp[v].push_back(u);
        in[u]++;
        in[v]++;
    }
    int maxs=0,st;
    for(int i=1;i<=n;i++)
    {
        if(in[i]>maxs)
        {
            maxs=in[i];
            st=i;
        }
    }
    bfs(st);
    for(int i=0;i<ans.size();i++)
        printf("%d %d\n",ans[i].first,ans[i].second);

    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m,f[101];

struct edge
{
    int a,b,lg;
}tree[10240];

int cmp(edge x,edge y)
{
    return x.lg<y.lg;
}

int find(int x)
{
    if(x!=f[x])
        f[x]=find(f[x]);
    return f[x];
}

int mix(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
        f[fy]=fx;
    else return 0;
    return 1;
}

void kru()
{
    int sum=0,num=0;
    sort(tree,tree+m,cmp);
    for(int i=1;i<=m&&num!=n-1;i++)
    {
        if(mix(tree[i].a,tree[i].b))
        {
            num++;
            sum+=tree[i].lg;
            printf("%d %d\n",tree[i].a,tree[i].b);
        }
    }
}

int main()
{
    while(scanf("%d%d",&m,&n)&&m!=0)
    {
        memset(tree,0,sizeof(tree));
        for(int i=1;i<=n;i++)
            f[i]=i;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&tree[i].a,&tree[i].b);
            tree[i].lg=1;
        }
        kru();
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m,f[200005];

struct edge
{
    int a,b,lg;
}tree[200005];

int cmp(edge x,edge y)
{
    return x.lg<y.lg;
}

int find(int x)
{
    if(x!=f[x])
        f[x]=find(f[x]);
    return f[x];
}

int mix(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
        f[fy]=fx;
    else return 0;
    return 1;
}

void kru()
{
    int num=0;
    sort(tree,tree+m,cmp);
    for(int i=1;i<=m&&num!=n-1;i++)
    {
        if(mix(tree[i].a,tree[i].b))
        {
            num++;
            printf("%d %d\n",tree[i].a,tree[i].b);
        }
        if(num==n-1)
            break;
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    memset(tree,0,sizeof(tree));
    for(int i=0;i<=m;i++)
        f[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&tree[i].a,&tree[i].b);
        tree[i].lg=1;
    }
    kru();
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m,f[200005];

struct edge
{
    int a,b,lg;
}tree[200005];

int cmp(edge x,edge y)
{
    return x.lg<y.lg;
}

int find(int x)
{
    if(x!=f[x])
        f[x]=find(f[x]);
    return f[x];
}

int mix(int x,int y)
{
    int fx=find(x),fy=find(y);
    if(fx!=fy)
        f[fy]=fx;
    else return 0;
    return 1;
}

void kru()
{
    int sum=0,num=0;
    sort(tree,tree+m,cmp);
    for(int i=1;i<=m&&num!=n-1;i++)
    {
        if(mix(tree[i].a,tree[i].b))
        {
            num++;
            sum+=tree[i].lg;
            printf("%d %d\n",tree[i].a,tree[i].b);
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    memset(tree,0,sizeof(tree));
    for(int i=0;i<=m;i++)
        f[i]=i;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&tree[i].a,&tree[i].b);
        tree[i].lg=1;
    }
    kru();
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define Mp make_pair
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<int> Vi;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vi> VVi;
typedef pair<int, int> Pii;
typedef pair<DD, DD> Pdd;
typedef vector<Pii> Vpi;
typedef vector<Pdd> Vpd;
typedef queue<int> Qi;
typedef stack<int> Si;
typedef deque<int> Di;

int _toggle(int N, int pos) {return N = N ^ (1 << pos);}
int _set(int N, int pos) {return N = N | (1 << pos);}
int _reset(int N, int pos) {return N = N & ~(1 << pos);}
bool _check(int N, int pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}

int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

int pnt[2 * mx5];

void init(int n)
{
    for(int i = 1; i <= n; i++)
        pnt[i] = i;
}

int fnd(int v)
{
    if(v == pnt[v])
        return v;
    return fnd(pnt[v]);
}

void join(int a, int b)
{
    a = fnd(a);
    b = fnd(b);
    if(a != b)
        pnt[b] = a;
}

Vi g[2 * mx5];
int sz[2 * mx5];
bool vis[2 * mx5];

int main()
{
    fast;
    int n, m;
    cin >> n >> m;

    init(n);

    for(int i = 1; i <= m; i++){
        int u, v;
        cin >> u >> v;

        g[u].Pb(v);
        g[v].Pb(u);
    }

    Vpi v;
    for(int i = 1; i <= n; i++)
        v.Pb(Mp(g[i].size(), i));

    sort(All(v));
    reverse(All(v));

    for(int i = 0; i < n; i++){
        int x = v[i].Y;
        int y = fnd(x);

        if(vis[x])
            continue;
        vis[x] = 1;

        for(auto j: g[x]){
            if(y == fnd(j))
                continue;

            vis[j] = 1;
            join(y, j);
            cout << x << ' ' << j << '\n';
        }
    }
}


#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define Mp make_pair
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<int> Vi;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vi> VVi;
typedef pair<int, int> Pii;
typedef pair<DD, DD> Pdd;
typedef vector<Pii> Vpi;
typedef vector<Pdd> Vpd;
typedef queue<int> Qi;
typedef stack<int> Si;
typedef deque<int> Di;

int _toggle(int N, int pos) {return N = N ^ (1 << pos);}
int _set(int N, int pos) {return N = N | (1 << pos);}
int _reset(int N, int pos) {return N = N & ~(1 << pos);}
bool _check(int N, int pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}

int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

int pnt[2 * mx5];

void init(int n)
{
    for(int i = 1; i <= n; i++)
        pnt[i] = i;
}

int fnd(int v)
{
    if(v == pnt[v])
        return v;
    return fnd(pnt[v]);
}

void join(int a, int b)
{
    a = fnd(a);
    b = fnd(b);
    if(a != b)
        pnt[b] = a;
}

Vi g[2 * mx5];
int sz[2 * mx5];

int main()
{
    fast;
    int n, m;
    cin >> n >> m;

    init(n);

    for(int i = 1; i <= m; i++){
        int u, v;
        cin >> u >> v;

        g[u].Pb(v);
        g[v].Pb(u);
    }

    Vpi v;
    for(int i = 1; i <= n; i++)
        v.Pb(Mp(g[i].size(), i));

    sort(All(v));
    reverse(All(v));

    for(int i = 0; i < n; i++){
        int x = v[i].Y;
        int y = fnd(x);

        for(auto j: g[x]){
            if(y == fnd(j))
                continue;

            join(y, j);
            cout << x << ' ' << j << '\n';
        }
    }
}


#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define Mp make_pair
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<int> Vi;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vi> VVi;
typedef pair<int, int> Pii;
typedef pair<DD, DD> Pdd;
typedef vector<Pii> Vpi;
typedef vector<Pdd> Vpd;
typedef queue<int> Qi;
typedef stack<int> Si;
typedef deque<int> Di;

int _toggle(int N, int pos) {return N = N ^ (1 << pos);}
int _set(int N, int pos) {return N = N | (1 << pos);}
int _reset(int N, int pos) {return N = N & ~(1 << pos);}
bool _check(int N, int pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}

int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

Vi g[2 * mx5];
int sz[2 * mx5];
bool vis[2 * mx5];

void bfs(int u)
{
    Qi Q;
    Q.push(u);
    vis[u] = 1;

    while(!Q.empty()){
        int t = Q.front();
        Q.pop();

        for(auto i: g[t]){
            if(vis[i])
                continue;
            vis[i] = 1;
            Q.push(i);
            cout << t << ' ' << i << '\n';
        }
    }
}

int main()
{
    fast;
    int n, m;
    cin >> n >> m;

    for(int i = 1; i <= m; i++){
        int u, v;
        cin >> u >> v;

        g[u].Pb(v);
        g[v].Pb(u);
    }

    Vpi v;
    for(int i = 1; i <= n; i++)
        v.Pb(Mp(g[i].size(), i));

    sort(All(v));
    reverse(All(v));

    bfs(v[0].Y);
}


#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define Mp make_pair
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<int> Vi;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vi> VVi;
typedef pair<int, int> Pii;
typedef pair<DD, DD> Pdd;
typedef vector<Pii> Vpi;
typedef vector<Pdd> Vpd;
typedef queue<int> Qi;
typedef stack<int> Si;
typedef deque<int> Di;

int _toggle(int N, int pos) {return N = N ^ (1 << pos);}
int _set(int N, int pos) {return N = N | (1 << pos);}
int _reset(int N, int pos) {return N = N & ~(1 << pos);}
bool _check(int N, int pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}

int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

int pnt[2 * mx5];

void init(int n)
{
    for(int i = 1; i <= n; i++)
        pnt[i] = i;
}

int fnd(int v)
{
    if(v == pnt[v])
        return v;
    return fnd(pnt[v]);
}

void join(int a, int b)
{
    a = fnd(a);
    b = fnd(b);
    if(a != b)
        pnt[b] = a;
}

int main()
{
    fast;
    int n, m;
    cin >> n >> m;

    init(n);

    for(int i = 1; i <= m; i++){
        int u, v;
        cin >> u >> v;

        if(fnd(u) == fnd(v))
            continue;

        join(u, v);
        cout << u << ' ' << v << '\n';
    }
}


#include<bits/stdc++.h>

using namespace std;

#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define In insert
#define Mp make_pair
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
#define mx1 10
#define mx2 110
#define mx3 1010
#define mx4 10010
#define mx5 100010
#define mx6 1000010

typedef vector<int> Vi;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vi> VVi;
typedef pair<int, int> Pii;
typedef pair<DD, DD> Pdd;
typedef vector<Pii> Vpi;
typedef vector<Pdd> Vpd;
typedef queue<int> Qi;
typedef stack<int> Si;
typedef deque<int> Di;

int _toggle(int N, int pos) {return N = N ^ (1 << pos);}
int _set(int N, int pos) {return N = N | (1 << pos);}
int _reset(int N, int pos) {return N = N & ~(1 << pos);}
bool _check(int N, int pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}

int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};

///**************************DONE****************************///

int pnt[2 * mx5];

void init(int n)
{
    for(int i = 1; i <= n; i++)
        pnt[i] = i;
}

int fnd(int v)
{
    if(v == pnt[v])
        return v;
    return fnd(pnt[v]);
}

void join(int a, int b)
{
    a = fnd(a);
    b = fnd(b);
    if(a != b)
        pnt[b] = a;
}

Vi g[2 * mx5];
int sz[2 * mx5];
bool vis[2 * mx5];

int main()
{
    fast;
    int n, m;
    cin >> n >> m;

    init(n);

    for(int i = 1; i <= m; i++){
        int u, v;
        cin >> u >> v;

        g[u].Pb(v);
        g[v].Pb(u);
    }

    Vpi v;
    for(int i = 1; i <= n; i++)
        v.Pb(Mp(g[i].size(), i));

    sort(All(v));
    reverse(All(v));

    for(int i = 0; i < n; i++){
        int x = v[i].Y;
        int y = fnd(x);

        for(auto j: g[x]){
            if(vis[j])
                continue;
            vis[j] = 1;
            if(y == fnd(j))
                continue;

            join(y, j);
            cout << x << ' ' << j << '\n';
        }
    }
}


#include <bits/stdc++.h>
#define ll long long
#define maxn 200005
using namespace std;
int n,m;
vector<int> v[maxn];
int pos, cnt;

void bfs(){
  queue<int> q;
  bool vis[maxn];
  memset(vis,false,sizeof(vis));
  q.push(pos);
  vis[pos] = true;
  while(!q.empty()){
    int u = q.front();
    q.pop();
    for(int i=0;i<v[u].size();i++){
      if(vis[v[u][i]] == false){
        vis[v[u][i]] = true;
        printf("%d %d\n", u, v[u][i]);
        q.push(v[u][i]);
      }
    }
  }
}

int main()
{
  scanf("%d%d",&n,&m);
  cnt = 0;
  for(int i=0;i<m;i++){
    int x,y;
    scanf("%d%d",&x,&y);
    v[x].push_back(y);
    v[y].push_back(x);
    if(cnt < v[x].size()){
      cnt = v[x].size();
      pos = x;
    }
    if(cnt < v[y].size()){
      cnt = v[y].size();
      pos = y;
    }
  }
  bfs();
  return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<set>
#include<string.h>
#include<stdlib.h>
#define mst(x,y) memset(x,y,sizeof(x));
#define debug(x) cout<< #x <<" = "<< (x) <<endl;
#define rep(i,a,n) for (long long i=a;i<n;i++)
#define per(i,a,n) for (long long i=n-1;i>=a;i--)
#define fi first
#define pb push_back
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef unsigned long long ull;
const int mod = 1e6+3;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e6+5;
bool vis[maxn];
int n,m;
vector<int>G[maxn];
int pos;
int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	int cnt= 0 ;
	for(int i=0;i<n;i++){
		if(G[i].size()>cnt){
			pos = i;
			cnt = G[i].size();
		}
	}
	mst(vis,0);
	queue<int> que;
	que.push(pos);
	vis[pos] = true;
	while(!que.empty()){
		int p = que.front();que.pop();
		for(int i=0;i<G[p].size();i++){
			int u = G[p][i];
			if(!vis[u]){
				vis[u] = true;
				que.push(u);
				printf("%d %d\n",p,u);
			}
		}
	}
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<set>
#include<string.h>
#include<stdlib.h>
#define mst(x,y) memset(x,y,sizeof(x));
#define debug(x) cout<< #x <<" = "<< (x) <<endl;
#define rep(i,a,n) for (long long i=a;i<n;i++)
#define per(i,a,n) for (long long i=n-1;i>=a;i--)
#define fi first
#define pb push_back
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef unsigned long long ull;
const int mod = 1e6+3;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e6+5;
bool vis[maxn];
int n,m;
vector<int>G[maxn];
int pos;
int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	int cnt= 0 ;
	for(int i=1;i<=n;i++){
		if(G[i].size()>cnt){
			pos = i;
			cnt = G[i].size();
		}
	}
	mst(vis,0);
	queue<int> que;
	que.push(pos);
	vis[pos] = true;
	while(!que.empty()){
		int p = que.front();que.pop();
		for(int i=0;i<G[p].size();i++){
			int u = G[p][i];
			if(!vis[u]){
				vis[u] = true;
				que.push(u);
				printf("%d %d\n",p,u);
			}
		}
	}
    return 0;
}


    	        			/*
 * Author- Priyam Vora
 * BTech 2nd Year DAIICT
 */

        		    import java.awt.Checkbox;
import java.awt.Point;
import java.io.*;
        		    import java.math.*;
        		    import java.util.*;
import java.util.Map.Entry;

import javax.print.attribute.SetOfIntegerSyntax;
import javax.swing.plaf.FontUIResource;

        		    public class Main{
        		  	private static InputStream stream;
        		    	private static byte[] buf = new byte[1024];
        		    	private static int curChar;
        		    	private static int numChars;
        		    	private static SpaceCharFilter filter;
        		    	private static PrintWriter pw;
        		    	private static long count = 0,mod=1000000007;
        		    //	private static TreeSet<Integer> ts=new TreeSet[200000];
public final static int INF = (int) 1E9;

public static void main(String args[]) {
	InputReader(System.in);
	pw = new PrintWriter(System.out);
    new Thread(null ,new Runnable(){
       public void run(){
           try{
               solve();

               pw.close();
           } catch(Exception e){
               e.printStackTrace();
           }
       }
   },"1",1<<26).start();
   }
    static StringBuilder sb;
    public static void test(){
    	 sb=new StringBuilder();
    	int t=nextInt();
    	while(t-->0){

    		solve();
    		//pw.println();
    		//sb.append("\n");
    	}
    	pw.println(sb);
    }
    public static long pow(long n, long p,long mod) {
		if(p==0)
			return 1;
		if(p==1)
			return n%mod;
		if(p%2==0){
			long temp=pow(n, p/2,mod);
		return (temp*temp)%mod;
		}else{
			 	long temp=pow(n,p/2,mod);
			 	temp=(temp*temp)%mod;
			 	return(temp*n)%mod;

		}
	}
    public static long pow(long n, long p) {
		if(p==0)
			return 1;
		if(p==1)
			return n;
		if(p%2==0){
			long temp=pow(n, p/2);
		return (temp*temp);
		}else{
			 	long temp=pow(n,p/2);
			 	temp=(temp*temp);
			 	return(temp*n);

		}
	}
    public static void Merge(long a[],int p,int r){
        if(p<r){
            int q = (p+r)/2;
            Merge(a,p,q);
            Merge(a,q+1,r);
            Merge_Array(a,p,q,r);
        }
    }
    public static void Merge_Array(long a[],int p,int q,int r){
       long b[] = new long[q-p+1];
        long c[] = new long[r-q];
        for(int i=0;i<b.length;i++)
            b[i] = a[p+i];
        for(int i=0;i<c.length;i++)
            c[i] = a[q+i+1];
        int i = 0,j = 0;
        for(int k=p;k<=r;k++){
            if(i==b.length){
                a[k] = c[j];
                j++;
            }
            else if(j==c.length){
                a[k] = b[i];
                i++;
            }
            else if(b[i]<c[j]){
                a[k] = b[i];
                i++;
            }
            else{
                a[k] = c[j];
                j++;
            }
        }
    }

	public static long gcd(long x, long y) {
		if (x == 0)
			return y;
		else
			return gcd( y % x,x);
	}

	public static boolean isPrime(int n) {
		if (n <= 1)
			return false;
		if (n <= 3)
			return true;

		if (n % 2 == 0 || n % 3 == 0)
			return false;

		for (int i = 5; i * i <= n; i = i + 6)
			if (n % i == 0 || n % (i + 2) == 0)
				return false;

		return true;
	}

        static LinkedList<Integer> adj[];
        static boolean Visited[];
        static HashSet<Integer> exc;
        static long oddsum[]=new long[1000001];
        static long co=0,ans=0;

    	private static void buildgraph(int n){
    		adj=new LinkedList[n+1];
    		Visited=new boolean[n+1];
    		levl=new int[n+1];

    		for(int i=0;i<=n;i++){
    			adj[i]=new LinkedList<Integer>();

    		}

    	}

        static int[] levl;
        static int[] eat;
        static int n,m;
        static int price[];
        //ind frog crab
    public static void solve(){

    	int n=nextInt();
    	long a[]=nextLongArray(n);
    	if(n==1){
    		pw.println(0);
    		return;
    	}
    	long inc[]=new long[n];
    	long dec[]=new long[n];
    	long temp[]=new long[n];
    	for(int i=0;i<n;i++){
    		temp[i]=a[i];
    	}
    	long a_1[]=new long[n];
    	for(int i=1;i<n;i++){
    		if(a[i]<=a[i-1] ){
    			inc[i]+=(a[i-1]+1-a[i]);
    			a[i]=a[i-1]+1;
    		}
    		inc[i]+=inc[i-1];

    	}

    	for(int i=0;i<n;i++){
    		{
    			a_1[i]=a[i];
    			a[i]=temp[i];
    		}
    	}
    	for(int i=n-2;i>=0;i--){
    		if(a[i]<=a[i+1]){
    			dec[i]+=(a[i+1]+1-a[i]);
    			a[i]=a[i+1]+1;
    		}
    		dec[i]+=dec[i+1];

    	}

    	long f_ans=Long.MAX_VALUE;
    	for(int i=0;i<n;i++){
    		long aft=0;
    		long prev=0;
    		if(i<n-1){
    			aft=dec[i+1];
    		}

    			prev=inc[i];
    		long ans=aft+prev;
    		if(i==0){
    			ans=dec[i];
    		}
    		else if(i==n-1){
    			ans=inc[i];
    		}else{
    			ans+= Math.max(Math.max(a_1[i], a[i+1]+1)-(a_1[i]),0);
    		}

    		f_ans=Math.min(f_ans, ans);
    	}
    	pw.println(f_ans);
    //	pArray(inc);
    	//pArray(dec);

    }
    public static boolean isVowel(char c){
    	if(c=='a' || c=='e'||c=='i' || c=='o' || c=='u')
    		return true;
    	return false;
    }
static int no_vert=0;

  private static void dfs(int start,int cake){
	  Visited[start]=true;
	  no_vert++;
	  eat[start]=cake;
	  for(int i:adj[start]){
		  if(!Visited[i])
			  {
			//  pw.println();
			  dfs(i,cake);
			  }
	  }

  }

    public static String reverseString(String s) {
		StringBuilder sb = new StringBuilder(s);
		sb.reverse();
		return (sb.toString());
	}

  /*
   private static void BFS(int sou){
    	Queue<Integer> q=new LinkedList<Integer>();
    	q.add(sou);
    	Visited[sou]=true;
    	levl[sou]=0;
    	while(!q.isEmpty()){
    		int top=q.poll();

    		for(int i:adj[top]){
    			//pw.println(i+" "+top);
			if(!Visited[i])
			{

				q.add(i);
				levl[i]=levl[top]+1;
			}

			Visited[i]=true;

			}
		}
	}*/

    		      static int indeg[];
    		   /* private static void kahn(int n){

    		    	PriorityQueue<Integer> q=new PriorityQueue<Integer>();
    				for(int i=1;i<=n;i++){
    					if(indeg[i]==0){
    						q.add(i);
    					}
    				}
    				while(!q.isEmpty()){
    					int top=q.poll();
    					st.push(top);
    					for(Node i:adj[top]){
    						indeg[i.to]--;
    						if(indeg[i.to]==0){
    							q.add(i.to);
    						}
    					}
    				}
    		    }

    		        static int state=1;
    		        static long no_exc=0,no_vert=0;
    		      static Stack<Integer> st;
    		      static HashSet<Integer> inset;
    		     /*   private static void topo(int curr){

    		        	Visited[curr]=true;
    		        	inset.add(curr);
    		        	for(int x:adj[curr]){
    		        		if(adj[x].contains(curr) || inset.contains(x)){
    		        			state=0;
    		        			return;
    		        		}
    		        		if(state==0)
    		        			return;

    		        	}
    		        	st.push(curr);

    		        	inset.remove(curr);
    		        }*/
    		        static HashSet<Integer> hs;

    		     	static boolean prime[];
					static int spf[];
					public static void sieve(int n){
						prime=new boolean[n+1];
						spf=new int[n+1];

						Arrays.fill(spf, 1);
						Arrays.fill(prime, true);
					prime[1]=false;
						spf[2]=2;

					for(int i=4;i<=n;i+=2){
						spf[i]=2;
					}
					for(int i=3;i<=n;i+=2){
						if(prime[i]){
							spf[i]=i;
							for(int j=2*i;j<=n;j+=i){

								prime[j]=false;
							if(spf[j]==1){
								spf[j]=i;
							}
							}
						}
					}

					}

    		    	// To Get Input
// Some Buffer Methods

    		     	public static void sort(long a[]){
    		     		Merge(a, 0, a.length-1);
    		     	}
    		    	public static void InputReader(InputStream stream1) {
    		    		stream = stream1;
    		    	}

    		    	private static boolean isWhitespace(int c) {
    		    		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    		    	}

    		    	private static boolean isEndOfLine(int c) {
    		    		return c == '\n' || c == '\r' || c == -1;
    		    	}

    		    	private static int read() {
    		    		if (numChars == -1)
    		    			throw new InputMismatchException();
    		    		if (curChar >= numChars) {
    		    			curChar = 0;
    		    			try {
    		    				numChars = stream.read(buf);
    		    			} catch (IOException e) {
    		    				throw new InputMismatchException();
    		    			}
    		    			if (numChars <= 0)
    		    				return -1;
    		    		}
    		    		return buf[curChar++];
    		    	}

    		    	private static int nextInt() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		int sgn = 1;
    		    		if (c == '-') {
		sgn = -1;
		c = read();
	}
	int res = 0;
	do {
		if (c < '0' || c > '9')
			throw new InputMismatchException();
		res *= 10;
		res += c - '0';
    		    			c = read();
    		    		} while (!isSpaceChar(c));
    		    		return res * sgn;
    		    	}

    		    	private static long nextLong() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		int sgn = 1;
    		    		if (c == '-') {
		sgn = -1;
		c = read();
	}
	long res = 0;
	do {
		if (c < '0' || c > '9')
			throw new InputMismatchException();
		res *= 10;
		res += c - '0';
    		    			c = read();
    		    		} while (!isSpaceChar(c));
    		    		return res * sgn;
    		    	}

    		    	private static String nextToken() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		StringBuilder res = new StringBuilder();
    		    		do {
    		    			res.appendCodePoint(c);
    		    			c = read();
    		    		} while (!isSpaceChar(c));
    		    		return res.toString();
    		    	}

    		    	private static String nextLine() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		StringBuilder res = new StringBuilder();
    		    		do {
    		    			res.appendCodePoint(c);
    		    			c = read();
    		    		} while (!isEndOfLine(c));
    		    		return res.toString();
    		    	}

    		    	private static int[] nextIntArray(int n) {
    		    		int[] arr = new int[n];
    		    		for (int i = 0; i < n; i++) {
    		    			arr[i] = nextInt();
    		    		}
    		    		return arr;
    		    	}

    		    	private static long[][] next2dArray(int n, int m) {
    		    		long[][] arr = new long[n][m];
    		    		for (int i = 0; i < n; i++) {
    		    			for (int j = 0; j < m; j++) {
    		    				arr[i][j] = nextLong();
    		    			}
    		    		}
    		    		return arr;
    		    	}
    		    	private static char[][] nextCharArray(int n,int m){
    		    		char [][]c=new char[n][m];
    		    		for(int i=0;i<n;i++){
    		    			String s=nextLine();
    		    			for(int j=0;j<s.length();j++){
    		    				c[i][j]=s.charAt(j);
    		    			}
    		    		}
    		    		return c;
    		    	}

    		    	private static long[] nextLongArray(int n) {
    		    		long[] arr = new long[n];
    		    		for (int i = 0; i < n; i++) {
    		    			arr[i] = nextLong();
    		    		}
    		    		return arr;
    		    	}

    		    	private static void pArray(int[] arr) {
    		    		for (int i = 0; i < arr.length; i++) {
    		    			pw.print(arr[i] + " ");
    		    		}
    		    		pw.println();
    		    		return;
    		    	}

    		    	private static void pArray(long[] arr) {
    		    		for (int i = 0; i < arr.length; i++) {
    		    			pw.print(arr[i] + " ");
    		    		}
    		    		pw.println();
    		    		return;
    		    	}

    		    	private static void pArray(boolean[] arr) {
    		    		for (int i = 0; i < arr.length; i++) {
    		    			pw.print(arr[i] + " ");
    		    		}
    		    		pw.println();
    		    		return;
    		    	}

    		    	private static boolean isSpaceChar(int c) {
    		    		if (filter != null)
    		    			return filter.isSpaceChar(c);
    		    		return isWhitespace(c);
    		    	}

    		    	private interface SpaceCharFilter {
    		    		public boolean isSpaceChar(int ch);
    		    	}

}

    		        class Node{
    		    	int to;
    		    	long dist;
    		    	Node(int to,long dist){
    		    		this.to=to;
    		    		this.dist=dist;
    		    	}

    		    }

class Dsu{
private int rank[], parent[] ,n;
long str[],si[],larg[];
long max=0;
int cnt=0;
private static int[] parent1;
Dsu(int size){
	this.n=size+1;
	rank=new int[n];
	cnt=n-1;
	//parent=new int[n];
    		    		    		parent=new int[n];
    		    		    		str=new long[n];
    		    		    		si=new long[n];

    		    		    		larg=new long[n];
    		    		    		makeSet();

    		    		    	}

    		    		    	void makeSet(){
    		    		    		for(int i=0;i<n;i++){
    		    		    			parent[i]=i;
    		    		    			si[i]=1;
    		    		    		}
    		    		    	}

    		    		    	int find(int x){
    		    		    		if(parent[x]!=x){

    		    		    			parent[x]=find(parent[x]);
    		    		    		}
    		    		    		return parent[x];
    		    		    	}

    		    		    	void join(int ind,long val){
    		    		    		if(str[ind]==0){
    		    		    			str[ind]=val;
    		    		    			larg[ind]=val;
    		    		    			max=Math.max(max, str[ind]);
    		    		    		}
    		    		    		int prev_ind=(ind-1);
    		    		    		int next_ind=(ind+1);
    		    		    		if(prev_ind>0 && larg[prev_ind]!=0){
    		    		    			union(prev_ind,ind);
    		    		    		}
    		    		    		if(next_ind<n && larg[next_ind]!=0){
    		    		    			union(next_ind,ind);
    		    		    		}

    		    		    	}

    		    		    	boolean union(int x,int y){
    		    		    		int xRoot=find(x);
    		    		    		int yRoot=find(y);

    		    		    		if(xRoot==yRoot)
    		    		    			return false;
    		    		    		if(rank[xRoot]<rank[yRoot]){
    		    		    		parent[xRoot]=yRoot;

    		    		    		larg[yRoot]=Math.max(larg[xRoot], larg[yRoot]);

    		    		    		si[yRoot]+=si[xRoot];
    		    		    		str[yRoot]=larg[yRoot]*si[yRoot];
    		    		    			max=Math.max(str[yRoot], max);
    		    		    		}else if(rank[yRoot]<rank[xRoot]){
    		    		    			parent[yRoot]=xRoot;

    		    		    			larg[xRoot]=Math.max(larg[xRoot], larg[yRoot]);

        		    		    		si[xRoot]+=si[yRoot];
        		    		    		str[xRoot]=larg[xRoot]*si[xRoot];
    			    		    		max=Math.max(str[xRoot], max);
    		    		    		}else{
    		    		    			parent[yRoot]=xRoot;
    		    		    			rank[xRoot]++;

    		    		    			larg[xRoot]=Math.max(larg[xRoot], larg[yRoot]);

        		    		    		si[xRoot]+=si[yRoot];
        		    		    		str[xRoot]=larg[xRoot]*si[xRoot];
    			    		    		max=Math.max(str[xRoot], max);

    		    		    		}
    		    		    		cnt--;
    		    		    		return true;
    		    		    	}

    		    		    }


	import java.io.*;

import java.math.*;
import java.nio.file.attribute.AclEntry.Builder;
import java.security.KeyStore.Entry;
import java.util.*;

	public  class Khatri {
		private InputStream is;
		private PrintWriter out;
		int time = 0, DP[], start[], end[], dist[],  black[];
		static int MOD = (int)(1e9+7);
		int arr[];
		int weight[][];
		int x[];
		int y[];
		int parent[];
		int MAX = 800000, N, K, L;
		long red[];
		ArrayList<Integer>[] amp;
		HashSet<Integer> hs = new HashSet<>();
		long Dp[][][][] = new long[110][110][12][12];
		public static void main(String[] args) throws Exception
		{
			new Thread(null, new Runnable()
			{
			public void run()
			{
				try {

			//new CODEFORCES().soln();
			} catch (Exception e)
				{
					System.out.println(e);
				}
			}
		}, "1", 1 << 26).start();
			new Khatri().soln();
		}
		char ch[][];
		//int ans = 0, cnt = 0, Q;
		int n1, n2, k1, k2, M;
		static ArrayList<Integer>[] g;
        static ArrayList<Integer> ar[];
		static long ok[];
		//static int a[];
		static int phi[]=new int[500005];
		void solve()
        {

			int n=ni();
		    long a[]=new long[n];
		    for(int i=0;i<n;i++)
		    	a[i]=nl();
		    long c[]=Arrays.copyOf(a, n);
		    int acs=0;int dcs=0;
		   long b[]=new long[n];
		    b[0]=(long)a[0];
		    for(int i=1;i<n;i++)
		    {
		    b[i]=b[i-1]+1;
		    }
		    long toy=0;
		    for(int i=0;i<n;i++)
		    {
		    	if(b[i]>=a[i])
		    	{
		    		toy+=(long)(b[i]-a[i]);
		    	}
		    	else
		    	{
		    		toy=0;
		    		break;
		    	}
		    }
		    long ans=Long.MAX_VALUE;

		    if(toy!=0)
		    	ans=toy;
		     toy=0;
		      b=new long[n];
			    b[n-1]=a[n-1];
			    for(int i=n-2;i>=0;i--)
			    {
			    b[i]=b[i+1]+1;
			    }
		    for(int i=0;i<n;i++)
		    {
		    	if(b[i]>=a[i])
		    	{
		    		toy+=(long)(b[i]-a[i]);
		    	}
		    	else
		    	{
		    		toy=0;
		    		break;
		    	}
		    }
		    if(toy!=0)
		    {
		    	ans=Math.min(toy, ans);
		    }

		    for(int i=0;i<n-1;i++)
		    	if(a[i]<a[i+1])
		    		acs++;
		    if(acs==n-1)
		    {
		    	out.print(0);
		    	return;
		    }
		    for(int i=0;i<n-1;i++)
		    	if(a[i]>a[i+1])
		    		dcs++;
		    if(dcs==n-1)
		    {
		    	out.print(0);
		    	return;
		    }
		    int right[]=new int[n];
		    right[n-1]=1;
		    for(int i=n-2;i>=0;i--)
		    {
		    if(a[i]>a[i+1])
		    {
		    	right[i]=1;
		    }
		    else
		    	break;
		    }
		    long sum[]=new long[n];
		    sum[0]=(long)0;
		    for(int i=0;i<n-1;i++)
		    {
		    	if(a[i]<a[i+1])
		    	{
		    		continue;
		    	}
		    	else
		    	{
		    		long tmp=a[i]-a[i+1]+1;
		    		a[i+1]=a[i]+1;
		    		sum[i+1]=tmp;
		    	}
		    }
		    for(int i=1;i<n;i++)
		    	sum[i]+=sum[i-1];

		    long sum2[]=new long[n];
		    sum2[n-1]=(long)0;
		    for(int i=n-2;i>=0;i--)
		    {
		    	if(c[i]>c[i+1])
		    	{
		    		continue;
		    	}
		    	else
		    	{
		    		long tmp=c[i+1]-c[i]+1;
		    		c[i]=c[i+1]+1;
		    		sum2[i]=tmp;
		    	}
		    }
		   for(int i=n-2;i>=0;i--)
		   {
			   sum2[i]+=sum2[i+1];
		   }
		   for(int i=0;i<n-1;i++)
		   {
			   if(a[i]>c[i+1])
			   {
				   ans=Math.min(sum[i]+sum2[i+1],ans);
			   }
		   }

		    out.print(ans);

        }

            boolean isPrime(int x)
            {
            if(x==0||x==1)
            	return false;
			for(int i = 2;i*1L*i<=x;i++) if(x%i==0) return false;
			return true;
		}
	    int  p ;
		void buildGraph(int n)
		{
			for(int i = 0; i<n ;i++){
				int x1 = ni() -1, y1 = ni()-1;
				amp[x1].add(y1);
				amp[y1].add(x1);
				x[i] = x1; y[i] = y1;
			}
		}
		long get(int x){
			long val = 0;
			for(int i = 0; i< N; i++){
				val += Math.abs(x-arr[i]);
				x+=L;
			}
			return val;
		}

		long modInverse(long a, long mOD2){
	        return  power(a, mOD2-2, mOD2);
		}
		long power(long x, long y, long m)
		{
			if (y == 0)
	    return 1;
		long p = power(x, y/2, m) % m;
		p = (p * p) % m;

		return (y%2 == 0)? p : (x * p) % m;
		}

		public static long gcd(long a, long b){
			if(b==0) return a;
			return gcd(b,a%b);
		}

		public static int[] shuffle(int[] a, Random gen){
			for(int i = 0, n = a.length;i < n;i++)
			{
				int ind = gen.nextInt(n-i)+i;
				int d = a[i];
				a[i] = a[ind];
				a[ind] = d;
			}
		return a;
		}

		class Pair1 implements Comparable<Pair1>{
			String s;
			int val;
			Pair1(String s,int time){
			this.s=s;
			this.val=time;
			}
			public int hashCode() {
				return Objects.hash();
			}

			@Override
			public int compareTo(Pair1 arg0) {
				return (int)(arg0.val-val);
			}

		}
		long power(long x, long y, int mod){
			long ans = 1;
			while(y>0){
				if(y%2==0){
					x = (x*x)%mod;
					y/=2;
				}
				else{
					ans = (x*ans)%mod;
					y--;
				}
			}
			return ans;
		}
		void soln() {
			is = System.in;
			out = new PrintWriter(System.out);
			long s = System.currentTimeMillis();
			solve();
			//out.close();
			out.flush();
			//tr(System.currentTimeMillis() - s + "ms");
		}

		// To Get Input
		// Some Buffer Methods
		private byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		private int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		private boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		private int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

		private double nd() {
			return Double.parseDouble(ns());
		}

		private char nc() {
			return (char) skip();
		}
		private String ns() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
										// ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		private char[] ns(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		private int[][] nm(int n, int m) {
			int[][] map = new int[n][m];
			for (int i = 0; i < n; i++)
			{
				for(int j=0;j<m;j++)
					map[i][j]=ni();
			}
			return map;
		}

		private int[] na(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = ni();
			return a;
		}

		private int ni() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private long nl() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

		private void tr(Object... o) {
			if (!oj)
				System.out.println(Arrays.deepToString(o));
		}
	}


    	        			/*
 * Author- Priyam Vora
 * BTech 2nd Year DAIICT
 */

        		    import java.awt.Checkbox;
import java.awt.Point;
import java.io.*;
        		    import java.math.*;
        		    import java.util.*;
import java.util.Map.Entry;

import javax.print.attribute.SetOfIntegerSyntax;
import javax.swing.plaf.FontUIResource;

        		    public class Main{
        		  	private static InputStream stream;
        		    	private static byte[] buf = new byte[1024];
        		    	private static int curChar;
        		    	private static int numChars;
        		    	private static SpaceCharFilter filter;
        		    	private static PrintWriter pw;
        		    	private static long count = 0,mod=1000000007;
        		    //	private static TreeSet<Integer> ts=new TreeSet[200000];
public final static int INF = (int) 1E9;

public static void main(String args[]) {
	InputReader(System.in);
	pw = new PrintWriter(System.out);
    new Thread(null ,new Runnable(){
       public void run(){
           try{
               solve();

               pw.close();
           } catch(Exception e){
               e.printStackTrace();
           }
       }
   },"1",1<<26).start();
   }
    static StringBuilder sb;
    public static void test(){
    	 sb=new StringBuilder();
    	int t=nextInt();
    	while(t-->0){

    		solve();
    		//pw.println();
    		//sb.append("\n");
    	}
    	pw.println(sb);
    }
    public static long pow(long n, long p,long mod) {
		if(p==0)
			return 1;
		if(p==1)
			return n%mod;
		if(p%2==0){
			long temp=pow(n, p/2,mod);
		return (temp*temp)%mod;
		}else{
			 	long temp=pow(n,p/2,mod);
			 	temp=(temp*temp)%mod;
			 	return(temp*n)%mod;

		}
	}
    public static long pow(long n, long p) {
		if(p==0)
			return 1;
		if(p==1)
			return n;
		if(p%2==0){
			long temp=pow(n, p/2);
		return (temp*temp);
		}else{
			 	long temp=pow(n,p/2);
			 	temp=(temp*temp);
			 	return(temp*n);

		}
	}
    public static void Merge(long a[],int p,int r){
        if(p<r){
            int q = (p+r)/2;
            Merge(a,p,q);
            Merge(a,q+1,r);
            Merge_Array(a,p,q,r);
        }
    }
    public static void Merge_Array(long a[],int p,int q,int r){
       long b[] = new long[q-p+1];
        long c[] = new long[r-q];
        for(int i=0;i<b.length;i++)
            b[i] = a[p+i];
        for(int i=0;i<c.length;i++)
            c[i] = a[q+i+1];
        int i = 0,j = 0;
        for(int k=p;k<=r;k++){
            if(i==b.length){
                a[k] = c[j];
                j++;
            }
            else if(j==c.length){
                a[k] = b[i];
                i++;
            }
            else if(b[i]<c[j]){
                a[k] = b[i];
                i++;
            }
            else{
                a[k] = c[j];
                j++;
            }
        }
    }

	public static long gcd(long x, long y) {
		if (x == 0)
			return y;
		else
			return gcd( y % x,x);
	}

	public static boolean isPrime(int n) {
		if (n <= 1)
			return false;
		if (n <= 3)
			return true;

		if (n % 2 == 0 || n % 3 == 0)
			return false;

		for (int i = 5; i * i <= n; i = i + 6)
			if (n % i == 0 || n % (i + 2) == 0)
				return false;

		return true;
	}

        static LinkedList<Integer> adj[];
        static boolean Visited[];
        static HashSet<Integer> exc;
        static long oddsum[]=new long[1000001];
        static long co=0,ans=0;

    	private static void buildgraph(int n){
    		adj=new LinkedList[n+1];
    		Visited=new boolean[n+1];
    		levl=new int[n+1];

    		for(int i=0;i<=n;i++){
    			adj[i]=new LinkedList<Integer>();

    		}

    	}

        static int[] levl;
        static int[] eat;
        static int n,m;
        static int price[];
        //ind frog crab
    public static void solve(){

    	int n=nextInt();
    	long a[]=nextLongArray(n);
    	if(n==1){
    		pw.println(0);
    		return;
    	}
    	long inc[]=new long[n];
    	long dec[]=new long[n];
    	long temp[]=new long[n];
    	for(int i=0;i<n;i++){
    		temp[i]=a[i];
    	}
    	long a_1[]=new long[n];
    	for(int i=1;i<n;i++){
    		if(a[i]<=a[i-1] ){
    			inc[i]+=(a[i-1]+1-a[i]);
    			a[i]=a[i-1]+1;
    		}
    		inc[i]+=inc[i-1];

    	}

    	for(int i=0;i<n;i++){
    		{
    			a_1[i]=a[i];
    			a[i]=temp[i];
    		}
    	}
    	for(int i=n-2;i>=0;i--){
    		if(a[i]<=a[i+1]){
    			dec[i]+=(a[i+1]+1-a[i]);
    			a[i]=a[i+1]+1;
    		}
    		dec[i]+=dec[i+1];

    	}

    	long f_ans=Long.MAX_VALUE;
    	for(int i=0;i<n;i++){
    		long aft=0;
    		long prev=0;
    		if(i<n-1){
    			aft=dec[i+1];
    		}

    			prev=inc[i];
    		long ans=aft+prev;
    		if(i==0){
    			ans=dec[i];
    		}
    		else if(i==n-1){
    			ans=inc[i];
    		}else{
    			ans+= Math.max(Math.max(a_1[i], a[i+1]+1)-(a_1[i]),0);
    		}

    		f_ans=Math.min(f_ans, ans);
    	}
    	pw.println(f_ans);
    	pArray(inc);
    	pArray(dec);

    }
    public static boolean isVowel(char c){
    	if(c=='a' || c=='e'||c=='i' || c=='o' || c=='u')
    		return true;
    	return false;
    }
static int no_vert=0;

  private static void dfs(int start,int cake){
	  Visited[start]=true;
	  no_vert++;
	  eat[start]=cake;
	  for(int i:adj[start]){
		  if(!Visited[i])
			  {
			//  pw.println();
			  dfs(i,cake);
			  }
	  }

  }

    public static String reverseString(String s) {
		StringBuilder sb = new StringBuilder(s);
		sb.reverse();
		return (sb.toString());
	}

  /*
   private static void BFS(int sou){
    	Queue<Integer> q=new LinkedList<Integer>();
    	q.add(sou);
    	Visited[sou]=true;
    	levl[sou]=0;
    	while(!q.isEmpty()){
    		int top=q.poll();

    		for(int i:adj[top]){
    			//pw.println(i+" "+top);
			if(!Visited[i])
			{

				q.add(i);
				levl[i]=levl[top]+1;
			}

			Visited[i]=true;

			}
		}
	}*/

    		      static int indeg[];
    		   /* private static void kahn(int n){

    		    	PriorityQueue<Integer> q=new PriorityQueue<Integer>();
    				for(int i=1;i<=n;i++){
    					if(indeg[i]==0){
    						q.add(i);
    					}
    				}
    				while(!q.isEmpty()){
    					int top=q.poll();
    					st.push(top);
    					for(Node i:adj[top]){
    						indeg[i.to]--;
    						if(indeg[i.to]==0){
    							q.add(i.to);
    						}
    					}
    				}
    		    }

    		        static int state=1;
    		        static long no_exc=0,no_vert=0;
    		      static Stack<Integer> st;
    		      static HashSet<Integer> inset;
    		     /*   private static void topo(int curr){

    		        	Visited[curr]=true;
    		        	inset.add(curr);
    		        	for(int x:adj[curr]){
    		        		if(adj[x].contains(curr) || inset.contains(x)){
    		        			state=0;
    		        			return;
    		        		}
    		        		if(state==0)
    		        			return;

    		        	}
    		        	st.push(curr);

    		        	inset.remove(curr);
    		        }*/
    		        static HashSet<Integer> hs;

    		     	static boolean prime[];
					static int spf[];
					public static void sieve(int n){
						prime=new boolean[n+1];
						spf=new int[n+1];

						Arrays.fill(spf, 1);
						Arrays.fill(prime, true);
					prime[1]=false;
						spf[2]=2;

					for(int i=4;i<=n;i+=2){
						spf[i]=2;
					}
					for(int i=3;i<=n;i+=2){
						if(prime[i]){
							spf[i]=i;
							for(int j=2*i;j<=n;j+=i){

								prime[j]=false;
							if(spf[j]==1){
								spf[j]=i;
							}
							}
						}
					}

					}

    		    	// To Get Input
// Some Buffer Methods

    		     	public static void sort(long a[]){
    		     		Merge(a, 0, a.length-1);
    		     	}
    		    	public static void InputReader(InputStream stream1) {
    		    		stream = stream1;
    		    	}

    		    	private static boolean isWhitespace(int c) {
    		    		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    		    	}

    		    	private static boolean isEndOfLine(int c) {
    		    		return c == '\n' || c == '\r' || c == -1;
    		    	}

    		    	private static int read() {
    		    		if (numChars == -1)
    		    			throw new InputMismatchException();
    		    		if (curChar >= numChars) {
    		    			curChar = 0;
    		    			try {
    		    				numChars = stream.read(buf);
    		    			} catch (IOException e) {
    		    				throw new InputMismatchException();
    		    			}
    		    			if (numChars <= 0)
    		    				return -1;
    		    		}
    		    		return buf[curChar++];
    		    	}

    		    	private static int nextInt() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		int sgn = 1;
    		    		if (c == '-') {
		sgn = -1;
		c = read();
	}
	int res = 0;
	do {
		if (c < '0' || c > '9')
			throw new InputMismatchException();
		res *= 10;
		res += c - '0';
    		    			c = read();
    		    		} while (!isSpaceChar(c));
    		    		return res * sgn;
    		    	}

    		    	private static long nextLong() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		int sgn = 1;
    		    		if (c == '-') {
		sgn = -1;
		c = read();
	}
	long res = 0;
	do {
		if (c < '0' || c > '9')
			throw new InputMismatchException();
		res *= 10;
		res += c - '0';
    		    			c = read();
    		    		} while (!isSpaceChar(c));
    		    		return res * sgn;
    		    	}

    		    	private static String nextToken() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		StringBuilder res = new StringBuilder();
    		    		do {
    		    			res.appendCodePoint(c);
    		    			c = read();
    		    		} while (!isSpaceChar(c));
    		    		return res.toString();
    		    	}

    		    	private static String nextLine() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		StringBuilder res = new StringBuilder();
    		    		do {
    		    			res.appendCodePoint(c);
    		    			c = read();
    		    		} while (!isEndOfLine(c));
    		    		return res.toString();
    		    	}

    		    	private static int[] nextIntArray(int n) {
    		    		int[] arr = new int[n];
    		    		for (int i = 0; i < n; i++) {
    		    			arr[i] = nextInt();
    		    		}
    		    		return arr;
    		    	}

    		    	private static long[][] next2dArray(int n, int m) {
    		    		long[][] arr = new long[n][m];
    		    		for (int i = 0; i < n; i++) {
    		    			for (int j = 0; j < m; j++) {
    		    				arr[i][j] = nextLong();
    		    			}
    		    		}
    		    		return arr;
    		    	}
    		    	private static char[][] nextCharArray(int n,int m){
    		    		char [][]c=new char[n][m];
    		    		for(int i=0;i<n;i++){
    		    			String s=nextLine();
    		    			for(int j=0;j<s.length();j++){
    		    				c[i][j]=s.charAt(j);
    		    			}
    		    		}
    		    		return c;
    		    	}

    		    	private static long[] nextLongArray(int n) {
    		    		long[] arr = new long[n];
    		    		for (int i = 0; i < n; i++) {
    		    			arr[i] = nextLong();
    		    		}
    		    		return arr;
    		    	}

    		    	private static void pArray(int[] arr) {
    		    		for (int i = 0; i < arr.length; i++) {
    		    			pw.print(arr[i] + " ");
    		    		}
    		    		pw.println();
    		    		return;
    		    	}

    		    	private static void pArray(long[] arr) {
    		    		for (int i = 0; i < arr.length; i++) {
    		    			pw.print(arr[i] + " ");
    		    		}
    		    		pw.println();
    		    		return;
    		    	}

    		    	private static void pArray(boolean[] arr) {
    		    		for (int i = 0; i < arr.length; i++) {
    		    			pw.print(arr[i] + " ");
    		    		}
    		    		pw.println();
    		    		return;
    		    	}

    		    	private static boolean isSpaceChar(int c) {
    		    		if (filter != null)
    		    			return filter.isSpaceChar(c);
    		    		return isWhitespace(c);
    		    	}

    		    	private interface SpaceCharFilter {
    		    		public boolean isSpaceChar(int ch);
    		    	}

}

    		        class Node{
    		    	int to;
    		    	long dist;
    		    	Node(int to,long dist){
    		    		this.to=to;
    		    		this.dist=dist;
    		    	}

    		    }

class Dsu{
private int rank[], parent[] ,n;
long str[],si[],larg[];
long max=0;
int cnt=0;
private static int[] parent1;
Dsu(int size){
	this.n=size+1;
	rank=new int[n];
	cnt=n-1;
	//parent=new int[n];
    		    		    		parent=new int[n];
    		    		    		str=new long[n];
    		    		    		si=new long[n];

    		    		    		larg=new long[n];
    		    		    		makeSet();

    		    		    	}

    		    		    	void makeSet(){
    		    		    		for(int i=0;i<n;i++){
    		    		    			parent[i]=i;
    		    		    			si[i]=1;
    		    		    		}
    		    		    	}

    		    		    	int find(int x){
    		    		    		if(parent[x]!=x){

    		    		    			parent[x]=find(parent[x]);
    		    		    		}
    		    		    		return parent[x];
    		    		    	}

    		    		    	void join(int ind,long val){
    		    		    		if(str[ind]==0){
    		    		    			str[ind]=val;
    		    		    			larg[ind]=val;
    		    		    			max=Math.max(max, str[ind]);
    		    		    		}
    		    		    		int prev_ind=(ind-1);
    		    		    		int next_ind=(ind+1);
    		    		    		if(prev_ind>0 && larg[prev_ind]!=0){
    		    		    			union(prev_ind,ind);
    		    		    		}
    		    		    		if(next_ind<n && larg[next_ind]!=0){
    		    		    			union(next_ind,ind);
    		    		    		}

    		    		    	}

    		    		    	boolean union(int x,int y){
    		    		    		int xRoot=find(x);
    		    		    		int yRoot=find(y);

    		    		    		if(xRoot==yRoot)
    		    		    			return false;
    		    		    		if(rank[xRoot]<rank[yRoot]){
    		    		    		parent[xRoot]=yRoot;

    		    		    		larg[yRoot]=Math.max(larg[xRoot], larg[yRoot]);

    		    		    		si[yRoot]+=si[xRoot];
    		    		    		str[yRoot]=larg[yRoot]*si[yRoot];
    		    		    			max=Math.max(str[yRoot], max);
    		    		    		}else if(rank[yRoot]<rank[xRoot]){
    		    		    			parent[yRoot]=xRoot;

    		    		    			larg[xRoot]=Math.max(larg[xRoot], larg[yRoot]);

        		    		    		si[xRoot]+=si[yRoot];
        		    		    		str[xRoot]=larg[xRoot]*si[xRoot];
    			    		    		max=Math.max(str[xRoot], max);
    		    		    		}else{
    		    		    			parent[yRoot]=xRoot;
    		    		    			rank[xRoot]++;

    		    		    			larg[xRoot]=Math.max(larg[xRoot], larg[yRoot]);

        		    		    		si[xRoot]+=si[yRoot];
        		    		    		str[xRoot]=larg[xRoot]*si[xRoot];
    			    		    		max=Math.max(str[xRoot], max);

    		    		    		}
    		    		    		cnt--;
    		    		    		return true;
    		    		    	}

    		    		    }


    	        			/*
 * Author- Priyam Vora
 * BTech 2nd Year DAIICT
 */

        		    import java.awt.Checkbox;
import java.awt.Point;
import java.io.*;
        		    import java.math.*;
        		    import java.util.*;
import java.util.Map.Entry;

import javax.print.attribute.SetOfIntegerSyntax;
import javax.swing.plaf.FontUIResource;

        		    public class Main{
        		  	private static InputStream stream;
        		    	private static byte[] buf = new byte[1024];
        		    	private static int curChar;
        		    	private static int numChars;
        		    	private static SpaceCharFilter filter;
        		    	private static PrintWriter pw;
        		    	private static long count = 0,mod=1000000007;
        		    //	private static TreeSet<Integer> ts=new TreeSet[200000];
public final static int INF = (int) 1E9;

public static void main(String args[]) {
	InputReader(System.in);
	pw = new PrintWriter(System.out);
    new Thread(null ,new Runnable(){
       public void run(){
           try{
               solve();

               pw.close();
           } catch(Exception e){
               e.printStackTrace();
           }
       }
   },"1",1<<26).start();
   }
    static StringBuilder sb;
    public static void test(){
    	 sb=new StringBuilder();
    	int t=nextInt();
    	while(t-->0){

    		solve();
    		//pw.println();
    		//sb.append("\n");
    	}
    	pw.println(sb);
    }
    public static long pow(long n, long p,long mod) {
		if(p==0)
			return 1;
		if(p==1)
			return n%mod;
		if(p%2==0){
			long temp=pow(n, p/2,mod);
		return (temp*temp)%mod;
		}else{
			 	long temp=pow(n,p/2,mod);
			 	temp=(temp*temp)%mod;
			 	return(temp*n)%mod;

		}
	}
    public static long pow(long n, long p) {
		if(p==0)
			return 1;
		if(p==1)
			return n;
		if(p%2==0){
			long temp=pow(n, p/2);
		return (temp*temp);
		}else{
			 	long temp=pow(n,p/2);
			 	temp=(temp*temp);
			 	return(temp*n);

		}
	}
    public static void Merge(long a[],int p,int r){
        if(p<r){
            int q = (p+r)/2;
            Merge(a,p,q);
            Merge(a,q+1,r);
            Merge_Array(a,p,q,r);
        }
    }
    public static void Merge_Array(long a[],int p,int q,int r){
       long b[] = new long[q-p+1];
        long c[] = new long[r-q];
        for(int i=0;i<b.length;i++)
            b[i] = a[p+i];
        for(int i=0;i<c.length;i++)
            c[i] = a[q+i+1];
        int i = 0,j = 0;
        for(int k=p;k<=r;k++){
            if(i==b.length){
                a[k] = c[j];
                j++;
            }
            else if(j==c.length){
                a[k] = b[i];
                i++;
            }
            else if(b[i]<c[j]){
                a[k] = b[i];
                i++;
            }
            else{
                a[k] = c[j];
                j++;
            }
        }
    }

	public static long gcd(long x, long y) {
		if (x == 0)
			return y;
		else
			return gcd( y % x,x);
	}

	public static boolean isPrime(int n) {
		if (n <= 1)
			return false;
		if (n <= 3)
			return true;

		if (n % 2 == 0 || n % 3 == 0)
			return false;

		for (int i = 5; i * i <= n; i = i + 6)
			if (n % i == 0 || n % (i + 2) == 0)
				return false;

		return true;
	}

        static LinkedList<Integer> adj[];
        static boolean Visited[];
        static HashSet<Integer> exc;
        static long oddsum[]=new long[1000001];
        static long co=0,ans=0;

    	private static void buildgraph(int n){
    		adj=new LinkedList[n+1];
    		Visited=new boolean[n+1];
    		levl=new int[n+1];

    		for(int i=0;i<=n;i++){
    			adj[i]=new LinkedList<Integer>();

    		}

    	}

        static int[] levl;
        static int[] eat;
        static int n,m;
        static int price[];
        //ind frog crab
    public static void solve(){

    	int n=nextInt();
    	long a[]=nextLongArray(n);
    	long inc[]=new long[n];
    	long dec[]=new long[n];
    	long temp[]=new long[n];
    	for(int i=0;i<n;i++){
    		temp[i]=a[i];
    	}
    	for(int i=1;i<n;i++){
    		if(a[i]<=a[i-1]){
    			inc[i]+=(a[i-1]+1-a[i]);
    			a[i]=a[i-1]+1;
    		}
    		inc[i]+=inc[i-1];

    	}
    	for(int i=0;i<n;i++){
    		a[i]=temp[i];
    	}
    	for(int i=n-2;i>=0;i--){
    		if(a[i]<=a[i+1]){
    			dec[i]+=(a[i+1]+1-a[i]);
    			a[i]=a[i+1]+1;
    		}
    		dec[i]+=dec[i+1];

    	}
    	long f_ans=Long.MAX_VALUE;
    	for(int i=0;i<n;i++){
    		long aft=0;
    		long prev=0;
    		if(i<n-1){
    			aft=dec[i+1];
    		}
    			prev=inc[i];

    		long ans=aft+prev;;

    		f_ans=Math.min(f_ans, ans);
    	}
    	pw.println(f_ans);
    	//pArray(inc);
    	//pArray(dec);

    }
    public static boolean isVowel(char c){
    	if(c=='a' || c=='e'||c=='i' || c=='o' || c=='u')
    		return true;
    	return false;
    }
static int no_vert=0;

  private static void dfs(int start,int cake){
	  Visited[start]=true;
	  no_vert++;
	  eat[start]=cake;
	  for(int i:adj[start]){
		  if(!Visited[i])
			  {
			//  pw.println();
			  dfs(i,cake);
			  }
	  }

  }

    public static String reverseString(String s) {
		StringBuilder sb = new StringBuilder(s);
		sb.reverse();
		return (sb.toString());
	}

  /*
   private static void BFS(int sou){
    	Queue<Integer> q=new LinkedList<Integer>();
    	q.add(sou);
    	Visited[sou]=true;
    	levl[sou]=0;
    	while(!q.isEmpty()){
    		int top=q.poll();

    		for(int i:adj[top]){
    			//pw.println(i+" "+top);
			if(!Visited[i])
			{

				q.add(i);
				levl[i]=levl[top]+1;
			}

			Visited[i]=true;

			}
		}
	}*/

    		      static int indeg[];
    		   /* private static void kahn(int n){

    		    	PriorityQueue<Integer> q=new PriorityQueue<Integer>();
    				for(int i=1;i<=n;i++){
    					if(indeg[i]==0){
    						q.add(i);
    					}
    				}
    				while(!q.isEmpty()){
    					int top=q.poll();
    					st.push(top);
    					for(Node i:adj[top]){
    						indeg[i.to]--;
    						if(indeg[i.to]==0){
    							q.add(i.to);
    						}
    					}
    				}
    		    }

    		        static int state=1;
    		        static long no_exc=0,no_vert=0;
    		      static Stack<Integer> st;
    		      static HashSet<Integer> inset;
    		     /*   private static void topo(int curr){

    		        	Visited[curr]=true;
    		        	inset.add(curr);
    		        	for(int x:adj[curr]){
    		        		if(adj[x].contains(curr) || inset.contains(x)){
    		        			state=0;
    		        			return;
    		        		}
    		        		if(state==0)
    		        			return;

    		        	}
    		        	st.push(curr);

    		        	inset.remove(curr);
    		        }*/
    		        static HashSet<Integer> hs;

    		     	static boolean prime[];
					static int spf[];
					public static void sieve(int n){
						prime=new boolean[n+1];
						spf=new int[n+1];

						Arrays.fill(spf, 1);
						Arrays.fill(prime, true);
					prime[1]=false;
						spf[2]=2;

					for(int i=4;i<=n;i+=2){
						spf[i]=2;
					}
					for(int i=3;i<=n;i+=2){
						if(prime[i]){
							spf[i]=i;
							for(int j=2*i;j<=n;j+=i){

								prime[j]=false;
							if(spf[j]==1){
								spf[j]=i;
							}
							}
						}
					}

					}

    		    	// To Get Input
// Some Buffer Methods

    		     	public static void sort(long a[]){
    		     		Merge(a, 0, a.length-1);
    		     	}
    		    	public static void InputReader(InputStream stream1) {
    		    		stream = stream1;
    		    	}

    		    	private static boolean isWhitespace(int c) {
    		    		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    		    	}

    		    	private static boolean isEndOfLine(int c) {
    		    		return c == '\n' || c == '\r' || c == -1;
    		    	}

    		    	private static int read() {
    		    		if (numChars == -1)
    		    			throw new InputMismatchException();
    		    		if (curChar >= numChars) {
    		    			curChar = 0;
    		    			try {
    		    				numChars = stream.read(buf);
    		    			} catch (IOException e) {
    		    				throw new InputMismatchException();
    		    			}
    		    			if (numChars <= 0)
    		    				return -1;
    		    		}
    		    		return buf[curChar++];
    		    	}

    		    	private static int nextInt() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		int sgn = 1;
    		    		if (c == '-') {
		sgn = -1;
		c = read();
	}
	int res = 0;
	do {
		if (c < '0' || c > '9')
			throw new InputMismatchException();
		res *= 10;
		res += c - '0';
    		    			c = read();
    		    		} while (!isSpaceChar(c));
    		    		return res * sgn;
    		    	}

    		    	private static long nextLong() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		int sgn = 1;
    		    		if (c == '-') {
		sgn = -1;
		c = read();
	}
	long res = 0;
	do {
		if (c < '0' || c > '9')
			throw new InputMismatchException();
		res *= 10;
		res += c - '0';
    		    			c = read();
    		    		} while (!isSpaceChar(c));
    		    		return res * sgn;
    		    	}

    		    	private static String nextToken() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		StringBuilder res = new StringBuilder();
    		    		do {
    		    			res.appendCodePoint(c);
    		    			c = read();
    		    		} while (!isSpaceChar(c));
    		    		return res.toString();
    		    	}

    		    	private static String nextLine() {
    		    		int c = read();
    		    		while (isSpaceChar(c))
    		    			c = read();
    		    		StringBuilder res = new StringBuilder();
    		    		do {
    		    			res.appendCodePoint(c);
    		    			c = read();
    		    		} while (!isEndOfLine(c));
    		    		return res.toString();
    		    	}

    		    	private static int[] nextIntArray(int n) {
    		    		int[] arr = new int[n];
    		    		for (int i = 0; i < n; i++) {
    		    			arr[i] = nextInt();
    		    		}
    		    		return arr;
    		    	}

    		    	private static long[][] next2dArray(int n, int m) {
    		    		long[][] arr = new long[n][m];
    		    		for (int i = 0; i < n; i++) {
    		    			for (int j = 0; j < m; j++) {
    		    				arr[i][j] = nextLong();
    		    			}
    		    		}
    		    		return arr;
    		    	}
    		    	private static char[][] nextCharArray(int n,int m){
    		    		char [][]c=new char[n][m];
    		    		for(int i=0;i<n;i++){
    		    			String s=nextLine();
    		    			for(int j=0;j<s.length();j++){
    		    				c[i][j]=s.charAt(j);
    		    			}
    		    		}
    		    		return c;
    		    	}

    		    	private static long[] nextLongArray(int n) {
    		    		long[] arr = new long[n];
    		    		for (int i = 0; i < n; i++) {
    		    			arr[i] = nextLong();
    		    		}
    		    		return arr;
    		    	}

    		    	private static void pArray(int[] arr) {
    		    		for (int i = 0; i < arr.length; i++) {
    		    			pw.print(arr[i] + " ");
    		    		}
    		    		pw.println();
    		    		return;
    		    	}

    		    	private static void pArray(long[] arr) {
    		    		for (int i = 0; i < arr.length; i++) {
    		    			pw.print(arr[i] + " ");
    		    		}
    		    		pw.println();
    		    		return;
    		    	}

    		    	private static void pArray(boolean[] arr) {
    		    		for (int i = 0; i < arr.length; i++) {
    		    			pw.print(arr[i] + " ");
    		    		}
    		    		pw.println();
    		    		return;
    		    	}

    		    	private static boolean isSpaceChar(int c) {
    		    		if (filter != null)
    		    			return filter.isSpaceChar(c);
    		    		return isWhitespace(c);
    		    	}

    		    	private interface SpaceCharFilter {
    		    		public boolean isSpaceChar(int ch);
    		    	}

}

    		        class Node{
    		    	int to;
    		    	long dist;
    		    	Node(int to,long dist){
    		    		this.to=to;
    		    		this.dist=dist;
    		    	}

    		    }

class Dsu{
private int rank[], parent[] ,n;
long str[],si[],larg[];
long max=0;
int cnt=0;
private static int[] parent1;
Dsu(int size){
	this.n=size+1;
	rank=new int[n];
	cnt=n-1;
	//parent=new int[n];
    		    		    		parent=new int[n];
    		    		    		str=new long[n];
    		    		    		si=new long[n];

    		    		    		larg=new long[n];
    		    		    		makeSet();

    		    		    	}

    		    		    	void makeSet(){
    		    		    		for(int i=0;i<n;i++){
    		    		    			parent[i]=i;
    		    		    			si[i]=1;
    		    		    		}
    		    		    	}

    		    		    	int find(int x){
    		    		    		if(parent[x]!=x){

    		    		    			parent[x]=find(parent[x]);
    		    		    		}
    		    		    		return parent[x];
    		    		    	}

    		    		    	void join(int ind,long val){
    		    		    		if(str[ind]==0){
    		    		    			str[ind]=val;
    		    		    			larg[ind]=val;
    		    		    			max=Math.max(max, str[ind]);
    		    		    		}
    		    		    		int prev_ind=(ind-1);
    		    		    		int next_ind=(ind+1);
    		    		    		if(prev_ind>0 && larg[prev_ind]!=0){
    		    		    			union(prev_ind,ind);
    		    		    		}
    		    		    		if(next_ind<n && larg[next_ind]!=0){
    		    		    			union(next_ind,ind);
    		    		    		}

    		    		    	}

    		    		    	boolean union(int x,int y){
    		    		    		int xRoot=find(x);
    		    		    		int yRoot=find(y);

    		    		    		if(xRoot==yRoot)
    		    		    			return false;
    		    		    		if(rank[xRoot]<rank[yRoot]){
    		    		    		parent[xRoot]=yRoot;

    		    		    		larg[yRoot]=Math.max(larg[xRoot], larg[yRoot]);

    		    		    		si[yRoot]+=si[xRoot];
    		    		    		str[yRoot]=larg[yRoot]*si[yRoot];
    		    		    			max=Math.max(str[yRoot], max);
    		    		    		}else if(rank[yRoot]<rank[xRoot]){
    		    		    			parent[yRoot]=xRoot;

    		    		    			larg[xRoot]=Math.max(larg[xRoot], larg[yRoot]);

        		    		    		si[xRoot]+=si[yRoot];
        		    		    		str[xRoot]=larg[xRoot]*si[xRoot];
    			    		    		max=Math.max(str[xRoot], max);
    		    		    		}else{
    		    		    			parent[yRoot]=xRoot;
    		    		    			rank[xRoot]++;

    		    		    			larg[xRoot]=Math.max(larg[xRoot], larg[yRoot]);

        		    		    		si[xRoot]+=si[yRoot];
        		    		    		str[xRoot]=larg[xRoot]*si[xRoot];
    			    		    		max=Math.max(str[xRoot], max);

    		    		    		}
    		    		    		cnt--;
    		    		    		return true;
    		    		    	}

    		    		    }


	import java.io.*;

import java.math.*;
import java.nio.file.attribute.AclEntry.Builder;
import java.security.KeyStore.Entry;
import java.util.*;

	public  class Khatri {
		private InputStream is;
		private PrintWriter out;
		int time = 0, DP[], start[], end[], dist[],  black[];
		static int MOD = (int)(1e9+7);
		int arr[];
		int weight[][];
		int x[];
		int y[];
		int parent[];
		int MAX = 800000, N, K, L;
		long red[];
		ArrayList<Integer>[] amp;
		HashSet<Integer> hs = new HashSet<>();
		long Dp[][][][] = new long[110][110][12][12];
		public static void main(String[] args) throws Exception
		{
			new Thread(null, new Runnable()
			{
			public void run()
			{
				try {

			//new CODEFORCES().soln();
			} catch (Exception e)
				{
					System.out.println(e);
				}
			}
		}, "1", 1 << 26).start();
			new Khatri().soln();
		}
		char ch[][];
		//int ans = 0, cnt = 0, Q;
		int n1, n2, k1, k2, M;
		static ArrayList<Integer>[] g;
        static ArrayList<Integer> ar[];
		static long ok[];
		//static int a[];
		static int phi[]=new int[500005];
		void solve()
        {

			int n=ni();
		    long a[]=new long[n];
		    for(int i=0;i<n;i++)
		    	a[i]=nl();
		    long c[]=Arrays.copyOf(a, n);
		    int acs=0;int dcs=0;
		   long b[]=new long[n];
		    b[0]=a[0];
		    for(int i=1;i<n;i++)
		    {
		    b[i]=b[i-1]+1;
		    }
		    long toy=0;
		    for(int i=0;i<n;i++)
		    {
		    	if(b[i]>=a[i])
		    	{
		    		toy+=(long)(b[i]-a[i]);
		    	}
		    	else
		    	{
		    		toy=0;
		    		break;
		    	}
		    }
		    long ans=Integer.MAX_VALUE;

		    if(toy!=0)
		    	ans=toy;
		     toy=0;
		      b=new long[n];
			    b[n-1]=a[n-1];
			    for(int i=n-2;i>=0;i--)
			    {
			    b[i]=b[i+1]+1;
			    }
		    for(int i=0;i<n;i++)
		    {
		    	if(b[i]>=a[i])
		    	{
		    		toy+=(long)(b[i]-a[i]);
		    	}
		    	else
		    	{
		    		toy=0;
		    		break;
		    	}
		    }
		    if(toy!=0)
		    {
		    	ans=Math.min(toy, ans);
		    }

		    for(int i=0;i<n-1;i++)
		    	if(a[i]<a[i+1])
		    		acs++;
		    if(acs==n-1)
		    {
		    	out.print(0);
		    	return;
		    }
		    for(int i=0;i<n-1;i++)
		    	if(a[i]>a[i+1])
		    		dcs++;
		    if(dcs==n-1)
		    {
		    	out.print(0);
		    	return;
		    }
		    int right[]=new int[n];
		    right[n-1]=1;
		    for(int i=n-2;i>=0;i--)
		    {
		    if(a[i]>a[i+1])
		    {
		    	right[i]=1;
		    }
		    else
		    	break;
		    }
		    long sum[]=new long[n];
		    sum[0]=(long)0;
		    for(int i=0;i<n-1;i++)
		    {
		    	if(a[i]<a[i+1])
		    	{
		    		continue;
		    	}
		    	else
		    	{
		    		long tmp=a[i]-a[i+1]+1;
		    		a[i+1]=a[i]+1;
		    		sum[i+1]=tmp;
		    	}
		    }
		    for(int i=1;i<n;i++)
		    	sum[i]+=sum[i-1];

		    long sum2[]=new long[n];
		    sum2[n-1]=(long)0;
		    for(int i=n-2;i>=0;i--)
		    {
		    	if(c[i]>c[i+1])
		    	{
		    		continue;
		    	}
		    	else
		    	{
		    		long tmp=c[i+1]-c[i]+1;
		    		c[i]=c[i+1]+1;
		    		sum2[i]=tmp;
		    	}
		    }
		   for(int i=n-2;i>=0;i--)
		   {
			   sum2[i]+=sum2[i+1];
		   }
		   for(int i=0;i<n-1;i++)
		   {
			   if(a[i]>c[i+1])
			   {
				   ans=Math.min(sum[i]+sum2[i+1],ans);
			   }
		   }

		    out.print(ans);

        }

            boolean isPrime(int x)
            {
            if(x==0||x==1)
            	return false;
			for(int i = 2;i*1L*i<=x;i++) if(x%i==0) return false;
			return true;
		}
	    int  p ;
		void buildGraph(int n)
		{
			for(int i = 0; i<n ;i++){
				int x1 = ni() -1, y1 = ni()-1;
				amp[x1].add(y1);
				amp[y1].add(x1);
				x[i] = x1; y[i] = y1;
			}
		}
		long get(int x){
			long val = 0;
			for(int i = 0; i< N; i++){
				val += Math.abs(x-arr[i]);
				x+=L;
			}
			return val;
		}

		long modInverse(long a, long mOD2){
	        return  power(a, mOD2-2, mOD2);
		}
		long power(long x, long y, long m)
		{
			if (y == 0)
	    return 1;
		long p = power(x, y/2, m) % m;
		p = (p * p) % m;

		return (y%2 == 0)? p : (x * p) % m;
		}

		public static long gcd(long a, long b){
			if(b==0) return a;
			return gcd(b,a%b);
		}

		public static int[] shuffle(int[] a, Random gen){
			for(int i = 0, n = a.length;i < n;i++)
			{
				int ind = gen.nextInt(n-i)+i;
				int d = a[i];
				a[i] = a[ind];
				a[ind] = d;
			}
		return a;
		}

		class Pair1 implements Comparable<Pair1>{
			String s;
			int val;
			Pair1(String s,int time){
			this.s=s;
			this.val=time;
			}
			public int hashCode() {
				return Objects.hash();
			}

			@Override
			public int compareTo(Pair1 arg0) {
				return (int)(arg0.val-val);
			}

		}
		long power(long x, long y, int mod){
			long ans = 1;
			while(y>0){
				if(y%2==0){
					x = (x*x)%mod;
					y/=2;
				}
				else{
					ans = (x*ans)%mod;
					y--;
				}
			}
			return ans;
		}
		void soln() {
			is = System.in;
			out = new PrintWriter(System.out);
			long s = System.currentTimeMillis();
			solve();
			//out.close();
			out.flush();
			//tr(System.currentTimeMillis() - s + "ms");
		}

		// To Get Input
		// Some Buffer Methods
		private byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		private int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		private boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		private int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

		private double nd() {
			return Double.parseDouble(ns());
		}

		private char nc() {
			return (char) skip();
		}
		private String ns() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
										// ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		private char[] ns(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		private int[][] nm(int n, int m) {
			int[][] map = new int[n][m];
			for (int i = 0; i < n; i++)
			{
				for(int j=0;j<m;j++)
					map[i][j]=ni();
			}
			return map;
		}

		private int[] na(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = ni();
			return a;
		}

		private int ni() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private long nl() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

		private void tr(Object... o) {
			if (!oj)
				System.out.println(Arrays.deepToString(o));
		}
	}


import java.io.*;
import java.util.*;

public class main
{
	private static InputStream stream;
	private static byte[] buf = new byte[1024];
	private static int curChar;
	private static int numChars;
	private static SpaceCharFilter filter;
	private static PrintWriter pw;
	//private static long mod = 1000000000 + 7;
	private static long mod=100000000;
	private static long mod1 = 1000000000 + 9;
	private static void soln()
	{
		int n=nextInt();
		int[] arr=nextIntArray(n);
		long[] l=new long[n];
		long[] r=new long[n];
		int prev=arr[0];
		for(int i=1;i<n;i++) {
			int cur=Math.max(arr[i],prev+1);
			prev=cur;
			l[i]=l[i-1]+(long)(cur-arr[i]);
		}
		prev=arr[n-1];
		for(int i=n-2;i>=0;i--) {
			int cur=Math.max(arr[i],prev+1);
			prev=cur;
			r[i]=r[i+1]+(long)(cur-arr[i]);
		}
		long min=r[0];
		for(int i=0;i<n-1;i++) {
			min=Math.min(min,l[i]+r[i+1]);
		}
		min=Math.min(min, l[n-1]);
		pw.println(min);
 	}
	private static int[] buildSuffix(String s,int n){
		Integer[] order=new Integer[n];
		for(int i=0;i<n;i++) order[i]=n-i-1;
		Arrays.sort(order,(a,b) -> Character.compare(s.charAt(a),s.charAt(b)));
		int[] sa=new int[n];
		int[] m_tmp=new int[n];
		for(int i=0;i<n;i++){
			sa[i]=order[i];
			m_tmp[i]=s.charAt(i);
		}
		for(int len=1;len<n;len<<=1){
			int[] tmp=m_tmp.clone();
			m_tmp[sa[0]]=0;
			for(int i=1;i<n;i++){
				m_tmp[sa[i]] = (tmp[sa[i-1]]==tmp[sa[i]] && sa[i-1] + len < n &&
								tmp[(sa[i-1]+(len>>1))]==tmp[(sa[i]+(len>>1))]) ? m_tmp[sa[i-1]]:i;
			}
			int[] cnt=new int[n];
			for(int i=0;i<n;i++) cnt[i]=i;
			int[] sa_tmp=sa.clone();
			for(int i=0;i<n;i++){
				int s1=sa_tmp[i]-len;
				if(s1>=0)
					sa[cnt[m_tmp[s1]]++]=s1;
			}
		}
		return sa;
	}
	private static int[] buildLCP(int[] sa,String s,int n){
		int[] ind=new int[n];
		for(int i=0;i<n;i++)
			ind[sa[i]]=i;
		int[] lcp=new int[n];
		for(int i=0, k=0; i<n; i++)
			if(ind[i] < n-1){
				for(int j=sa[ind[i]+1]; Math.max(i,j)+k < n && s.charAt(i+k)==s.charAt(j+k);) k++;
				lcp[ind[i]]=k;
				if(k>0) k--;
			}
		return lcp;
	}
	private static class Edge{
		int u,v;
		public Edge(int a,int b) {
			u=a;v=b;
		}
	}
	private static void shortestPath(LinkedList<Edge>[] l,int n,int k,int[] masks) {
		int tot=(int) Math.pow(2,k);
		int[][] dist=new int[n][tot];
		int[][] from=new int[n][tot];
		for(int i=0;i<n;i++)
			Arrays.fill(dist[i], Integer.MAX_VALUE);
		dist[0][masks[0]]=0;
		from[0][masks[0]]=-1;
		PriorityQueue<Long> q=new PriorityQueue<>();
		q.add((long) masks[0]);
		//System.out.println(0);
		while(!q.isEmpty()) {
			long cur=q.poll();
			//System.out.println(cur);
			int u = ((int) cur)>>>10;
			int mask=(int) (cur&(tot-1));
			//System.out.println((u+1)+" "+mask+" "+dist[u][mask]);
			if((cur>>>32) != dist[u][mask])
				continue;
			//System.out.println((u+1)+" "+Integer.toBinaryString(mask)+" "+dist[u][mask]);
			Iterator<Edge> it=l[u].listIterator();
			while(it.hasNext()) {
				Edge e=it.next();
				int len=dist[u][mask]+e.v;
				if(dist[e.u][mask|masks[e.u]]>len) {
					//System.out.println((e.u+1)+" "+(mask|masks[e.u])+" "+len+" "+Long.toBinaryString(((long)len << 32)+(e.u<<10)+mask));
					dist[e.u][mask|masks[e.u]]=len;
					from[e.u][mask|masks[e.u]]=u;
					q.add(((long)len << 32)+(e.u<<10)+(mask|masks[e.u]));
				}
			}
		}
		int ans=Integer.MAX_VALUE;
		//System.out.println(Integer.toBinaryString(tot-1));
		for(int i=0;i<tot;i++) {
			for(int j=0;j<tot;j++) {
				//System.out.println(Integer.toBinaryString(i)+" "+Integer.toBinaryString(j)+" "+dist[n-1][i]+" "+dist[n-1][j]);
				if((i|j) != (tot-1))
					continue;
				//System.out.println(i+" "+j);
				ans=Math.min(ans,Math.max(dist[n-1][i],dist[n-1][j]));
			}
		}
		pw.println(ans);
	}
	private static class Pair1 implements Comparable<Pair1>{
		int dist, affected, u;
		public Pair1(int a) {
			dist=Integer.MAX_VALUE;
			affected=Integer.MAX_VALUE;
			u=a;
		}
		public int compareTo(Pair1 another) {
			return this.dist!=another.dist?this.dist-another.dist:this.affected-another.affected;
		}
	}
	static void multiply(long[][] a, long[][] b, long m)
	{
		int n = a.length;
		long[][] mul = new long[n][n];
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				mul[i][j] = 0;
				for (int k = 0; k < n; k++)
					mul[i][j] = (mul[i][j] + (a[i][k] * b[k][j]) % m) % m;
			}
		}
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				a[i][j] = mul[i][j];
	}
	private static class DSU{
		int[] parent;
		int[] rank;
		int[][] lr;
		public DSU(int n){
			parent=new int[n];
			rank=new int[n];
			lr=new int[n][2];
			//cnt=new int[n];
			for(int i=0;i<n;i++){
				parent[i]=i;
				rank[i]=1;
				lr[i][0]=lr[i][1]=i;
				//cnt[i]=1;
			}
		}
		int find(int i){
	        while(parent[i] !=i){
	        	parent[i]=parent[parent[i]];
	        	i=parent[i];
	        }
	        return i;
	    }
		int Union(int x, int y){
	        int xset = find(x);
	        int yset = find(y);
	        if(xset==yset)
	        	return 0;
	        	parent[yset]=xset;
	        	rank[xset]+=rank[yset];
	        	rank[yset]=0;
	        	lr[xset][1]=lr[yset][1];
	        	lr[yset][0]=lr[xset][0];
	        	//cnt[xset]+=cnt[yset];
	        	//cnt[yset]=0;
	        	return xset;
	    }
	}

	private static long pow(long a, long b, long c)
	{
		if (b == 0)
			return 1;
		long p = pow(a, b / 2, c);
		p = (p * p) % c;
		return (b % 2 == 0) ? p : (a * p) % c;
	}
	private static int block;
	private static class Mo implements Comparable<Mo>
	{
		int l;
		int r;
		int i;

		public Mo(int a, int b, int ind)
		{
			l = a;
			r = b;
			i = ind;
		}

		@Override
		public int compareTo(Mo arg0)
		{
			int b1 = this.l / block;
			int b2 = arg0.l / block;
			return (b1 != b2) ? (b1 - b2) : (this.r - arg0.r);
		}
	}

	private static long gcd(long n, long l)
	{
		if (l == 0)
			return n;
		return gcd(l, n % l);
	}
	public static void main(String[] args) throws Exception
	{
		new Thread(null, new Runnable()
		{
			@Override
			public void run()
			{
				/*
				 * try { InputReader(new FileInputStream(
				 * "C:\\Users\\hardik\\Desktop\\C-small-1-attempt1.in")); } catch
				 * (FileNotFoundException e) { // TODO Auto-generated catch block
				 * e.printStackTrace(); }
				 */
				InputReader(System.in);
				pw = new PrintWriter(System.out);
				/*
				 * try { pw=new PrintWriter(new
				 * FileOutputStream("C:\\Users\\hardik\\Desktop\\out.txt")); } catch
				 * (FileNotFoundException e) { // TODO Auto-generated catch block
				 * e.printStackTrace(); }
				 */
				soln();
				pw.close();
			}
		}, "1", 1 << 26).start();
	}

	public static void InputReader(InputStream stream1)
	{
		stream = stream1;
	}

	private static boolean isWhitespace(int c)
	{
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private static boolean isEndOfLine(int c)
	{
		return c == '\n' || c == '\r' || c == -1;
	}

	private static int read()
	{
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars)
		{
			curChar = 0;
			try
			{
				numChars = stream.read(buf);
			} catch (IOException e)
			{
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	private static int nextInt()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-')
		{
			sgn = -1;
			c = read();
		}
		int res = 0;
		do
		{
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	private static long nextLong()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-')
		{
			sgn = -1;
			c = read();
		}
		long res = 0;
		do
		{
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	private static String nextToken()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do
		{
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

	private static String nextLine()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do
		{
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	private static int[] nextIntArray(int n)
	{
		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
		{
			arr[i] = nextInt();
		}
		return arr;
	}

	private static long[] nextLongArray(int n)
	{
		long[] arr = new long[n];
		for (int i = 0; i < n; i++)
		{
			arr[i] = nextLong();
		}
		return arr;
	}

	private static void pArray(int[] arr)
	{
		for (int i = 0; i < arr.length; i++)
		{
			System.out.print(arr[i] + " ");
		}
		System.out.println();
		return;
	}

	private static void pArray(long[] arr)
	{
		for (int i = 0; i < arr.length; i++)
		{
			System.out.print(arr[i] + " ");
		}
		System.out.println();
		return;
	}

	private static boolean isSpaceChar(int c)
	{
		if (filter != null)
			return filter.isSpaceChar(c);
		return isWhitespace(c);
	}

	private static char nextChar()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		char c1 = (char) c;
		while (!isSpaceChar(c))
			c = read();
		return c1;
	}

	private interface SpaceCharFilter
	{
		public boolean isSpaceChar(int ch);
	}
}


import java.io.*;
import java.util.*;

public class main
{
	private static InputStream stream;
	private static byte[] buf = new byte[1024];
	private static int curChar;
	private static int numChars;
	private static SpaceCharFilter filter;
	private static PrintWriter pw;
	//private static long mod = 1000000000 + 7;
	private static long mod=100000000;
	private static long mod1 = 1000000000 + 9;
	private static void soln()
	{
		int n=nextInt();
		int[] arr=nextIntArray(n);
		long[] l=new long[n];
		long[] r=new long[n];
		int[] lv=new int[n];
		int[] rv=new int[n];
		lv[0]=arr[0];
		int prev=arr[0];
		for(int i=1;i<n;i++) {
			int cur=Math.max(arr[i],prev+1);
			lv[i]=cur;
			prev=cur;
			l[i]=l[i-1]+(long)(cur-arr[i]);
		}
		prev=arr[n-1];
		rv[n-1]=arr[n-1];
		for(int i=n-2;i>=0;i--) {
			int cur=Math.max(arr[i],prev+1);
			prev=cur;
			rv[i]=cur;
			r[i]=r[i+1]+(long)(cur-arr[i]);
		}
		long min=r[0];
		for(int i=0;i<n-1;i++) {
			if(lv[i]>rv[i+1])
				min=Math.min(min,l[i]+r[i+1]);
		}
		min=Math.min(min, l[n-1]);
		pw.println(min);
 	}
	private static int[] buildSuffix(String s,int n){
		Integer[] order=new Integer[n];
		for(int i=0;i<n;i++) order[i]=n-i-1;
		Arrays.sort(order,(a,b) -> Character.compare(s.charAt(a),s.charAt(b)));
		int[] sa=new int[n];
		int[] m_tmp=new int[n];
		for(int i=0;i<n;i++){
			sa[i]=order[i];
			m_tmp[i]=s.charAt(i);
		}
		for(int len=1;len<n;len<<=1){
			int[] tmp=m_tmp.clone();
			m_tmp[sa[0]]=0;
			for(int i=1;i<n;i++){
				m_tmp[sa[i]] = (tmp[sa[i-1]]==tmp[sa[i]] && sa[i-1] + len < n &&
								tmp[(sa[i-1]+(len>>1))]==tmp[(sa[i]+(len>>1))]) ? m_tmp[sa[i-1]]:i;
			}
			int[] cnt=new int[n];
			for(int i=0;i<n;i++) cnt[i]=i;
			int[] sa_tmp=sa.clone();
			for(int i=0;i<n;i++){
				int s1=sa_tmp[i]-len;
				if(s1>=0)
					sa[cnt[m_tmp[s1]]++]=s1;
			}
		}
		return sa;
	}
	private static int[] buildLCP(int[] sa,String s,int n){
		int[] ind=new int[n];
		for(int i=0;i<n;i++)
			ind[sa[i]]=i;
		int[] lcp=new int[n];
		for(int i=0, k=0; i<n; i++)
			if(ind[i] < n-1){
				for(int j=sa[ind[i]+1]; Math.max(i,j)+k < n && s.charAt(i+k)==s.charAt(j+k);) k++;
				lcp[ind[i]]=k;
				if(k>0) k--;
			}
		return lcp;
	}
	private static class Edge{
		int u,v;
		public Edge(int a,int b) {
			u=a;v=b;
		}
	}
	private static void shortestPath(LinkedList<Edge>[] l,int n,int k,int[] masks) {
		int tot=(int) Math.pow(2,k);
		int[][] dist=new int[n][tot];
		int[][] from=new int[n][tot];
		for(int i=0;i<n;i++)
			Arrays.fill(dist[i], Integer.MAX_VALUE);
		dist[0][masks[0]]=0;
		from[0][masks[0]]=-1;
		PriorityQueue<Long> q=new PriorityQueue<>();
		q.add((long) masks[0]);
		//System.out.println(0);
		while(!q.isEmpty()) {
			long cur=q.poll();
			//System.out.println(cur);
			int u = ((int) cur)>>>10;
			int mask=(int) (cur&(tot-1));
			//System.out.println((u+1)+" "+mask+" "+dist[u][mask]);
			if((cur>>>32) != dist[u][mask])
				continue;
			//System.out.println((u+1)+" "+Integer.toBinaryString(mask)+" "+dist[u][mask]);
			Iterator<Edge> it=l[u].listIterator();
			while(it.hasNext()) {
				Edge e=it.next();
				int len=dist[u][mask]+e.v;
				if(dist[e.u][mask|masks[e.u]]>len) {
					//System.out.println((e.u+1)+" "+(mask|masks[e.u])+" "+len+" "+Long.toBinaryString(((long)len << 32)+(e.u<<10)+mask));
					dist[e.u][mask|masks[e.u]]=len;
					from[e.u][mask|masks[e.u]]=u;
					q.add(((long)len << 32)+(e.u<<10)+(mask|masks[e.u]));
				}
			}
		}
		int ans=Integer.MAX_VALUE;
		//System.out.println(Integer.toBinaryString(tot-1));
		for(int i=0;i<tot;i++) {
			for(int j=0;j<tot;j++) {
				//System.out.println(Integer.toBinaryString(i)+" "+Integer.toBinaryString(j)+" "+dist[n-1][i]+" "+dist[n-1][j]);
				if((i|j) != (tot-1))
					continue;
				//System.out.println(i+" "+j);
				ans=Math.min(ans,Math.max(dist[n-1][i],dist[n-1][j]));
			}
		}
		pw.println(ans);
	}
	private static class Pair1 implements Comparable<Pair1>{
		int dist, affected, u;
		public Pair1(int a) {
			dist=Integer.MAX_VALUE;
			affected=Integer.MAX_VALUE;
			u=a;
		}
		public int compareTo(Pair1 another) {
			return this.dist!=another.dist?this.dist-another.dist:this.affected-another.affected;
		}
	}
	static void multiply(long[][] a, long[][] b, long m)
	{
		int n = a.length;
		long[][] mul = new long[n][n];
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				mul[i][j] = 0;
				for (int k = 0; k < n; k++)
					mul[i][j] = (mul[i][j] + (a[i][k] * b[k][j]) % m) % m;
			}
		}
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				a[i][j] = mul[i][j];
	}
	private static class DSU{
		int[] parent;
		int[] rank;
		int[][] lr;
		public DSU(int n){
			parent=new int[n];
			rank=new int[n];
			lr=new int[n][2];
			//cnt=new int[n];
			for(int i=0;i<n;i++){
				parent[i]=i;
				rank[i]=1;
				lr[i][0]=lr[i][1]=i;
				//cnt[i]=1;
			}
		}
		int find(int i){
	        while(parent[i] !=i){
	        	parent[i]=parent[parent[i]];
	        	i=parent[i];
	        }
	        return i;
	    }
		int Union(int x, int y){
	        int xset = find(x);
	        int yset = find(y);
	        if(xset==yset)
	        	return 0;
	        	parent[yset]=xset;
	        	rank[xset]+=rank[yset];
	        	rank[yset]=0;
	        	lr[xset][1]=lr[yset][1];
	        	lr[yset][0]=lr[xset][0];
	        	//cnt[xset]+=cnt[yset];
	        	//cnt[yset]=0;
	        	return xset;
	    }
	}

	private static long pow(long a, long b, long c)
	{
		if (b == 0)
			return 1;
		long p = pow(a, b / 2, c);
		p = (p * p) % c;
		return (b % 2 == 0) ? p : (a * p) % c;
	}
	private static int block;
	private static class Mo implements Comparable<Mo>
	{
		int l;
		int r;
		int i;

		public Mo(int a, int b, int ind)
		{
			l = a;
			r = b;
			i = ind;
		}

		@Override
		public int compareTo(Mo arg0)
		{
			int b1 = this.l / block;
			int b2 = arg0.l / block;
			return (b1 != b2) ? (b1 - b2) : (this.r - arg0.r);
		}
	}

	private static long gcd(long n, long l)
	{
		if (l == 0)
			return n;
		return gcd(l, n % l);
	}
	public static void main(String[] args) throws Exception
	{
		new Thread(null, new Runnable()
		{
			@Override
			public void run()
			{
				/*
				 * try { InputReader(new FileInputStream(
				 * "C:\\Users\\hardik\\Desktop\\C-small-1-attempt1.in")); } catch
				 * (FileNotFoundException e) { // TODO Auto-generated catch block
				 * e.printStackTrace(); }
				 */
				InputReader(System.in);
				pw = new PrintWriter(System.out);
				/*
				 * try { pw=new PrintWriter(new
				 * FileOutputStream("C:\\Users\\hardik\\Desktop\\out.txt")); } catch
				 * (FileNotFoundException e) { // TODO Auto-generated catch block
				 * e.printStackTrace(); }
				 */
				soln();
				pw.close();
			}
		}, "1", 1 << 26).start();
	}

	public static void InputReader(InputStream stream1)
	{
		stream = stream1;
	}

	private static boolean isWhitespace(int c)
	{
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private static boolean isEndOfLine(int c)
	{
		return c == '\n' || c == '\r' || c == -1;
	}

	private static int read()
	{
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars)
		{
			curChar = 0;
			try
			{
				numChars = stream.read(buf);
			} catch (IOException e)
			{
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	private static int nextInt()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-')
		{
			sgn = -1;
			c = read();
		}
		int res = 0;
		do
		{
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	private static long nextLong()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-')
		{
			sgn = -1;
			c = read();
		}
		long res = 0;
		do
		{
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	private static String nextToken()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do
		{
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

	private static String nextLine()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do
		{
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	private static int[] nextIntArray(int n)
	{
		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
		{
			arr[i] = nextInt();
		}
		return arr;
	}

	private static long[] nextLongArray(int n)
	{
		long[] arr = new long[n];
		for (int i = 0; i < n; i++)
		{
			arr[i] = nextLong();
		}
		return arr;
	}

	private static void pArray(int[] arr)
	{
		for (int i = 0; i < arr.length; i++)
		{
			System.out.print(arr[i] + " ");
		}
		System.out.println();
		return;
	}

	private static void pArray(long[] arr)
	{
		for (int i = 0; i < arr.length; i++)
		{
			System.out.print(arr[i] + " ");
		}
		System.out.println();
		return;
	}

	private static boolean isSpaceChar(int c)
	{
		if (filter != null)
			return filter.isSpaceChar(c);
		return isWhitespace(c);
	}

	private static char nextChar()
	{
		int c = read();
		while (isSpaceChar(c))
			c = read();
		char c1 = (char) c;
		while (!isSpaceChar(c))
			c = read();
		return c1;
	}

	private interface SpaceCharFilter
	{
		public boolean isSpaceChar(int ch);
	}
}