import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Program {

  public void solve() {

    int n = nextInt();
    long[] a = new long[n];
    for (int i = 0; i < n; i++) {
      a[i] = nextLong();
    }

    long[] pref = new long[n];
    long[] preff = new long[n];
    long[] suff = new long[n];
    long[] sufff = new long[n];

    for (int i = 1; i < n; i++) {
      if (a[i - 1] + preff[i - 1] >= a[i]) {
        preff[i] = a[i - 1] + preff[i - 1] - a[i] + 1;
      }
      pref[i] += pref[i - 1] + preff[i];
    }
    for (int i = n - 2; i >= 0; i--) {
      if (a[i + 1] + sufff[i + 1] >= a[i]) {
        sufff[i] = a[i + 1] + sufff[i + 1] - a[i] + 1;
      }
      suff[i] += suff[i + 1] + sufff[i];
    }

    long ans = Math.min(pref[n - 1], suff[0]);

    for (int i = 0; i < n - 1; i++) {
      long cur = pref[i] + suff[i + 1];
      if (a[i] + preff[i] == a[i + 1] + sufff[i + 1]) {
        cur++;
      }
      ans = Math.min(ans, cur);
    }

    out.println(ans);
  }

  public static void main(String[] args) {
    new Program().run();
  }

  public void run() {
    try {
      init();
      solve();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void init() {
    in = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out);
  }

  BufferedReader in;
  PrintWriter out;
  StringTokenizer tok = new StringTokenizer("");

  String nextString() {
    while (!tok.hasMoreTokens()) {
      try {
        tok = new StringTokenizer(in.readLine());
      } catch (Exception e) {
        return null;
      }
    }
    return tok.nextToken();
  }

  int nextInt() {
    return Integer.parseInt(nextString());
  }

  long nextLong() {
    return Long.parseLong(nextString());
  }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Program {

  public void solve() {

    int n = nextInt();
    long[] a = new long[n];
    for (int i = 0; i < n; i++) {
      a[i] = nextLong();
    }

    long[] pref = new long[n];
    long[] preff = new long[n];
    long[] suff = new long[n];
    long[] sufff = new long[n];

    for (int i = 1; i < n; i++) {
      if (a[i - 1] + preff[i - 1] >= a[i]) {
        preff[i] = a[i - 1] + preff[i - 1] - a[i] + 1;
      }
      pref[i] += pref[i - 1] + preff[i];
    }
    for (int i = n - 2; i >= 0; i--) {
      if (a[i + 1] + sufff[i + 1] >= a[i]) {
        sufff[i] = a[i + 1] + sufff[i + 1] - a[i] + 1;
      }
      suff[i] += suff[i + 1] + sufff[i];
    }

    long ans = Math.min(pref[n - 1], suff[0]);

    for (int i = 0; i < n - 1; i++) {
      long cur = pref[i] + suff[i + 1];
      if (a[i] + pref[i] == a[i + 1] + suff[i + 1]) {
        cur++;
      }
      ans = Math.min(ans, cur);
    }

    out.println(ans);
  }

  public static void main(String[] args) {
    new Program().run();
  }

  public void run() {
    try {
      init();
      solve();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void init() {
    in = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out);
  }

  BufferedReader in;
  PrintWriter out;
  StringTokenizer tok = new StringTokenizer("");

  String nextString() {
    while (!tok.hasMoreTokens()) {
      try {
        tok = new StringTokenizer(in.readLine());
      } catch (Exception e) {
        return null;
      }
    }
    return tok.nextToken();
  }

  int nextInt() {
    return Integer.parseInt(nextString());
  }

  long nextLong() {
    return Long.parseLong(nextString());
  }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Program {

  public void solve() {

    int n = nextInt();
    long[] a = new long[n];
    for (int i = 0; i < n; i++) {
      a[i] = nextLong();
    }

    long[] pref = new long[n];
    long[] suff = new long[n];

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

    long ans = Math.min(pref[n - 1], suff[0]);

    for (int i = 0; i < n - 1; i++) {
      long cur = pref[i] + suff[i + 1];
      if (a[i] + pref[i] == a[i + 1] + suff[i + 1]) {
        cur++;
      }
      ans = Math.min(ans, cur);
    }

    out.println(ans);
  }

  public static void main(String[] args) {
    new Program().run();
  }

  public void run() {
    try {
      init();
      solve();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void init() {
    in = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(System.out);
  }

  BufferedReader in;
  PrintWriter out;
  StringTokenizer tok = new StringTokenizer("");

  String nextString() {
    while (!tok.hasMoreTokens()) {
      try {
        tok = new StringTokenizer(in.readLine());
      } catch (Exception e) {
        return null;
      }
    }
    return tok.nextToken();
  }

  int nextInt() {
    return Integer.parseInt(nextString());
  }

  long nextLong() {
    return Long.parseLong(nextString());
  }
}


import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
	private static BufferedReader br;
	private static StringTokenizer st;
	private static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			long[] l = new long[n];
			for(int i = 0; i < n; i++) {
				l[i] = readLong();
			}
			int min = 0;
			int max = n-1;
			while(max-min > 4) {
				int lhs = (2*min+max)/3;
				int rhs = (2*max+min)/3;
				long ll = solve(l, lhs);
				long rr = solve(l, rhs);
				if(ll < rr) {
					max = rhs;
				}
				else {
					min = lhs;
				}
			}
			long ret = Long.MAX_VALUE;
			for(int i = min; i <= max; i++) {
				ret = Math.min(ret, solve(l, i));
			}
			pw.println(ret);
		}
		pw.close();
	}

	public static long solve(long[] l, int index) {
		long ret = 0;
		long first = l[0];
		long top = l[index];
		for(int i = 1; i < index; i++) {
			first = Math.max(first+1, l[i]);
			ret += first - l[i];
		}
		top = Math.max(top, first+1);
		first = l[l.length-1];
		for(int i = l.length-2; i > index; i--) {
			first = Math.max(first+1, l[i]);
			ret += first - l[i];
		}
		top = Math.max(top, first+1);
		ret += top - l[index];
		return ret;
	}

	private static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	private static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	private static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	private static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	private static String nextLine() throws IOException  {
		if(!br.ready()) {
			exitImmediately();
		}
		st = null;
		return br.readLine();
	}

	private static String nextToken() throws IOException  {
		while(st == null || !st.hasMoreTokens())  {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine().trim());
		}
		return st.nextToken();
	}
}


import java.awt.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;

public class Main {
	private static BufferedReader br;
	private static StringTokenizer st;
	private static PrintWriter pw;

	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		//int qq = 1;
		int qq = Integer.MAX_VALUE;
		//int qq = readInt();
		for(int casenum = 1; casenum <= qq; casenum++) {
			int n = readInt();
			long[] l = new long[n];
			for(int i = 0; i < n; i++) {
				l[i] = readLong();
			}
			long[] fromLHS = new long[n];
			long[] atLHS = new long[n];
			atLHS[0] = l[0];
			for(int i = 1; i < n; i++) {
				atLHS[i] = Math.max(atLHS[i-1] + 1, l[i]);
				fromLHS[i] = fromLHS[i-1] + atLHS[i] - l[i];
			}
			long[] fromRHS = new long[n];
			long[] atRHS = new long[n];
			atRHS[n-1] = l[n-1];
			for(int i = n-2; i >= 0; i--) {
				atRHS[i] = Math.max(atRHS[i+1] + 1, l[i]);
				fromRHS[i] = fromRHS[i+1] + atRHS[i] - l[i];
			}
			long ret = Math.min(fromRHS[0], fromLHS[n-1]);
			for(int i = 1; i < n-1; i++) {
				ret = Math.min(ret, fromLHS[i-1] + fromRHS[i+1] + Math.max(atLHS[i], atRHS[i]) - l[i]);
			}
			pw.println(ret);
		}
		pw.close();
	}

	private static void exitImmediately() {
		pw.close();
		System.exit(0);
	}

	private static long readLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	private static double readDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	private static int readInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	private static String nextLine() throws IOException  {
		if(!br.ready()) {
			exitImmediately();
		}
		st = null;
		return br.readLine();
	}

	private static String nextToken() throws IOException  {
		while(st == null || !st.hasMoreTokens())  {
			if(!br.ready()) {
				exitImmediately();
			}
			st = new StringTokenizer(br.readLine().trim());
		}
		return st.nextToken();
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Sanket Makani
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskH solver = new TaskH();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskH {
        public void solve(int testNumber, InputReader sc, PrintWriter w) {

            int n = sc.nextInt();

            int arr[] = new int[n + 2];
            arr[0] = 0;
            arr[n + 1] = 0;

            int start = 0;
            int end = n;

            for (int i = 1; i <= n; i++)
                arr[i] = sc.nextInt();

            for (int i = 1; i <= n; i++) {
                if (arr[i] > arr[i - 1])
                    start = i;
                else
                    break;
            }

            for (int i = n; i > 0; i--) {
                if (arr[i] > arr[i + 1])
                    end = i;
                else
                    break;
            }

            if (start >= end)
                w.println("0");
            else {
                long forward[] = new long[n + 2];
                long prev[] = new long[n + 2];
                long next[] = new long[n + 2];
                long reverse[] = new long[n + 2];

                for (int i = 1; i <= n; i++) {
                    prev[i] = prev[i - 1] + 1;
                    if (prev[i] < arr[i]) {
                        prev[i] = arr[i];
                        forward[i] += forward[i - 1];
                    } else {
                        forward[i] += forward[i - 1] + (prev[i] - arr[i]);
                    }
                }

                for (int i = n; i > 0; i--) {
                    next[i] = next[i + 1] + 1;
                    if (next[i] < arr[i]) {
                        next[i] = arr[i];
                        reverse[i] += reverse[i + 1];
                    } else {
                        reverse[i] += reverse[i + 1] + next[i] - arr[i];
                    }
                }

//            w.println(Arrays.toString(forward));
//            w.println(Arrays.toString(reverse));
//            w.println(Arrays.toString(prev));
//            w.println(Arrays.toString(next));

                long ans = Long.MAX_VALUE;

                for (int i = 1; i <= n; i++) {
                    if (prev[i] != next[i + 1])
                        ans = Math.min(ans, forward[i] + reverse[i + 1]);
                }

                w.println(ans);
            }
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public 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++];
        }

        public 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;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

        }

    }
}


import java.io.*;
import java.util.*;
import java.util.stream.IntStream;

public class Solution {

    static MyScanner sc;
    private static PrintWriter out;
    static long M = 1000000007;

    public static void main(String[] s) throws Exception {
        StringBuilder stringBuilder = new StringBuilder();
//
//        stringBuilder.append("200000 200000 ");
//        Random x = new Random(3);
//
//        for (int i = 0; i < 400000; i++) {
//            stringBuilder.append(x.nextInt() + " ");
//        }

        if (stringBuilder.length() == 0) {
            sc = new MyScanner(System.in);
        } else {
            sc = new MyScanner(new BufferedReader(new StringReader(stringBuilder.toString())));
        }

        out = new PrintWriter(new OutputStreamWriter(System.out));

        initData();
        solve();
        out.flush();
    }

    private static void initData() {
    }

    private static void solve() throws IOException {
        int n = sc.nextInt();

        long[] k = sc.nl(n);

        if (n == 1) {
            out.println(0);
            return;
        }

        long[][] t1 = new long[n][2];
        long[][] t2 = new long[n][2];
        t2[n - 1][0] = k[n - 1];

        t1[0][0] = k[0];

        for (int l = 1; l < n; l++) {
            t1[l][1] = t1[l - 1][1];
            if (t1[l - 1][0] >= k[l]) {
                t1[l][0] = t1[l - 1][0] + 1;
                t1[l][1] += t1[l][0] - k[l];
            } else {
                t1[l][0] = k[l];
            }
        }
        for (int l = n - 2; l >= 0; l--) {
            t2[l][1] = t2[l + 1][1];
            if (t2[l + 1][0] >= k[l]) {
                t2[l][0] = t2[l + 1][0] + 1;
                t2[l][1] += t2[l][0] - k[l];
            } else {
                t2[l][0] = k[l];
            }
        }

        long min = Long.MAX_VALUE;
        for (int r = 0; r < n; r++) {
            long cur = 0;
            long left = r > 0 ? t1[r - 1][0] : Long.MIN_VALUE;
            long right = (r < n - 1) ? t2[r + 1][0] : Long.MIN_VALUE;
            cur += (r > 0) ? (t1[r - 1][1]) : 0;
            cur += (r < n - 1) ? (t2[r + 1][1]) : 0;
            long xx = Math.max(left, right);

            if (xx >= k[r]) {
                cur += xx - k[r] + 1;
            }

            min = Math.min(min, cur);
        }
        out.println(min);

    }

    private static void solveT() throws IOException {
        int t = sc.nextInt();
        while (t-- > 0) {
            solve();
        }
    }

    private static long gcd(long l, long l1) {
        if (l > l1) return gcd(l1, l);
        if (l == 0) return l1;
        return gcd(l1 % l, l);
    }

    private static long pow(long a, long b, long m) {
        if (b == 0) return 1;
        if (b == 1) return a;
        long pp = pow(a, b / 2, m);
        pp *= pp;
        pp %= m;
        return (pp * (b % 2 == 0 ? 1 : a)) % m;
    }

    static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        MyScanner(BufferedReader br) {
            this.br = br;
        }

        public MyScanner(InputStream in) {
            this(new BufferedReader(new InputStreamReader(in)));
        }

        void findToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        String next() {
            findToken();
            return st.nextToken();
        }

        Integer[] nab(int n) {
            Integer[] k = new Integer[n];
            for (int i = 0; i < n; i++) {
                k[i] = sc.fi();
            }
            return k;
        }

        int[] na(int n) {
            int[] k = new int[n];
            for (int i = 0; i < n; i++) {
                k[i] = sc.fi();
            }
            return k;
        }

        long[] nl(int n) {
            long[] k = new long[n];
            for (int i = 0; i < n; i++) {
                k[i] = sc.nextLong();
            }
            return k;
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int fi() {
            String t = next();
            int cur = 0;
            boolean n = t.charAt(0) == '-';
            for (int a = n ? 1 : 0; a < t.length(); a++) {
                cur = cur * 10 + t.charAt(a) - '0';
            }
            return n ? -cur : cur;
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }
    }

}


import java.util.*;
import java.io.*;
import java.math.*;
public class code7 {
	InputStream is;
	PrintWriter out;
	static long mod=pow(10,9)+7;
	void solve()
	{
		int n=ni();
		int a[]=na(n);
		long prefix[]=new long[n];
		long suffix[]=new long[n];
		prefix[0]=a[0];
		for(int i=1;i<n;i++)
			prefix[i]=Math.max(prefix[i-1]+1,a[i]);
		suffix[n-1]=a[n-1];
		for(int i=n-2;i>=0;i--)
			suffix[i]=Math.max(suffix[i+1]+1,a[i]);
		prefix[0]=0;
		for(int i=1;i<n;i++)
		{
			prefix[i]=prefix[i]-a[i];
			prefix[i]+=prefix[i-1];
		}
		suffix[n-1]=0;
		for(int i=n-2;i>=0;i--)
		{
			suffix[i]=suffix[i]-a[i];
			suffix[i]+=suffix[i+1];
		}
		long min=suffix[0];
		for(int i=1;i<n;i++)
			min=Math.min(min,prefix[i-1]+suffix[i]);
		out.println(min);
	}
	public static int count(int x)
	{
		int num=0;
		while(x!=0)
		{
			x=x&(x-1);
			num++;
		}
		return num;
	}
	static long d, x, y;
	void extendedEuclid(long A, long B) {
	    if(B == 0) {
	        d = A;
	        x = 1;
	        y = 0;
	    }
	    else {
	        extendedEuclid(B, A%B);
	        long temp = x;
	        x = y;
	        y = temp - (A/B)*y;
	    }
	}
	long modInverse(long A,long M) //A and M are coprime
	{
	    extendedEuclid(A,M);
	    return (x%M+M)%M;    //x may be negative
	}
	public static void mergeSort(int[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(int arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		int left[] = new int[n1];
		int right[] = new int[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	public static void mergeSort(long[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(long arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		long left[] = new long[n1];
		long right[] = new long[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	 static class Pair implements Comparable<Pair>{

	       int x,y,k,i;
	        int xx,yy;
		Pair (int x,int y){
			this.x=x;
			this.y=y;

		}

		public int compareTo(Pair o) {
			if(this.x>=o.x)
				return this.x-o.x;
			else
				return this.y-o.y;
		}

	        public boolean equals(Object o) {
	            if (o instanceof Pair) {
	                Pair p = (Pair)o;
	                return p.x == x && p.y == y;
	            }
	            return false;
	        }

	        @Override
	        public String toString() {
	            return  "("+x + " " + y +" "+k+" "+i+" )";
	        }

	    }

	    public static boolean isPal(String s){
	        for(int i=0, j=s.length()-1;i<=j;i++,j--){
	                if(s.charAt(i)!=s.charAt(j)) return false;
	        }
	        return true;
	    }
	    public static String rev(String s){
			StringBuilder sb=new StringBuilder(s);
			sb.reverse();
			return sb.toString();
	    }

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

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

	    public static long gcdExtended(long a,long b,long[] x){

	        if(a==0){
	            x[0]=0;
	            x[1]=1;
	            return b;
	        }
	        long[] y=new long[2];
	        long gcd=gcdExtended(b%a, a, y);

	        x[0]=y[1]-(b/a)*y[0];
	        x[1]=y[0];

	        return gcd;
	    }

	    public static int abs(int a,int b){
		return (int)Math.abs(a-b);
	    }

	    public static long abs(long a,long b){
		return (long)Math.abs(a-b);
	    }

	    public static int max(int a,int b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static int min(int a,int b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long max(long a,long b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static long min(long a,long b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long pow(long n,long p,long m){
		 long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    if(result>=m)
		    result%=m;
		    p >>=1;
		    n*=n;
		    if(n>=m)
		    n%=m;
		}
		return result;
	    }

	    public static long pow(long n,long p){
		long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    p >>=1;
		    n*=n;
		}
		return result;
	    }
	    public static void debug(Object... o) {
			System.out.println(Arrays.deepToString(o));
		}
	    void run() throws Exception {
			is = System.in;
			out = new PrintWriter(System.out);
			solve();
			out.flush();
		}

	    public static void main(String[] args) throws Exception {
			new Thread(null, new Runnable() {
				public void run() {
					try {
						new code7().run();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}, "1", 1 << 26).start();

		}
	    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 char[][] nm(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = ns(m);
			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();
			}
		}

}


import java.util.*;
import java.io.*;
import java.math.*;
public class code7 {
	InputStream is;
	PrintWriter out;
	static long mod=pow(10,9)+7;
	void solve()
	{
		int n=ni();
		int a[]=na(n);
		long prefix[]=new long[n];
		long suffix[]=new long[n];
		long p[]=new long[n];
		long s[]=new long[n];
		prefix[0]=a[0];
		for(int i=1;i<n;i++)
			prefix[i]=Math.max(prefix[i-1]+1,a[i]);
		suffix[n-1]=a[n-1];
		for(int i=n-2;i>=0;i--)
			suffix[i]=Math.max(suffix[i+1]+1,a[i]);
		p=prefix.clone();
		s=suffix.clone();
		prefix[0]=0;
		for(int i=1;i<n;i++)
		{
			prefix[i]=prefix[i]-a[i];
			prefix[i]+=prefix[i-1];
		}
		suffix[n-1]=0;
		for(int i=n-2;i>=0;i--)
		{
			suffix[i]=suffix[i]-a[i];
			suffix[i]+=suffix[i+1];
		}
		long min=Math.min(suffix[0],prefix[n-1]);
		for(int i=1;i<n-1;i++)
		{
			min=Math.min(min,prefix[i-1]+suffix[i+1]+Math.max(p[i],s[i])-a[i]);
		}
		out.println(min);
	}
	public static int count(int x)
	{
		int num=0;
		while(x!=0)
		{
			x=x&(x-1);
			num++;
		}
		return num;
	}
	static long d, x, y;
	void extendedEuclid(long A, long B) {
	    if(B == 0) {
	        d = A;
	        x = 1;
	        y = 0;
	    }
	    else {
	        extendedEuclid(B, A%B);
	        long temp = x;
	        x = y;
	        y = temp - (A/B)*y;
	    }
	}
	long modInverse(long A,long M) //A and M are coprime
	{
	    extendedEuclid(A,M);
	    return (x%M+M)%M;    //x may be negative
	}
	public static void mergeSort(int[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(int arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		int left[] = new int[n1];
		int right[] = new int[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	public static void mergeSort(long[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(long arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		long left[] = new long[n1];
		long right[] = new long[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	 static class Pair implements Comparable<Pair>{

	       int x,y,k,i;
	        int xx,yy;
		Pair (int x,int y){
			this.x=x;
			this.y=y;

		}

		public int compareTo(Pair o) {
			if(this.x>=o.x)
				return this.x-o.x;
			else
				return this.y-o.y;
		}

	        public boolean equals(Object o) {
	            if (o instanceof Pair) {
	                Pair p = (Pair)o;
	                return p.x == x && p.y == y;
	            }
	            return false;
	        }

	        @Override
	        public String toString() {
	            return  "("+x + " " + y +" "+k+" "+i+" )";
	        }

	    }

	    public static boolean isPal(String s){
	        for(int i=0, j=s.length()-1;i<=j;i++,j--){
	                if(s.charAt(i)!=s.charAt(j)) return false;
	        }
	        return true;
	    }
	    public static String rev(String s){
			StringBuilder sb=new StringBuilder(s);
			sb.reverse();
			return sb.toString();
	    }

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

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

	    public static long gcdExtended(long a,long b,long[] x){

	        if(a==0){
	            x[0]=0;
	            x[1]=1;
	            return b;
	        }
	        long[] y=new long[2];
	        long gcd=gcdExtended(b%a, a, y);

	        x[0]=y[1]-(b/a)*y[0];
	        x[1]=y[0];

	        return gcd;
	    }

	    public static int abs(int a,int b){
		return (int)Math.abs(a-b);
	    }

	    public static long abs(long a,long b){
		return (long)Math.abs(a-b);
	    }

	    public static int max(int a,int b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static int min(int a,int b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long max(long a,long b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static long min(long a,long b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long pow(long n,long p,long m){
		 long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    if(result>=m)
		    result%=m;
		    p >>=1;
		    n*=n;
		    if(n>=m)
		    n%=m;
		}
		return result;
	    }

	    public static long pow(long n,long p){
		long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    p >>=1;
		    n*=n;
		}
		return result;
	    }
	    public static void debug(Object... o) {
			System.out.println(Arrays.deepToString(o));
		}
	    void run() throws Exception {
			is = System.in;
			out = new PrintWriter(System.out);
			solve();
			out.flush();
		}

	    public static void main(String[] args) throws Exception {
			new Thread(null, new Runnable() {
				public void run() {
					try {
						new code7().run();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}, "1", 1 << 26).start();

		}
	    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 char[][] nm(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = ns(m);
			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();
			}
		}

}


import java.util.*;
import java.io.*;
import java.math.*;
public class code7 {
	InputStream is;
	PrintWriter out;
	static long mod=pow(10,9)+7;
	void solve()
	{
		int n=ni();
		int a[]=na(n);
		long prefix[]=new long[n];
		long suffix[]=new long[n];
		prefix[0]=a[0];
		for(int i=1;i<n;i++)
			prefix[i]=Math.max(prefix[i-1]+1,a[i]);
		suffix[n-1]=a[n-1];
		for(int i=n-2;i>=0;i--)
			suffix[i]=Math.max(suffix[i+1]+1,a[i]);
		prefix[0]=0;
		for(int i=1;i<n;i++)
		{
			prefix[i]=prefix[i]-a[i];
			prefix[i]+=prefix[i-1];
		}
		suffix[n-1]=0;
		for(int i=n-2;i>=0;i--)
		{
			suffix[i]=suffix[i]-a[i];
			suffix[i]+=suffix[i+1];
		}
		long min=suffix[0];
		for(int i=1;i<n;i++)
			min=Math.min(min,prefix[i-1]+suffix[i]);
		min=Math.min(min,prefix[n-1]);
		out.println(min);
	}
	public static int count(int x)
	{
		int num=0;
		while(x!=0)
		{
			x=x&(x-1);
			num++;
		}
		return num;
	}
	static long d, x, y;
	void extendedEuclid(long A, long B) {
	    if(B == 0) {
	        d = A;
	        x = 1;
	        y = 0;
	    }
	    else {
	        extendedEuclid(B, A%B);
	        long temp = x;
	        x = y;
	        y = temp - (A/B)*y;
	    }
	}
	long modInverse(long A,long M) //A and M are coprime
	{
	    extendedEuclid(A,M);
	    return (x%M+M)%M;    //x may be negative
	}
	public static void mergeSort(int[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(int arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		int left[] = new int[n1];
		int right[] = new int[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	public static void mergeSort(long[] arr, int l ,int r){
		if((r-l)>=1){
			int mid = (l+r)/2;
			mergeSort(arr,l,mid);
			mergeSort(arr,mid+1,r);
			merge(arr,l,r,mid);
		}
	}
	public static void merge(long arr[], int l, int r, int mid){
		int n1 = (mid-l+1), n2 = (r-mid);
		long left[] = new long[n1];
		long right[] = new long[n2];
		for(int i =0 ;i<n1;i++) left[i] = arr[l+i];
		for(int i =0 ;i<n2;i++) right[i] = arr[mid+1+i];
		int i =0, j =0, k = l;
		while(i<n1 && j<n2){
			if(left[i]>right[j]){
				arr[k++] = right[j++];
			}
			else{
				arr[k++] = left[i++];
			}
		}
		while(i<n1) arr[k++] = left[i++];
		while(j<n2) arr[k++] = right[j++];
	}
	 static class Pair implements Comparable<Pair>{

	       int x,y,k,i;
	        int xx,yy;
		Pair (int x,int y){
			this.x=x;
			this.y=y;

		}

		public int compareTo(Pair o) {
			if(this.x>=o.x)
				return this.x-o.x;
			else
				return this.y-o.y;
		}

	        public boolean equals(Object o) {
	            if (o instanceof Pair) {
	                Pair p = (Pair)o;
	                return p.x == x && p.y == y;
	            }
	            return false;
	        }

	        @Override
	        public String toString() {
	            return  "("+x + " " + y +" "+k+" "+i+" )";
	        }

	    }

	    public static boolean isPal(String s){
	        for(int i=0, j=s.length()-1;i<=j;i++,j--){
	                if(s.charAt(i)!=s.charAt(j)) return false;
	        }
	        return true;
	    }
	    public static String rev(String s){
			StringBuilder sb=new StringBuilder(s);
			sb.reverse();
			return sb.toString();
	    }

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

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

	    public static long gcdExtended(long a,long b,long[] x){

	        if(a==0){
	            x[0]=0;
	            x[1]=1;
	            return b;
	        }
	        long[] y=new long[2];
	        long gcd=gcdExtended(b%a, a, y);

	        x[0]=y[1]-(b/a)*y[0];
	        x[1]=y[0];

	        return gcd;
	    }

	    public static int abs(int a,int b){
		return (int)Math.abs(a-b);
	    }

	    public static long abs(long a,long b){
		return (long)Math.abs(a-b);
	    }

	    public static int max(int a,int b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static int min(int a,int b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long max(long a,long b){
		if(a>b)
			return a;
		else
			return b;
	    }

	    public static long min(long a,long b){
		if(a>b)
			return b;
		else
			return a;
	    }

	    public static long pow(long n,long p,long m){
		 long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    if(result>=m)
		    result%=m;
		    p >>=1;
		    n*=n;
		    if(n>=m)
		    n%=m;
		}
		return result;
	    }

	    public static long pow(long n,long p){
		long  result = 1;
		  if(p==0)
		    return 1;
		if (p==1)
		    return n;
		while(p!=0)
		{
		    if(p%2==1)
		        result *= n;
		    p >>=1;
		    n*=n;
		}
		return result;
	    }
	    public static void debug(Object... o) {
			System.out.println(Arrays.deepToString(o));
		}
	    void run() throws Exception {
			is = System.in;
			out = new PrintWriter(System.out);
			solve();
			out.flush();
		}

	    public static void main(String[] args) throws Exception {
			new Thread(null, new Runnable() {
				public void run() {
					try {
						new code7().run();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}, "1", 1 << 26).start();

		}
	    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 char[][] nm(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = ns(m);
			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();
			}
		}

}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author ssaxena36
 * Solution is at the Top
 */
public class NewClass {
    void solve(){
        int n = inpi();
        long arr[] = new long[n];
        long Al[] = new long[n];
        long Bl[] = new long[n];
        for(int i = 0; i < n; i++){
            arr[i] = inpl();
            Al[i] = arr[i];
            Bl[i] = arr[i];
        }
        long ans = 0L;
        for(int i = 1; i < n; i++){
            if(Al[i] <= Al[i - 1]){
                Al[i] = Al[i - 1] + 1;
            }
        }

        for(int i = n - 1; i > 0; i--){
            if(Bl[i] >= Bl[i - 1]){
                Bl[i - 1] = Bl[i] + 1;
            }
        }
        long All[] = new long[n];
        long Bll[] = new long[n];
       // System.out.print("Init\n");
        for(int i = 0; i < n; i++){
            long fin = Math.min(Al[i], Bl[i]);
           // System.out.print(fin+" ");
            All[i] = fin;
            Bll[i] = fin;
        }
        for(int i = 1; i < n; i++){
            if(All[i] == All[i - 1]){
                All[i] = All[i - 1] + 1;
            }
        }

        for(int i = n - 1; i > 0; i--){
            if(Bll[i] == Bll[i - 1]){
                Bll[i - 1] = Bll[i] + 1;
            }
        }
        //System.out.println(Arrays.toString(All)+"\n"+Arrays.toString(Bll));
        long ansL = ans, ansR = ans;
        for(int i = 0; i < n; i++){
            ansL += All[i] - arr[i];
        }
        for(int i = 0; i < n; i++){
            ansR += Bll[i] - arr[i];
        }
        out.println(Math.min(ansL, ansR));
    }

   public boolean isPrime(long n) {
        return BigInteger.valueOf(n).isProbablePrime(5);
    }

    public void SieveOfEratosthenes(int n) {
        boolean prime[] = new boolean[n + 1];
        Arrays.fill(prime, true);
        for (int p = 2; p * p <= n; p++) {
            if (prime[p] == true) {
                for (int i = p * 2; i <= n; i += p) {
                    prime[i] = false;
                }
            }
        }
        // Print all prime numbers
        for (int p = 2; p <= n; p++) {
            if (prime[p]) {
                //  System.out.print(p+" ");
               // primes[p] = true;
            }
        }
        // return list;
    }
    InputStream obj;
    PrintWriter out;
    String check = "";

    public static void main(String[] args) throws IOException {
        new Thread(null, new Runnable() {
        public void run() {
            try {
                new NewClass().main1();
            } catch (IOException ex) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }, "child-thread", 1 << 26).start();

    }

    void main1() throws IOException {
        out = new PrintWriter(System.out);
        obj = check.isEmpty() ? System.in : new ByteArrayInputStream(check.getBytes());
        solve();
        out.flush();
        out.close();
    }
    byte inbuffer[] = new byte[1024];
    int lenbuffer = 0, ptrbuffer = 0;

    int readByte() {
        if (lenbuffer == -1) {
            throw new InputMismatchException();
        }
        if (ptrbuffer >= lenbuffer) {
            ptrbuffer = 0;
            try {
                lenbuffer = obj.read(inbuffer);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
        }
        if (lenbuffer <= 0) {
            return -1;
        }
        return inbuffer[ptrbuffer++];
    }

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

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

    String inps() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    int inpi() {
        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();
        }
    }

    long inpl() {
        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();
        }
    }

    float inpf() {
        return Float.parseFloat(inps());
    }

    double inpd() {
        return Double.parseDouble(inps());
    }

    char inpc() {
        return (char) skip();
    }

    int[] inpia(int n) {
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = inpi();
        }
        return a;
    }

    long[] inpla(int n) {
        long a[] = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = inpl();
        }
        return a;
    }

    String[] inpsa(int n) {
        String a[] = new String[n];
        for (int i = 0; i < n; i++) {
            a[i] = inps();
        }
        return a;
    }

    double[][] inpdm(int n, int m) {
        double a[][] = new double[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = inpd();
            }
        }
        return a;
    }

    int[][] inpim(int n, int m) {
        int a[][] = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = inpi();
            }
        }
        return a;
    }
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author ssaxena36
 * Solution is at the Top
 */
public class NewClass {
    void solve(){
        int n = inpi();
        long arr[] = new long[n];
        long Al[] = new long[n];
        long Bl[] = new long[n];
        for(int i = 0; i < n; i++){
            arr[i] = inpl();
            Al[i] = arr[i];
            Bl[i] = arr[i];
        }
        long ans = 0L;
        for(int i = 1; i < n; i++){
            if(Al[i] <= Al[i - 1]){
                Al[i] = Al[i - 1] + 1;
            }
        }

        for(int i = n - 1; i > 0; i--){
            if(Bl[i] >= Bl[i - 1]){
                Bl[i - 1] = Bl[i] + 1;
            }
        }
        for(int i = 0; i < n; i++){
            ans += Math.min(Bl[i], Al[i]) - arr[i];
        }
        if(n % 2 == 0){
            if(arr[n/2] == arr[n/2 - 1]){
                ans += 1;
            }
        }
        out.println(ans);
    }

   public boolean isPrime(long n) {
        return BigInteger.valueOf(n).isProbablePrime(5);
    }

    public void SieveOfEratosthenes(int n) {
        boolean prime[] = new boolean[n + 1];
        Arrays.fill(prime, true);
        for (int p = 2; p * p <= n; p++) {
            if (prime[p] == true) {
                for (int i = p * 2; i <= n; i += p) {
                    prime[i] = false;
                }
            }
        }
        // Print all prime numbers
        for (int p = 2; p <= n; p++) {
            if (prime[p]) {
                //  System.out.print(p+" ");
               // primes[p] = true;
            }
        }
        // return list;
    }
    InputStream obj;
    PrintWriter out;
    String check = "";

    public static void main(String[] args) throws IOException {
        new Thread(null, new Runnable() {
        public void run() {
            try {
                new NewClass().main1();
            } catch (IOException ex) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }, "child-thread", 1 << 26).start();

    }

    void main1() throws IOException {
        out = new PrintWriter(System.out);
        obj = check.isEmpty() ? System.in : new ByteArrayInputStream(check.getBytes());
        solve();
        out.flush();
        out.close();
    }
    byte inbuffer[] = new byte[1024];
    int lenbuffer = 0, ptrbuffer = 0;

    int readByte() {
        if (lenbuffer == -1) {
            throw new InputMismatchException();
        }
        if (ptrbuffer >= lenbuffer) {
            ptrbuffer = 0;
            try {
                lenbuffer = obj.read(inbuffer);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
        }
        if (lenbuffer <= 0) {
            return -1;
        }
        return inbuffer[ptrbuffer++];
    }

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

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

    String inps() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    int inpi() {
        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();
        }
    }

    long inpl() {
        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();
        }
    }

    float inpf() {
        return Float.parseFloat(inps());
    }

    double inpd() {
        return Double.parseDouble(inps());
    }

    char inpc() {
        return (char) skip();
    }

    int[] inpia(int n) {
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = inpi();
        }
        return a;
    }

    long[] inpla(int n) {
        long a[] = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = inpl();
        }
        return a;
    }

    String[] inpsa(int n) {
        String a[] = new String[n];
        for (int i = 0; i < n; i++) {
            a[i] = inps();
        }
        return a;
    }

    double[][] inpdm(int n, int m) {
        double a[][] = new double[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = inpd();
            }
        }
        return a;
    }

    int[][] inpim(int n, int m) {
        int a[][] = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = inpi();
            }
        }
        return a;
    }
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author ssaxena36
 * Solution is at the Top
 */
public class NewClass {
    void solve(){
        int n = inpi();
        long arr[] = new long[n];
        long Al[] = new long[n];
        long Bl[] = new long[n];
        for(int i = 0; i < n; i++){
            arr[i] = inpi();
            Al[i] = arr[i];
            Bl[i] = arr[i];
        }
        long ans = 0L;
        for(int i = 1; i < n; i++){
            if(Al[i] <= Al[i - 1]){
                Al[i] = Al[i - 1] + 1;
            }
        }

        for(int i = n - 1; i > 0; i--){
            if(Bl[i] >= Bl[i - 1]){
                Bl[i - 1] = Bl[i] + 1;
            }
        }
        for(int i = 0; i < n; i++){
            ans += Math.min(Bl[i], Al[i]) - arr[i];
        }
        out.println(ans);
    }

   public boolean isPrime(long n) {
        return BigInteger.valueOf(n).isProbablePrime(5);
    }

    public void SieveOfEratosthenes(int n) {
        boolean prime[] = new boolean[n + 1];
        Arrays.fill(prime, true);
        for (int p = 2; p * p <= n; p++) {
            if (prime[p] == true) {
                for (int i = p * 2; i <= n; i += p) {
                    prime[i] = false;
                }
            }
        }
        // Print all prime numbers
        for (int p = 2; p <= n; p++) {
            if (prime[p]) {
                //  System.out.print(p+" ");
               // primes[p] = true;
            }
        }
        // return list;
    }
    InputStream obj;
    PrintWriter out;
    String check = "";

    public static void main(String[] args) throws IOException {
        new Thread(null, new Runnable() {
        public void run() {
            try {
                new NewClass().main1();
            } catch (IOException ex) {
                Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }, "child-thread", 1 << 26).start();

    }

    void main1() throws IOException {
        out = new PrintWriter(System.out);
        obj = check.isEmpty() ? System.in : new ByteArrayInputStream(check.getBytes());
        solve();
        out.flush();
        out.close();
    }
    byte inbuffer[] = new byte[1024];
    int lenbuffer = 0, ptrbuffer = 0;

    int readByte() {
        if (lenbuffer == -1) {
            throw new InputMismatchException();
        }
        if (ptrbuffer >= lenbuffer) {
            ptrbuffer = 0;
            try {
                lenbuffer = obj.read(inbuffer);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
        }
        if (lenbuffer <= 0) {
            return -1;
        }
        return inbuffer[ptrbuffer++];
    }

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

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

    String inps() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    int inpi() {
        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();
        }
    }

    long inpl() {
        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();
        }
    }

    float inpf() {
        return Float.parseFloat(inps());
    }

    double inpd() {
        return Double.parseDouble(inps());
    }

    char inpc() {
        return (char) skip();
    }

    int[] inpia(int n) {
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = inpi();
        }
        return a;
    }

    long[] inpla(int n) {
        long a[] = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = inpl();
        }
        return a;
    }

    String[] inpsa(int n) {
        String a[] = new String[n];
        for (int i = 0; i < n; i++) {
            a[i] = inps();
        }
        return a;
    }

    double[][] inpdm(int n, int m) {
        double a[][] = new double[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = inpd();
            }
        }
        return a;
    }

    int[][] inpim(int n, int m) {
        int a[][] = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                a[i][j] = inpi();
            }
        }
        return a;
    }
}


import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.Random;
import java.util.StringTokenizer;

public class Main {

    final long b = 31;
    String fileName = "";

    //////////////////////    SOLUTION   SOLUTION  SOLUTION    //////////////////////////////
    int INF = Integer.MAX_VALUE/10;
    long MODULO = 1000_1000_100l;
    int MAX_VALUE = 1000_1000;
    ArrayList<Integer>[] graph;

    class Pupil implements Comparable<Pupil>{
        int id, sum;
        Pupil(int id, int sum){
            this.id = id;
            this.sum = sum;
        }

        @Override
        public int compareTo(Pupil o) {
            if (this.sum == o.sum) return this.id - o.id;
            return o.sum - this.sum;
        }
    }
    void solve() throws IOException {
        int n = readInt();
        int[] arr = readIntArray(n);

        long[] suff = new long[n+2];
        long[] pref = new long[n+1];
        int last = arr[0];
        for (int i=2; i<=n; ++i){
            pref[i] = pref[i-1];
            if (arr[i - 1] <= last){
                pref[i] += last + 1 - arr[i-1];
                last = last+1;
            }else{
                last = arr[i-1];
            }
        }
        last = arr[n-1];
        int[] lastVal = new int[n+1];
        lastVal[n] = arr[n-1];
        for (int i=n-1; i>=1; --i){
            suff[i] = suff[i+1];
            if (arr[i - 1] <= last){
                suff[i] += last + 1 - arr[i-1];
                last = last+1;
            }else{
                last = arr[i-1];
            }
            lastVal[i] = last;
        }
        long ans = Long.MAX_VALUE;

        for (int i=0; i<=n; ++i){
            long curAns = pref[i] + suff[i+1];
            if (i > 0 && i<n && arr[i-1] == lastVal[i+1]) curAns++;
            ans = Math.min(ans, curAns);
        }
        out.println(ans);
    }

    class Number implements Comparable<Number>{
        int x, cost;
        Number(int x, int cost){
            this.x = x;
            this.cost = cost;
        }

        @Override
        public int compareTo(Number o) {
            return Integer.compare(this.cost, o.cost);
        }
    }
    class Point{
        int x, y;
        Point(int x, int y){
            this.x = x;
            this.y = y;
        }
    }

    class Vertex implements Comparable<Vertex>{
        int num, depth, e, c;
        Vertex(int num, int depth, int e, int c){
            this.num = num;
            this.e = e;
            this.depth = depth;
            this.c = c;
        }

        @Override
        public int compareTo(Vertex o) {
            return Integer.compare(this.e, o.e);
        }
    }
    ///////////////////////////////////////////////////////////////////////////////////////////

    class Edge{
        int from, to, num;
        Edge(int to, int num){
            this.to = to;
            this.num = num;
        }
    }

    class SparseTable{
        int[][] rmq;
        int[] logTable;
        int n;
        SparseTable(int[] a){
            n = a.length;
            logTable = new int[n+1];
            for(int i = 2; i <= n; ++i){
                logTable[i] = logTable[i >> 1] + 1;
            }
            rmq = new int[logTable[n] + 1][n];
            for(int i=0; i<n; ++i){
                rmq[0][i] = a[i];
            }

            for(int k=1; (1 << k) < n; ++k){
                for(int i=0; i + (1 << k) <= n; ++i){
                    int max1 = rmq[k - 1][i];
                    int max2 = rmq[k-1][i + (1 << (k-1))];
                    rmq[k][i] = Math.max(max1, max2);
                }
            }
        }

        int max(int l, int r){
            int k = logTable[r - l];
            int max1 = rmq[k][l];
            int max2 = rmq[k][r - (1 << k) + 1];
            return Math.max(max1, max2);
        }
    }
    long checkBit(long mask, int bit){
        return (mask >> bit) & 1;
    }
    class Dsu{
        int[] parent;
        int countSets;
        Dsu(int n){
            countSets = n;
            parent = new int[n];
            for(int i=0; i<n; ++i){
                parent[i] = i;
            }
        }
        int findSet(int a){
            if(parent[a] == a) return a;
            parent[a] = findSet(parent[a]);
            return parent[a];
        }
        void unionSets(int a, int b){
            a = findSet(a);
            b = findSet(b);
            if(a!=b){
                countSets--;
                parent[a] = b;
            }
        }
    }
    static int checkBit(int mask, int bit) {
        return (mask >> bit) & 1;
    }
    boolean isLower(char c){
        return c >= 'a' && c <= 'z';
    }

    ////////////////////////////////////////////////////////////

    class SegmentTree{
        int[] t;
        int n;
        SegmentTree(int n){
            t = new int[4*n];
            build(new int[n+1], 1, 1, n);
        }
        void build (int a[], int v, int tl, int tr) {
            if (tl == tr)
                t[v] = a[tl];
            else {
                int tm = (tl + tr) / 2;
                build (a, v*2, tl, tm);
                build (a, v*2+1, tm+1, tr);
            }
        }

        void update (int v, int tl, int tr, int l, int r, int add) {
            if (l > r)
                return;
            if (l == tl && tr == r)
                t[v] += add;
            else {
                int tm = (tl + tr) / 2;
                update (v*2, tl, tm, l, Math.min(r,tm), add);
                update (v*2+1, tm+1, tr, Math.max(l,tm+1), r, add);
            }
        }

        int get (int v, int tl, int tr, int pos) {
            if (tl == tr)
                return t[v];
            int tm = (tl + tr) / 2;
            if (pos <= tm)
                return t[v] + get (v*2, tl, tm, pos);
            else
                return t[v] + get (v*2+1, tm+1, tr, pos);
        }
    }
    class Fenwik {
        long[] t;
        int length;

        Fenwik(int[] a) {
            length = a.length + 100;
            t = new long[length];

            for (int i = 0; i < a.length; ++i) {
                inc(i, a[i]);
            }
        }

        void inc(int ind, int delta) {
            for (; ind < length; ind = ind | (ind + 1)) {
                t[ind] = Math.max(delta, t[ind]);
            }
        }

        long getMax(int r) {
            long sum = 0;
            for (; r >= 0; r = (r & (r + 1)) - 1) {
                sum = Math.max(sum, t[r]);
            }
            return sum;
        }
    }
    int gcd(int a, int b){
        return b == 0 ? a : gcd(b, a%b);
    }
    long gcd(long a, long b){
        return b == 0 ? a : gcd(b, a%b);
    }
    double binPow(double a, int pow){
        if (pow == 0) return 1;
        if (pow % 2 == 1) {
            return a * binPow(a, pow - 1);
        } else {
            double c = binPow(a, pow / 2);
            return c * c;
        }
    }
    long binPow(long a, long b, long m) {
        if (b == 0) {
            return 1;
        }
        if (b % 2 == 1) {
            return ((a % m) * (binPow(a, b - 1, m) % m)) % m;
        } else {
            long c = binPow(a, b / 2, m);
            return (c * c) % m;
        }

    }
    int minInt(int... values) {
        int min = Integer.MAX_VALUE;
        for (int value : values) min = Math.min(min, value);
        return min;
    }

    int maxInt(int... values) {
        int max = Integer.MIN_VALUE;
        for (int value : values) max = Math.max(max, value);
        return max;
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        // TODO Auto-generated method stub
        new Main().run();
    }

    void run() throws NumberFormatException, IOException {
        solve();
        out.close();
    };

    BufferedReader in;
    PrintWriter out;

    StringTokenizer tok;
    String delim = " ";
    Random rnd = new Random();

    Main() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            if (fileName.isEmpty()) {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                in = new BufferedReader(new FileReader(fileName + ".in"));
                out = new PrintWriter(fileName + ".out");
            }

        }
        tok = new StringTokenizer("");
    }

    String readLine() throws IOException {
        return in.readLine();
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            String nextLine = readLine();
            if (null == nextLine) {
                return null;
            }

            tok = new StringTokenizer(nextLine);
        }
        return tok.nextToken();
    }

    int readInt() throws NumberFormatException, IOException {
        return Integer.parseInt(readString());
    }
    byte readByte() throws NumberFormatException, IOException {
        return Byte.parseByte(readString());
    }
    int[] readIntArray (int n) throws NumberFormatException, IOException {
        int[] a = new int[n];
        for(int i=0; i<n; ++i){
            a[i] = readInt();
        }
        return a;
    }

    Integer[] readIntegerArray (int n) throws NumberFormatException, IOException {
        Integer[] a = new Integer[n];
        for(int i=0; i<n; ++i){
            a[i] = readInt();
        }
        return a;
    }

    long readLong() throws NumberFormatException, IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws NumberFormatException, IOException {
        return Double.parseDouble(readString());
    }
}


import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.Random;
import java.util.StringTokenizer;

public class Main {

    final long b = 31;
    String fileName = "";

    //////////////////////    SOLUTION   SOLUTION  SOLUTION    //////////////////////////////
    int INF = Integer.MAX_VALUE/10;
    long MODULO = 1000_1000_100l;
    int MAX_VALUE = 1000_1000;
    ArrayList<Integer>[] graph;

    class Pupil implements Comparable<Pupil>{
        int id, sum;
        Pupil(int id, int sum){
            this.id = id;
            this.sum = sum;
        }

        @Override
        public int compareTo(Pupil o) {
            if (this.sum == o.sum) return this.id - o.id;
            return o.sum - this.sum;
        }
    }
    void solve() throws IOException {
        int n = readInt();
        int[] arr = readIntArray(n);

        long[] suff = new long[n+2];
        long[] pref = new long[n+1];
        int[] lastPref = new int[n+2];
        int last = arr[0];
        lastPref[1] = arr[0];
        for (int i=2; i<=n; ++i){
            pref[i] = pref[i-1];
            if (arr[i - 1] <= last){
                pref[i] += last + 1 - arr[i-1];
                last = last+1;
            }else{
                last = arr[i-1];
            }
            lastPref[i] = last;
        }
        last = arr[n-1];
        int[] lastVal = new int[n+2];
        lastVal[n] = arr[n-1];
        for (int i=n-1; i>=1; --i){
            suff[i] = suff[i+1];
            if (arr[i - 1] <= last){
                suff[i] += last + 1 - arr[i-1];
                last = last+1;
            }else{
                last = arr[i-1];
            }
            lastVal[i] = last;
        }
        long ans = Long.MAX_VALUE;

        for (int i=0; i<=n; ++i){
            long curAns = pref[i] + suff[i+1];
            if (i > 0 && i<=n && lastPref[i] == lastVal[i+1]) curAns++;
            ans = Math.min(ans, curAns);
        }
        out.println(ans);
    }

    class Number implements Comparable<Number>{
        int x, cost;
        Number(int x, int cost){
            this.x = x;
            this.cost = cost;
        }

        @Override
        public int compareTo(Number o) {
            return Integer.compare(this.cost, o.cost);
        }
    }
    class Point{
        int x, y;
        Point(int x, int y){
            this.x = x;
            this.y = y;
        }
    }

    class Vertex implements Comparable<Vertex>{
        int num, depth, e, c;
        Vertex(int num, int depth, int e, int c){
            this.num = num;
            this.e = e;
            this.depth = depth;
            this.c = c;
        }

        @Override
        public int compareTo(Vertex o) {
            return Integer.compare(this.e, o.e);
        }
    }
    ///////////////////////////////////////////////////////////////////////////////////////////

    class Edge{
        int from, to, num;
        Edge(int to, int num){
            this.to = to;
            this.num = num;
        }
    }

    class SparseTable{
        int[][] rmq;
        int[] logTable;
        int n;
        SparseTable(int[] a){
            n = a.length;
            logTable = new int[n+1];
            for(int i = 2; i <= n; ++i){
                logTable[i] = logTable[i >> 1] + 1;
            }
            rmq = new int[logTable[n] + 1][n];
            for(int i=0; i<n; ++i){
                rmq[0][i] = a[i];
            }

            for(int k=1; (1 << k) < n; ++k){
                for(int i=0; i + (1 << k) <= n; ++i){
                    int max1 = rmq[k - 1][i];
                    int max2 = rmq[k-1][i + (1 << (k-1))];
                    rmq[k][i] = Math.max(max1, max2);
                }
            }
        }

        int max(int l, int r){
            int k = logTable[r - l];
            int max1 = rmq[k][l];
            int max2 = rmq[k][r - (1 << k) + 1];
            return Math.max(max1, max2);
        }
    }
    long checkBit(long mask, int bit){
        return (mask >> bit) & 1;
    }
    class Dsu{
        int[] parent;
        int countSets;
        Dsu(int n){
            countSets = n;
            parent = new int[n];
            for(int i=0; i<n; ++i){
                parent[i] = i;
            }
        }
        int findSet(int a){
            if(parent[a] == a) return a;
            parent[a] = findSet(parent[a]);
            return parent[a];
        }
        void unionSets(int a, int b){
            a = findSet(a);
            b = findSet(b);
            if(a!=b){
                countSets--;
                parent[a] = b;
            }
        }
    }
    static int checkBit(int mask, int bit) {
        return (mask >> bit) & 1;
    }
    boolean isLower(char c){
        return c >= 'a' && c <= 'z';
    }

    ////////////////////////////////////////////////////////////

    class SegmentTree{
        int[] t;
        int n;
        SegmentTree(int n){
            t = new int[4*n];
            build(new int[n+1], 1, 1, n);
        }
        void build (int a[], int v, int tl, int tr) {
            if (tl == tr)
                t[v] = a[tl];
            else {
                int tm = (tl + tr) / 2;
                build (a, v*2, tl, tm);
                build (a, v*2+1, tm+1, tr);
            }
        }

        void update (int v, int tl, int tr, int l, int r, int add) {
            if (l > r)
                return;
            if (l == tl && tr == r)
                t[v] += add;
            else {
                int tm = (tl + tr) / 2;
                update (v*2, tl, tm, l, Math.min(r,tm), add);
                update (v*2+1, tm+1, tr, Math.max(l,tm+1), r, add);
            }
        }

        int get (int v, int tl, int tr, int pos) {
            if (tl == tr)
                return t[v];
            int tm = (tl + tr) / 2;
            if (pos <= tm)
                return t[v] + get (v*2, tl, tm, pos);
            else
                return t[v] + get (v*2+1, tm+1, tr, pos);
        }
    }
    class Fenwik {
        long[] t;
        int length;

        Fenwik(int[] a) {
            length = a.length + 100;
            t = new long[length];

            for (int i = 0; i < a.length; ++i) {
                inc(i, a[i]);
            }
        }

        void inc(int ind, int delta) {
            for (; ind < length; ind = ind | (ind + 1)) {
                t[ind] = Math.max(delta, t[ind]);
            }
        }

        long getMax(int r) {
            long sum = 0;
            for (; r >= 0; r = (r & (r + 1)) - 1) {
                sum = Math.max(sum, t[r]);
            }
            return sum;
        }
    }
    int gcd(int a, int b){
        return b == 0 ? a : gcd(b, a%b);
    }
    long gcd(long a, long b){
        return b == 0 ? a : gcd(b, a%b);
    }
    double binPow(double a, int pow){
        if (pow == 0) return 1;
        if (pow % 2 == 1) {
            return a * binPow(a, pow - 1);
        } else {
            double c = binPow(a, pow / 2);
            return c * c;
        }
    }
    long binPow(long a, long b, long m) {
        if (b == 0) {
            return 1;
        }
        if (b % 2 == 1) {
            return ((a % m) * (binPow(a, b - 1, m) % m)) % m;
        } else {
            long c = binPow(a, b / 2, m);
            return (c * c) % m;
        }

    }
    int minInt(int... values) {
        int min = Integer.MAX_VALUE;
        for (int value : values) min = Math.min(min, value);
        return min;
    }

    int maxInt(int... values) {
        int max = Integer.MIN_VALUE;
        for (int value : values) max = Math.max(max, value);
        return max;
    }
    public static void main(String[] args) throws NumberFormatException, IOException {
        // TODO Auto-generated method stub
        new Main().run();
    }

    void run() throws NumberFormatException, IOException {
        solve();
        out.close();
    };

    BufferedReader in;
    PrintWriter out;

    StringTokenizer tok;
    String delim = " ";
    Random rnd = new Random();

    Main() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            if (fileName.isEmpty()) {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                in = new BufferedReader(new FileReader(fileName + ".in"));
                out = new PrintWriter(fileName + ".out");
            }

        }
        tok = new StringTokenizer("");
    }

    String readLine() throws IOException {
        return in.readLine();
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            String nextLine = readLine();
            if (null == nextLine) {
                return null;
            }

            tok = new StringTokenizer(nextLine);
        }
        return tok.nextToken();
    }

    int readInt() throws NumberFormatException, IOException {
        return Integer.parseInt(readString());
    }
    byte readByte() throws NumberFormatException, IOException {
        return Byte.parseByte(readString());
    }
    int[] readIntArray (int n) throws NumberFormatException, IOException {
        int[] a = new int[n];
        for(int i=0; i<n; ++i){
            a[i] = readInt();
        }
        return a;
    }

    Integer[] readIntegerArray (int n) throws NumberFormatException, IOException {
        Integer[] a = new Integer[n];
        for(int i=0; i<n; ++i){
            a[i] = readInt();
        }
        return a;
    }

    long readLong() throws NumberFormatException, IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws NumberFormatException, IOException {
        return Double.parseDouble(readString());
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class C {

    public static void main(String[] args) {
        try (InputStreamReader instream = new InputStreamReader(System.in);
             BufferedReader buffer = new BufferedReader(instream)) {
            String str = buffer.readLine();
            int n = Integer.parseInt(str);
            long[] v = new long[n];
            long[] ls = new long[n];
            long[] lv = new long[n];
            long[] rs = new long[n];
            long[] rv = new long[n];
            String secondStr = buffer.readLine();
            String[] vStr = secondStr.split(" ");
            for(int i=0; i<n; i++) {
                v[i] = Long.parseLong(vStr[i]);
                if(i!=n-1) {
                    v[i + 1] = Long.parseLong(vStr[i + 1]);
                    long tmp = lv[i] !=0 ? lv[i] : v[i];
                    if (v[i + 1] <= tmp) {
                        lv[i + 1] = tmp + 1;
                        ls[i + 1] = lv[i + 1] - v[i + 1] + ls[i];
                    } else {
                        lv[i] = v[i];
                        lv[i + 1] = v[i+1];
                    }
                }
            }
            for(int i=n-1; i>=0; i-- ){
                if(i!=0) {
                    long tmp = rv[i] !=0 ? rv[i] : v[i];
                    if(v[i-1]<=tmp){
                        rv[i-1] = tmp+1;
                        rs[i-1] = rv[i-1] - v[i-1]+rs[i];
                    }
                    else {
                        rv[i] = v[i];
                        rv[i-1] = v[i-1];
                    }
                }
            }
            long res = Long.MAX_VALUE;
            for(int i=0; i<n; i++) {
                long tmp = ls[i]+rs[i]-(Math.min(lv[i],rv[i])-v[i]);
                if(tmp<res) {
                    res = tmp;
                }
            }
            System.out.println(res);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class C {

    public static void main(String[] args) {
        try (InputStreamReader instream = new InputStreamReader(System.in);
             BufferedReader buffer = new BufferedReader(instream)) {
            String str = buffer.readLine();
            int n = Integer.parseInt(str);
            long[] v = new long[n];
            long[] ls = new long[n];
            long[] lv = new long[n];
            long[] rs = new long[n];
            long[] rv = new long[n];
            String secondStr = buffer.readLine();
            String[] vStr = secondStr.split(" ");
            v[0] = Long.parseLong(vStr[0]);
            lv[0] = v[0];
            ls[0] = 0;
            for(int i=1; i<n; i++) {
                v[i] = Long.parseLong(vStr[i]);
                if(v[i]<=lv[i-1]){
                    lv[i]=lv[i-1]+1;
                } else {
                    lv[i] = v[i];
                }
                ls[i]=lv[i]-v[i]+ls[i-1];
            }
            rv[n-1] = v[n-1];
            rs[n-1] = 0;
            for(int i=n-2; i>=0; i--) {
                if(v[i]<=rv[i+1]){
                    rv[i]=rv[i+1]+1;
                } else {
                    rv[i] = v[i];
                }
                rs[i]=rv[i]-v[i]+rs[i+1];
            }
            long res = Long.MAX_VALUE;
            for(int i=0;i<n;i++){
                long tmp = (ls[i]+rs[i]-(Math.min(lv[i],rv[i])-v[i]));
                if(tmp<res)
                    res = tmp;
            }
            System.out.println(res);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


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

public class Solution {

    public static void main(String[] args) throws Exception {
        MyReader reader = new MyReader(System.in);
        MyWriter writer = new MyWriter(System.out);
        new Solution().run(reader, writer);
        writer.close();
    }

    private void run(MyReader reader, MyWriter writer) throws IOException {
        int n = reader.nextInt();
        int[] a = reader.nextIntArray(n);
        int l = 0;
        int r = n - 1;
        long ans = 0;
        while (true) {
            while (l + 1 < n && a[l] < a[l + 1]) {
                l++;
            }
            while (r > 0 && a[r - 1] > a[r]) {
                r--;
            }
            if (l >= r) {
                writer.print(ans);
                return;
            }
            if (a[l] < a[r]) {
                ans += a[l] - a[l + 1] + 1;
                a[l + 1] = a[l] + 1;
            } else {
                ans += a[r] - a[r - 1] + 1;
                a[r - 1] = a[r] + 1;
            }
        }
    }

    static class MyReader {

        final BufferedInputStream in;
        final int bufSize = 1 << 16;
        final byte buf[] = new byte[bufSize];
        int i = bufSize;
        int k = bufSize;
        final StringBuilder str = new StringBuilder();

        MyReader(InputStream in) {
            this.in = new BufferedInputStream(in, bufSize);
        }

        int nextInt() throws IOException {
            return (int) nextLong();
        }

        int[] nextIntArray(int n) throws IOException {
            int[] m = new int[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextInt();
            }
            return m;
        }

        int[][] nextIntMatrix(int n, int m) throws IOException {
            int[][] a = new int[n][0];
            for (int j = 0; j < n; j++) {
                a[j] = nextIntArray(m);
            }
            return a;
        }

        long nextLong() throws IOException {
            int c;
            long x = 0;
            boolean sign = true;
            while ((c = nextChar()) <= 32) ;
            if (c == '-') {
                sign = false;
                c = nextChar();
            }
            if (c == '+') {
                c = nextChar();
            }
            while (c >= '0') {
                x = x * 10 + (c - '0');
                c = nextChar();
            }
            return sign ? x : -x;
        }

        long[] nextLongArray(int n) throws IOException {
            long[] m = new long[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextLong();
            }
            return m;
        }

        int nextChar() throws IOException {
            if (i == k) {
                k = in.read(buf, 0, bufSize);
                i = 0;
            }
            return i >= k ? -1 : buf[i++];
        }

        String nextString() throws IOException {
            int c;
            str.setLength(0);
            while ((c = nextChar()) <= 32 && c != -1) ;
            if (c == -1) {
                return null;
            }
            while (c > 32) {
                str.append((char) c);
                c = nextChar();
            }
            return str.toString();
        }

        String nextLine() throws IOException {
            int c;
            str.setLength(0);
            while ((c = nextChar()) <= 32 && c != -1) ;
            if (c == -1) {
                return null;
            }
            while (c != '\n') {
                str.append((char) c);
                c = nextChar();
            }
            return str.toString();
        }

        char[] nextCharArray() throws IOException {
            return nextString().toCharArray();
        }

        char[][] nextCharMatrix(int n) throws IOException {
            char[][] a = new char[n][0];
            for (int i = 0; i < n; i++) {
                a[i] = nextCharArray();
            }
            return a;
        }
    }

    static class MyWriter {

        final BufferedOutputStream out;
        final int bufSize = 1 << 16;
        final byte buf[] = new byte[bufSize];
        int i = 0;
        final byte c[] = new byte[30];
        static final String newLine = System.getProperty("line.separator");

        MyWriter(OutputStream out) {
            this.out = new BufferedOutputStream(out, bufSize);
        }

        void print(long x) throws IOException {
            int j = 0;
            if (i + 30 >= bufSize) {
                flush();
            }
            if (x < 0) {
                buf[i++] = (byte) ('-');
                x = -x;
            }
            while (j == 0 || x != 0) {
                c[j++] = (byte) (x % 10 + '0');
                x /= 10;
            }
            while (j-- > 0)
                buf[i++] = c[j];
        }

        void print(int[] m) throws IOException {
            for (int a : m) {
                print(a);
                print(' ');
            }
        }

        void print(long[] m) throws IOException {
            for (long a : m) {
                print(a);
                print(' ');
            }
        }

        void print(String s) throws IOException {
            for (int i = 0; i < s.length(); i++) {
                print(s.charAt(i));
            }
        }

        void print(char x) throws IOException {
            if (i == bufSize) {
                flush();
            }
            buf[i++] = (byte) x;
        }

        void print(char[] m) throws IOException {
            for (char c : m) {
                print(c);
            }
        }

        void println(String s) throws IOException {
            print(s);
            println();
        }

        void println() throws IOException {
            print(newLine);
        }

        void flush() throws IOException {
            out.write(buf, 0, i);
            out.flush();
            i = 0;
        }

        void close() throws IOException {
            flush();
            out.close();
        }
    }
}


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

public class Solution {

    public static void main(String[] args) throws Exception {
        MyReader reader = new MyReader(System.in);
        MyWriter writer = new MyWriter(System.out);
        new Solution().run(reader, writer);
        writer.close();
    }

    private void run(MyReader reader, MyWriter writer) throws IOException {
        int n = reader.nextInt();
        int[] a = reader.nextIntArray(n);
        int l = 0;
        int r = n - 1;
        long ans = 0;
        while (true) {
            while (l < n && a[l] < a[l + 1]) {
                l++;
            }
            while (r >= 0 && a[r - 1] > a[r]) {
                r--;
            }
            if (l >= r) {
                writer.print(ans);
                return;
            }
            if (a[l] < a[r]) {
                ans += a[l] - a[l + 1] + 1;
                a[l + 1] = a[l] + 1;
            } else {
                ans += a[r] - a[r - 1] + 1;
                a[r - 1] = a[r] + 1;
            }
        }
    }

    static class MyReader {

        final BufferedInputStream in;
        final int bufSize = 1 << 16;
        final byte buf[] = new byte[bufSize];
        int i = bufSize;
        int k = bufSize;
        final StringBuilder str = new StringBuilder();

        MyReader(InputStream in) {
            this.in = new BufferedInputStream(in, bufSize);
        }

        int nextInt() throws IOException {
            return (int) nextLong();
        }

        int[] nextIntArray(int n) throws IOException {
            int[] m = new int[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextInt();
            }
            return m;
        }

        int[][] nextIntMatrix(int n, int m) throws IOException {
            int[][] a = new int[n][0];
            for (int j = 0; j < n; j++) {
                a[j] = nextIntArray(m);
            }
            return a;
        }

        long nextLong() throws IOException {
            int c;
            long x = 0;
            boolean sign = true;
            while ((c = nextChar()) <= 32) ;
            if (c == '-') {
                sign = false;
                c = nextChar();
            }
            if (c == '+') {
                c = nextChar();
            }
            while (c >= '0') {
                x = x * 10 + (c - '0');
                c = nextChar();
            }
            return sign ? x : -x;
        }

        long[] nextLongArray(int n) throws IOException {
            long[] m = new long[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextLong();
            }
            return m;
        }

        int nextChar() throws IOException {
            if (i == k) {
                k = in.read(buf, 0, bufSize);
                i = 0;
            }
            return i >= k ? -1 : buf[i++];
        }

        String nextString() throws IOException {
            int c;
            str.setLength(0);
            while ((c = nextChar()) <= 32 && c != -1) ;
            if (c == -1) {
                return null;
            }
            while (c > 32) {
                str.append((char) c);
                c = nextChar();
            }
            return str.toString();
        }

        String nextLine() throws IOException {
            int c;
            str.setLength(0);
            while ((c = nextChar()) <= 32 && c != -1) ;
            if (c == -1) {
                return null;
            }
            while (c != '\n') {
                str.append((char) c);
                c = nextChar();
            }
            return str.toString();
        }

        char[] nextCharArray() throws IOException {
            return nextString().toCharArray();
        }

        char[][] nextCharMatrix(int n) throws IOException {
            char[][] a = new char[n][0];
            for (int i = 0; i < n; i++) {
                a[i] = nextCharArray();
            }
            return a;
        }
    }

    static class MyWriter {

        final BufferedOutputStream out;
        final int bufSize = 1 << 16;
        final byte buf[] = new byte[bufSize];
        int i = 0;
        final byte c[] = new byte[30];
        static final String newLine = System.getProperty("line.separator");

        MyWriter(OutputStream out) {
            this.out = new BufferedOutputStream(out, bufSize);
        }

        void print(long x) throws IOException {
            int j = 0;
            if (i + 30 >= bufSize) {
                flush();
            }
            if (x < 0) {
                buf[i++] = (byte) ('-');
                x = -x;
            }
            while (j == 0 || x != 0) {
                c[j++] = (byte) (x % 10 + '0');
                x /= 10;
            }
            while (j-- > 0)
                buf[i++] = c[j];
        }

        void print(int[] m) throws IOException {
            for (int a : m) {
                print(a);
                print(' ');
            }
        }

        void print(long[] m) throws IOException {
            for (long a : m) {
                print(a);
                print(' ');
            }
        }

        void print(String s) throws IOException {
            for (int i = 0; i < s.length(); i++) {
                print(s.charAt(i));
            }
        }

        void print(char x) throws IOException {
            if (i == bufSize) {
                flush();
            }
            buf[i++] = (byte) x;
        }

        void print(char[] m) throws IOException {
            for (char c : m) {
                print(c);
            }
        }

        void println(String s) throws IOException {
            print(s);
            println();
        }

        void println() throws IOException {
            print(newLine);
        }

        void flush() throws IOException {
            out.write(buf, 0, i);
            out.flush();
            i = 0;
        }

        void close() throws IOException {
            flush();
            out.close();
        }
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 *
 * @author bogomolov_d_ju
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */

    long[] rv;
    long[] ls;
    long[] rs;
    int n;
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        long[] init = new long[n];
        long[] lv = new long[n];
        long[] rv = new long[n];
        long[] ls = new long[n];
        long[] rs = new long[n];
        StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
        int i = 0;
        while(tokenizer.hasMoreElements()) {
            init[i] = Integer.parseInt(tokenizer.nextToken());
            i++;
        }
        lv[0] = init[0];
        ls[0] = lv[0] - init[0];
        for(i = 1; i<n; i++) {
            lv[i] = init[i] <= lv[i-1] ? lv[i-1] + 1 : init[i];
            ls[i] = lv[i] - init[i] + ls[i-1];
        }
        rv[n-1] = init[n-1];
        rs[n-1] = rv[n-1] - init[n-1];
        for(i = n-2; i>=0; i--) {
            rv[i] = init[i] <= rv[i+1] ? rv[i+1] + 1 : init[i];
            rs[i] = rv[i] - init[i] + rs[i+1];
        }

        long min = Long.MAX_VALUE;
        for(i = 0; i<n; i++) {
            long temp = ls[i] + rs[i] - (Math.min(lv[i], rv[i]) - init[i]);
            if(temp < min) min = temp;
        }
        System.out.println(min);
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class H {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt();
		long[] c = new long[n];
		long[] d = new long[n];
		for (int i = 0; i < n; i++) {
			d[i] = c[i] = sc.nextLong();
		}
		long[] a = new long[n + 1];
		for (int i = n - 2; i >= 0; i--) {
			if (c[i] <= c[i + 1]) {
				a[i] = Math.abs(c[i] - c[i + 1]) + 1;
				c[i] += Math.abs(c[i] - c[i + 1]) + 1;
			}
			a[i] += a[i + 1];
		}
		long[] b = new long[n + 1];
		for (int i = 1; i < n; i++) {
			if (d[i] <= d[i - 1]) {
				b[i + 1] = Math.abs(d[i] - d[i - 1]) + 1;
				d[i] += Math.abs(d[i] - d[i - 1]) + 1;
			}
			b[i + 1] += b[i];
		}
		long ans = Long.MAX_VALUE;
		for (int i = 0; i <= n; i++)
			ans = Math.min(ans, a[i] + b[i]);
		out.println(ans);
		out.flush();
		out.close();
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner(InputStream is) {
			br = new BufferedReader(new InputStreamReader(is));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws IOException {
			return Long.parseLong(next());
		}
	}
}