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++) {
			if (i > 0 && i < n && c[i] == d[i - 1])
				ans = Math.min(ans, a[i] + b[i] + 1);
			else
				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());
		}
	}
}


import java.io.*;

public class H {
    public static void main(String[] args) throws IOException
    {
        int n = nI();
        long mas[] = new long[n];
        for (int i = 0; i < n; i++) {
            mas[i] = nI();
        }
        int posleft = 0;
        int posright = n-1;
        long result = 0;

        while(posleft < posright-1){
            if(Math.max(mas[posleft+1], mas[posleft]+1) < mas[posright]){
                long plus = Math.max(mas[posleft+1], mas[posleft]+1) - mas[posleft+1];
                result += plus;
                mas[posleft+1] += plus;
                posleft++;
            }else{
                long plus = Math.max(mas[posright-1], mas[posright]+1) - mas[posright-1];
                result += plus;
                mas[posright-1] += plus;
                posright--;
            }
        }
        out.println(result);
        out.flush();
        out.close();
    }

    static PrintWriter out = new PrintWriter(System.out);
    public static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int nI() throws IOException
    {
        st.nextToken();
        return (int) st.nval;
    }
    static int nC() throws IOException
    {
        st.nextToken();
        return (char) st.nval;
    }
    static String nS() throws IOException {
        st.nextToken();
        return st.sval;
    }
}


//package icpc;

import java.io.*;

import java.util.*;
public class H847 {

	public static void main(String[] args) throws IOException {

		IO io = new IO(System.in);
		int n = io.nextInt();
		long[] h = new long[n];
		for (int i = 0; i < h.length; i++) {
			h[i] = io.nextLong();
		}
		long[] r = new long[n], l = new long[n];
		long[] newr = new long[n], newl = new long[n];
		r[0] = 0;
		newr[0] = h[0];
		for (int i = 1; i < n; i++) {
			long max = Math.max(h[i], newr[i-1] + 1);
			newr[i] = max;
			r[i] = r[i-1] + max - h[i];
		}
		l[n-1] = 0;
		newl[n-1] = h[n-1];
		for (int i = n-2; i >= 0; i--) {
			long max = Math.max(h[i], newl[i+1] + 1);
			newl[i] = max;
			l[i] = l[i+1] + max - h[i];
		}
		long min = Long.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			long cost = 0;
			if(newl[i] > newr[i]) {
				cost += l[i];
				cost += r[i];
				cost -= (newr[i] - h[i]);
				min = Math.min(min, cost);
			}
			else {
				cost += l[i];
				cost += r[i];
				cost -= (newl[i] - h[i]);
				min = Math.min(min, cost);
			}
		}
		io.println(min);

		io.close();

	}

	static class IO extends PrintWriter {

		static BufferedReader r;

		static StringTokenizer t;

		public IO(InputStream i) {

			super(new BufferedOutputStream(System.out));

			r = new BufferedReader(new InputStreamReader(i));

			t = new StringTokenizer("");

		}

		public String next() throws IOException {

			while (!t.hasMoreTokens()) {

				t = new StringTokenizer(r.readLine());

			}

			return t.nextToken();

		}

		public int nextInt() throws IOException{

			return Integer.parseInt(next());

		}

		public long nextLong() throws IOException {

			return Long.parseLong(next());

		}

		public double nextDouble() throws IOException {

			return Double.parseDouble(next());

		}

	}

}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;

public final class Main {

	static int p[] = new int[100005];
	static int s[] = new int[100005];
	static int arr[] = new int[100005];
	public static void main(String[]args) {

		InputReader ir = new InputReader(System.in);
		PrintWriter w = new PrintWriter(System.out);

		int t,n,i,j,k;
		n = ir.nextInt();
		for(i=1 ; i<=n ; i++)
			arr[i] = ir.nextInt();

		p[0] = s[n+1] = arr[0] = arr[n+1] = 0;
		for(i=1 ; i<=n ; i++) {
			p[i] = Math.max(p[i-1]+arr[i-1]-arr[i]+1,0);
		}

		long ps =0;
		long ss = 0;
		ss = 0;
		for(i=n ; i>=1 ; i--) {
			s[i] = Math.max(arr[i+1]+s[i+1]-arr[i]+1, 0);
			ss += s[i];
		}

		long ans = (long)4e+18;
		int ht;
		for(i=1 ; i<=n ; i++) {

			ss -= s[i];
			ht = Math.max(arr[i+1]+s[i+1], arr[i-1]+p[i-1]);
			if(ht >= arr[i]) {
				ht = ht-arr[i]+1;
			}else
				ht = 0;
			ans = Math.min(ans,ss+ps+ht);
			ps += p[i];
		}
		w.println(ans);
		w.close();
	}

	static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        private SpaceCharFilter filter;

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

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

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

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

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextInt();
            }
            return a;
        }

        public String readString() {
            int c = snext();
            while (isSpaceChar(c)) {
                c = snext();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = snext();
            } while (!isSpaceChar(c));
            return res.toString();
        }

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

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

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

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}
class node{
	node ref[];
	int cnt;
	node(){
		ref = new node[11];
		cnt =0;
	}
}


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

/*
 * 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 Quan2202
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        int n = in.nextInt();
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextLong();
        }
        //Max from the left
        long[] maxInc = new long[n];
        maxInc[0] = a[0];
        long[] inc = new long[n];
        inc[0] = 0;

        for (int i = 1; i < n; i++) {
            if (a[i] <= maxInc[i-1]) {
                maxInc[i] = maxInc[i-1]+1;
                inc[i] += inc[i - 1] + (maxInc[i] - a[i]);
            } else {
                maxInc[i] = a[i];
                inc[i] = inc[i - 1];
            }
        }
        //Max from the right
        long[] maxDec = new long[n];
        maxDec[n-1] = a[n-1];
        long[] dec = new long[n];
        dec[n - 1] = 0;

        for (int i = n - 2; i >= 0; i--) {
            if (a[i] <= maxDec[i+1]) {
                maxDec[i] = maxDec[i+1]+1;
                dec[i] += dec[i + 1] + (maxDec[i] - a[i]);
            } else {
                maxDec[i] = a[i];
                dec[i] = dec[i + 1];
            }
        }
        //Determine situations
        long[] sum = new long[n+1];

        for (int i = 0; i < n-1; i++) {
            //out.println(inc[i] + " " + dec[i]);
            sum[i+1] = inc[i] + dec[i + 1];
            if(maxInc[i]==maxDec[i+1]){
                sum[i+1]++;
            }

        }
        sum[0] = dec[0];
        sum[n] = inc[n-1];

        long min = sum[0];
        for (int i = 1; i <= n; i++) {
            if (sum[i] < min) {
                min = sum[i];
            }
        }
        out.print(min);
        out.flush();
    }

    static void sort(int arr[]) {
        int n = arr.length;

        // Build heap (rearrange array)
        for (int i = n / 2 - 1; i >= 0; i--) {
            heapify(arr, n, i);
        }

        // One by one extract an element from heap
        for (int i = n - 1; i >= 0; i--) {
            // Move current root to end
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;

            // call max heapify on the reduced heap
            heapify(arr, i, 0);
        }
    }

    // To heapify a subtree rooted with node i which is
    // an index in arr[]. n is size of heap
    static void heapify(int arr[], int n, int i) {
        int largest = i; // Initialize largest as root
        int l = 2 * i + 1; // left = 2*i + 1
        int r = 2 * i + 2; // right = 2*i + 2

        // If left child is larger than root
        if (l < n && arr[l] > arr[largest]) {
            largest = l;
        }

        // If right child is larger than largest so far
        if (r < n && arr[r] > arr[largest]) {
            largest = r;
        }

        // If largest is not root
        if (largest != i) {
            int swap = arr[i];
            arr[i] = arr[largest];
            arr[largest] = swap;

            // Recursively heapify the affected sub-tree
            heapify(arr, n, largest);
        }
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }
        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}


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

/*
 * 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 Quan2202
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);

        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
        }
        int[] maxInc = new int[n];
        maxInc[0] = a[0];
        int[] inc = new int[n];
        inc[0] = 0;

        for (int i = 1; i < n; i++) {
            if (a[i] <= maxInc[i-1]) {
                maxInc[i] = maxInc[i-1]+1;
                inc[i] += inc[i - 1] + (maxInc[i] - a[i]);
            } else {
                maxInc[i] = a[i];
                inc[i] = inc[i - 1];
            }
        }

        int[] maxDec = new int[n];
        maxDec[n-1] = a[n-1];
        int[] dec = new int[n];
        dec[n - 1] = 0;

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

        int[] sum = new int[n+1];

        for (int i = 0; i < n-1; i++) {
            //out.println(inc[i] + " " + dec[i]);
            sum[i+1] = inc[i] + dec[i + 1];
            if(maxInc[i]==maxDec[i+1]){
                sum[i+1]++;
            }

        }
        sum[0] = dec[0];
        sum[n] = inc[n-1];

        int min = sum[0];
        for (int i = 1; i <= n; i++) {
            if (sum[i] < min) {
                min = sum[i];
            }
        }
        out.print(min);
        out.flush();
    }

    static void sort(int arr[]) {
        int n = arr.length;

        // Build heap (rearrange array)
        for (int i = n / 2 - 1; i >= 0; i--) {
            heapify(arr, n, i);
        }

        // One by one extract an element from heap
        for (int i = n - 1; i >= 0; i--) {
            // Move current root to end
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;

            // call max heapify on the reduced heap
            heapify(arr, i, 0);
        }
    }

    // To heapify a subtree rooted with node i which is
    // an index in arr[]. n is size of heap
    static void heapify(int arr[], int n, int i) {
        int largest = i; // Initialize largest as root
        int l = 2 * i + 1; // left = 2*i + 1
        int r = 2 * i + 2; // right = 2*i + 2

        // If left child is larger than root
        if (l < n && arr[l] > arr[largest]) {
            largest = l;
        }

        // If right child is larger than largest so far
        if (r < n && arr[r] > arr[largest]) {
            largest = r;
        }

        // If largest is not root
        if (largest != i) {
            int swap = arr[i];
            arr[i] = arr[largest];
            arr[largest] = swap;

            // Recursively heapify the affected sub-tree
            heapify(arr, n, largest);
        }
    }

    static class InputReader {

        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class NEERC17_H
{
	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
			arr[i] = sc.nextInt();

		long[] hinc, hdec, cinc, cdec;
		hinc = new long[n]; cinc = new long[n]; hdec = new long[n]; cdec = new long[n];
		cinc[0] = cdec[n-1] = 0;
		hinc[0] = arr[0]; hdec[n-1] = arr[n-1];

		for (int i = 1; i < n; i++)
		{
			hinc[i] = Math.max(arr[i], hinc[i-1]+1);
			cinc[i] = cinc[i-1] + Math.max(0, hinc[i] - arr[i]);
		}

		for (int i = n-2; i >= 0; i--)
		{
			hdec[i] = Math.max(arr[i], hdec[i+1]+1);
			cdec[i] = cdec[i+1] + Math.max(0, hdec[i] - arr[i]);
		}

		long ans = Long.MAX_VALUE;
		for (int i = 0; i < n; i++)
		{
			long cur = 0;
			if(i > 0)
				cur += cinc[i-1];
			if(i < n-1)
				cur += cdec[i+1];
			cur += Math.max(hdec[i] - arr[i], hinc[i] - arr[i]);
			ans = Math.min(ans, cur);
		}

		System.out.println(ans);
	}

	static class Scanner
	{
		StringTokenizer st; BufferedReader br;
		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}
		public Scanner(String s) throws FileNotFoundException{	br = new BufferedReader(new FileReader(new File((s))));}
		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException {return Integer.parseInt(next());}
		public long nextLong() throws IOException {return Long.parseLong(next());}
		public String nextLine() throws IOException {return br.readLine();}
		public double nextDouble() throws IOException
		{
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if(x.charAt(0) == '-')
			{
				neg = true;
				start++;
			}
			for(int i = start; i < x.length(); i++)
				if(x.charAt(i) == '.')
				{
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				}
				else
				{
					sb.append(x.charAt(i));
					if(dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg?-1:1);
		}
		public boolean ready() throws IOException {return br.ready();}
	}
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class NEERC17_H
{
	public static void main(String[] args) throws IOException
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++)
			arr[i] = sc.nextInt();

		int[] hinc, hdec, cinc, cdec;
		hinc = new int[n]; cinc = new int[n]; hdec = new int[n]; cdec = new int[n];
		cinc[0] = cinc[n-1] = 0;
		hinc[0] = arr[0]; hdec[n-1] = arr[n-1];

		for (int i = 1; i < n; i++)
		{
			hinc[i] = Math.max(arr[i], hinc[i-1]+1);
			cinc[i] = cinc[i-1] + Math.max(0, hinc[i] - arr[i]);
		}

		for (int i = n-2; i >= 0; i--)
		{
			hdec[i] = Math.max(arr[i], hdec[i+1]+1);
			cdec[i] = cdec[i+1] + Math.max(0, hdec[i] - arr[i]);
		}

		int ans = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++)
		{
			int cur = 0;
			if(i > 0)
				cur += cinc[i-1];
			if(i < n-1)
				cur += cdec[i+1];
			cur += Math.min(hdec[i] - arr[i], hinc[i] - arr[i]);
			ans = Math.min(ans, cur);
		}

		System.out.println(ans);
	}

	static class Scanner
	{
		StringTokenizer st; BufferedReader br;
		public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(s));}
		public Scanner(String s) throws FileNotFoundException{	br = new BufferedReader(new FileReader(new File((s))));}
		public String next() throws IOException
		{
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int nextInt() throws IOException {return Integer.parseInt(next());}
		public long nextLong() throws IOException {return Long.parseLong(next());}
		public String nextLine() throws IOException {return br.readLine();}
		public double nextDouble() throws IOException
		{
			String x = next();
			StringBuilder sb = new StringBuilder("0");
			double res = 0, f = 1;
			boolean dec = false, neg = false;
			int start = 0;
			if(x.charAt(0) == '-')
			{
				neg = true;
				start++;
			}
			for(int i = start; i < x.length(); i++)
				if(x.charAt(i) == '.')
				{
					res = Long.parseLong(sb.toString());
					sb = new StringBuilder("0");
					dec = true;
				}
				else
				{
					sb.append(x.charAt(i));
					if(dec)
						f *= 10;
				}
			res += Long.parseLong(sb.toString()) / f;
			return res * (neg?-1:1);
		}
		public boolean ready() throws IOException {return br.ready();}
	}
}


import javax.print.DocFlavor;
import java.beans.IntrospectionException;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
import static java.lang.Math.*;
public class main implements Runnable {

    static ArrayList<Integer> adj[];

    static void Check2(int n) {
        adj = new ArrayList[n + 1];
        for (int i = 0; i <= n; i++) {
            adj[i] = new ArrayList<>();
        }

    }
    static void add(int i, int j)
    {
        adj[i].add(j);
        adj[j].add(i);
    }
    public static void main(String[] args) {
        new Thread(null, new main(), "Check2", 1 << 28).start();// to increse stack size in java
    }
    static long mod = (long) (1e9 + 7);
    public void run() {
        InputReader in = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);

        int n=in.nextInt();
        long a[]=new long[n];
        long inc[]=new long[n];
        long dec[]=new long[n];
        long prf[]=new long[n];
        long suf[]=new long[n];
        for(int i=0;i<n;i++)a[i]=in.nextLong();
        long prv=a[0];
        prf[0]=a[0];
        for(int i=1;i<n;i++)
        {
            long cur=max(a[i],prv+1);
            prf[i]=cur;
            inc[i]=inc[i-1]+abs(cur-a[i]);
            prv=cur;
        }
        suf[n-1]=a[n-1];
        prv=a[n-1];
        for(int i=n-2;i>=0;i--){
            long cur=max(prv+1,a[i]);
            suf[i]=cur;
            dec[i]=dec[i+1]+abs(cur-a[i]);
            prv=cur;

        }
        long ans=Long.MAX_VALUE;

        for(int i=0;i<n-1;i++){

            if(prf[i]>suf[i+1]){

                ans=min(ans,inc[i]+dec[i+1]);
            }

        }
        if(ans!=Long.MAX_VALUE)
        w.println(ans);
        else
            w.println(0);

        w.close();

    }
    static long lcm(long a,long b){
        return ((long)a*(long)b)/gcd(a,b);
    }
    static long gcd(long a,long b){
        if(b==0)return a;
        return gcd(b,a%b);
    }
    static class pair2{

        int a;
        int b;
        pair2(int a,int b){
            this.a=a;
            this.b=b;
        }

    }
    static void diji(int s,long dist[],ArrayList <pair> adj[]){
        dist[s]=0;
        PriorityQueue <pair> q=new PriorityQueue<>(new cmp());
        q.add(new pair(s,0));
        while (!q.isEmpty())
        {
            pair p1=q.poll();
            for(pair ne:adj[p1.a]){

                if((long)p1.b+(long)ne.b<dist[ne.a]){
                    dist[ne.a]=(long)p1.b+(long)ne.b;
                    pair p2=new pair(ne.a,dist[ne.a]);
                    q.add(p2);
                }

            }
        }

    }
    static class cmp implements Comparator<pair>{

        public  int compare(pair o1,pair o2)
        {
            return o1.b<o2.b?-1:o1.b>o2.b?1:0;
        }

    }

    static class pair {
        int a;
        long b;
        pair(int a,long b){
            this.a=a;
            this.b=b;
        }
        public boolean equals(Object obj) {      //  override equals method for object to remove tem from arraylist and sets etc.......
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            pair other = (pair) obj;
            if (b!= other.b||a!=other.a)
                return false;
            return true;
        }
    }

    static long modinv(long a,long b)
    {
        long p=power(b,mod-2);

        p=a%mod*p%mod;
        p%=mod;

        return p;

    }

    static long power(long x,long y){
        if(y==0)return 1%mod;
        if(y==1)return x%mod;

        long res=1;
        x=x%mod;
        while(y>0){

            if((y%2)!=0){
                res=(res*x)%mod;
            }

            y=y/2;
            x=(x*x)%mod;
        }

        return res;

    }
    static  int gcd(int a,int b){

        if(b==0)return a;
        return gcd(b,a%b);
    }

    static  void sev(int a[],int n){

        for(int i=2;i<=n;i++)a[i]=i;
        for(int i=2;i<=n;i++){

            if(a[i]!=0){
                for(int j=2*i;j<=n;){

                    a[j]=0;
                    j=j+i;
                }
            }

        }

    }

    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private 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 String nextLine()
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        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 long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

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

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

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

        public String next()
        {
            return readString();
        }

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

}


import javax.print.DocFlavor;
import java.beans.IntrospectionException;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.lang.*;
import static java.lang.Math.*;
public class main implements Runnable {

    static ArrayList<Integer> adj[];

    static void Check2(int n) {
        adj = new ArrayList[n + 1];
        for (int i = 0; i <= n; i++) {
            adj[i] = new ArrayList<>();
        }

    }
    static void add(int i, int j)
    {
        adj[i].add(j);
        adj[j].add(i);
    }
    public static void main(String[] args) {
        new Thread(null, new main(), "Check2", 1 << 28).start();// to increse stack size in java
    }
    static long mod = (long) (1e9 + 7);
    public void run() {
        InputReader in = new InputReader(System.in);
        PrintWriter w = new PrintWriter(System.out);

        int n=in.nextInt();
        long a[]=new long[n];
        long inc[]=new long[n];
        long dec[]=new long[n];
        long prf[]=new long[n];
        long suf[]=new long[n];
        for(int i=0;i<n;i++)a[i]=in.nextLong();
        long prv=a[0];
        for(int i=1;i<n;i++)
        {
            long cur=max(a[i],prv+1);
            prf[i]=cur;
            inc[i]=inc[i-1]+abs(cur-a[i]);
            prv=cur;
        }
        prv=a[n-1];
        for(int i=n-2;i>=0;i--){
            long cur=max(prv+1,a[i]);
            suf[i]=cur;
            dec[i]=dec[i+1]+abs(cur-a[i]);
            prv=cur;

        }
        long ans=Long.MAX_VALUE;

        for(int i=0;i<n-1;i++){

            if(prf[i]>suf[i+1]){

                ans=min(ans,inc[i]+dec[i+1]);
            }

        }
        w.println(ans);

        w.close();

    }
    static long lcm(long a,long b){
        return ((long)a*(long)b)/gcd(a,b);
    }
    static long gcd(long a,long b){
        if(b==0)return a;
        return gcd(b,a%b);
    }
    static class pair2{

        int a;
        int b;
        pair2(int a,int b){
            this.a=a;
            this.b=b;
        }

    }
    static void diji(int s,long dist[],ArrayList <pair> adj[]){
        dist[s]=0;
        PriorityQueue <pair> q=new PriorityQueue<>(new cmp());
        q.add(new pair(s,0));
        while (!q.isEmpty())
        {
            pair p1=q.poll();
            for(pair ne:adj[p1.a]){

                if((long)p1.b+(long)ne.b<dist[ne.a]){
                    dist[ne.a]=(long)p1.b+(long)ne.b;
                    pair p2=new pair(ne.a,dist[ne.a]);
                    q.add(p2);
                }

            }
        }

    }
    static class cmp implements Comparator<pair>{

        public  int compare(pair o1,pair o2)
        {
            return o1.b<o2.b?-1:o1.b>o2.b?1:0;
        }

    }

    static class pair {
        int a;
        long b;
        pair(int a,long b){
            this.a=a;
            this.b=b;
        }
        public boolean equals(Object obj) {      //  override equals method for object to remove tem from arraylist and sets etc.......
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            pair other = (pair) obj;
            if (b!= other.b||a!=other.a)
                return false;
            return true;
        }
    }

    static long modinv(long a,long b)
    {
        long p=power(b,mod-2);

        p=a%mod*p%mod;
        p%=mod;

        return p;

    }

    static long power(long x,long y){
        if(y==0)return 1%mod;
        if(y==1)return x%mod;

        long res=1;
        x=x%mod;
        while(y>0){

            if((y%2)!=0){
                res=(res*x)%mod;
            }

            y=y/2;
            x=(x*x)%mod;
        }

        return res;

    }
    static  int gcd(int a,int b){

        if(b==0)return a;
        return gcd(b,a%b);
    }

    static  void sev(int a[],int n){

        for(int i=2;i<=n;i++)a[i]=i;
        for(int i=2;i<=n;i++){

            if(a[i]!=0){
                for(int j=2*i;j<=n;){

                    a[j]=0;
                    j=j+i;
                }
            }

        }

    }

    static class InputReader
    {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private 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 String nextLine()
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
        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 long nextLong()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            long res = 0;

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

        public double nextDouble()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-')
            {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.')
            {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.')
            {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String readString()
        {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do
            {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

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

        public String next()
        {
            return readString();
        }

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

}


import java.util.*;
import java.io.*;
//public
class Main{
   public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    PrintWriter o=new PrintWriter(System.out);
    int n=sc.nextInt();
    long a[]=new long[n];
    for (int i=0;i<n ;i++ ) {
      a[i]=sc.nextLong();
    }
    long left[]=new long[n];
    long right[]=new long[n];
    left[0]=0L;
    right[n-1]=0L;

    for(int i=1;i<n;i++)
    {

          left[i]=a[i-1]+left[i-1]+1-a[i];
          if(left[i]<0)
            left[i]=0;

    }
    for(int i=n-2;i>=0;i--)
    {

          right[i]=a[i+1]+right[i+1]+1-a[i];
        if(right[i]<0)
          right[i]=0;

    }
     for(int i=1;i<n;i++)
        left[i]=left[i]+left[i-1];

     for(int i=n-2;i>=0;i--)
          right[i]=right[i]+right[i+1];

    for (int i=0;i<n ;i++ ) {
      System.out.println(left[i]+" "+right[i]);
    }
      long  max=Long.MAX_VALUE;
    if(n==1){
      System.out.println("0");
    }
    else{
    for(int i=0;i<n;i++)
    {
      long temp=0L;
      if(i==0){
         temp=Math.min(left[0]+right[1],right[0]);
         //System.out.println(i+" "+temp);

         max=Math.min(temp,max);
      }
      else if(i==n-1){
        temp=Math.min(left[n-1],right[n-1]+left[n-2]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
      else{
        temp=Math.min(left[i]+right[i+1],left[i-1]+right[i]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
    }
    o.println(max);
    o.flush();
    o.close();
   }}
}


import java.util.*;
import java.io.*;
//public
class Main{
   public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    PrintWriter o=new PrintWriter(System.out);
    int n=sc.nextInt();
    long a[]=new long[n];
    for (int i=0;i<n ;i++ ) {
      a[i]=sc.nextLong();
    }
    long left[]=new long[n];
    long right[]=new long[n];
    left[0]=0L;
    right[n-1]=0L;

    for(int i=1;i<n;i++)
    {

          left[i]=a[i-1]+left[i-1]+1-a[i];
          if(left[i]<0)
            left[i]=0;

    }
    for(int i=n-2;i>=0;i--)
    {

          right[i]=a[i+1]+right[i+1]+1-a[i];
        if(right[i]<0)
          right[i]=0;

    }
     for(int i=1;i<n;i++)
        left[i]=left[i]+left[i-1];

     for(int i=n-2;i>=0;i--)
          right[i]=right[i]+right[i+1];

   // for (int i=0;i<n ;i++ ) {
   //   System.out.println(left[i]+" "+right[i]);
    //}
      long  max=Long.MAX_VALUE;
    if(n==1){
      System.out.println("0");
    }
    else{
    for(int i=0;i<n;i++)
    {
      long temp=0L;
      if(i==0){
         temp=Math.min(left[0]+right[1],right[0]);
         //System.out.println(i+" "+temp);

         max=Math.min(temp,max);
      }
      else if(i==n-1){
        temp=Math.min(left[n-1],right[n-1]+left[n-2]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
      else{
        temp=Math.min(left[i]+right[i+1],left[i-1]+right[i]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
    }
    o.println(max);
    o.flush();
    o.close();
   }}
}


import java.util.*;
import java.io.*;
public class Main{
   public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    PrintWriter o=new PrintWriter(System.out);
    int n=sc.nextInt();
    int a[]=new int[n];
    for (int i=0;i<n ;i++ ) {
      a[i]=sc.nextInt();
    }
    int left[]=new int[n];
    int right[]=new int[n];
    left[0]=0;
    right[n-1]=0;

    for(int i=1;i<n;i++)
    {

          left[i]=a[i-1]+left[i-1]+1-a[i];
          if(left[i]<0)
            left[i]=left[i-1];

    }
    for(int i=n-2;i>=0;i--)
    {

          right[i]=a[i+1]+right[i+1]+1-a[i];
        if(right[i]<0)
          right[i]=right[i+1];

    }
     for(int i=1;i<n;i++)
        left[i]=left[i]+left[i-1];

     for(int i=n-2;i>=0;i--)
          right[i]=right[i]+right[i+1];

   // for (int i=0;i<n ;i++ ) {
     // System.out.println(left[i]+" "+right[i]);
    //}
      long  max=Long.MAX_VALUE;
    if(n==1){
      System.out.println("0");
    }
    else{
    for(int i=0;i<n;i++)
    {
      long temp=0L;
      if(i==0){
         temp=Math.min(left[0]+right[1],right[0]);
         //System.out.println(i+" "+temp);

         max=Math.min(temp,max);
      }
      else if(i==n-1){
        temp=Math.min(left[n-1],right[n-1]+left[n-2]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
      else{
        temp=Math.min(left[i]+right[i+1],left[i-1]+right[i]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
    }
    o.println(max);
    o.flush();
    o.close();
   }}
}


import java.util.*;
import java.io.*;
public class Main{
   public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    PrintWriter o=new PrintWriter(System.out);
    int n=sc.nextInt();
    int a[]=new int[n];
    for (int i=0;i<n ;i++ ) {
      a[i]=sc.nextInt();
    }
    int left[]=new int[n];
    int right[]=new int[n];
    left[0]=0;
    right[n-1]=0;

    for(int i=1;i<n;i++)
    {

          left[i]=a[i-1]+left[i-1]+1-a[i];
          if(left[i]<0)
            left[i]=left[i-1];

    }
    for(int i=n-2;i>=0;i--)
    {

          right[i]=a[i+1]+right[i+1]+1-a[i];
        if(right[i]<0)
          right[i]=right[i+1];

    }
     for(int i=1;i<n;i++)
        left[i]=left[i]+left[i-1];

     for(int i=n-2;i>=0;i--)
          right[i]=right[i]+right[i+1];

   // for (int i=0;i<n ;i++ ) {
     // System.out.println(left[i]+" "+right[i]);
    //}
      int max=Integer.MAX_VALUE;
    for(int i=0;i<n;i++)
    {
      int temp=0;
      if(i==0){
         temp=Math.min(left[0]+right[1],right[0]);
         //System.out.println(i+" "+temp);

         max=Math.min(temp,max);
      }
      else if(i==n-1){
        temp=Math.min(left[n-1],right[n-1]+left[n-2]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
      else{
        temp=Math.min(left[i]+right[i+1],left[i-1]+right[i]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
    }
    o.println(max);
    o.flush();
    o.close();
   }
}


import java.util.*;
import java.io.*;
//public
class Main{
   public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    PrintWriter o=new PrintWriter(System.out);
    int n=sc.nextInt();
    long a[]=new long[n];
    for (int i=0;i<n ;i++ ) {
      a[i]=sc.nextLong();
    }
    long left[]=new long[n];
    long right[]=new long[n];
    left[0]=0L;
    right[n-1]=0L;

    for(int i=1;i<n;i++)
    {

          left[i]=a[i-1]+left[i-1]+1-a[i];
          if(left[i]<0)
            left[i]=0;

    }
    for(int i=n-2;i>=0;i--)
    {

          right[i]=a[i+1]+right[i+1]+1-a[i];
        if(right[i]<0)
          right[i]=0;

    }
    long ttt[]=new long[n];
    for(int i=0;i<n;i++){
      ttt[i]=Math.min(a[i]+left[i],a[i]+right[i]);
    }
    boolean inc=false;
    for(int i=1;i<n;i++){
      if(ttt[i]==ttt[i-1]){
         inc=true;
         break;}
    }
     for(int i=1;i<n;i++)
        left[i]=left[i]+left[i-1];

     for(int i=n-2;i>=0;i--)
          right[i]=right[i]+right[i+1];

   // for (int i=0;i<n ;i++ ) {
   //   System.out.println(left[i]+" "+right[i]);
    //}
      long  max=Long.MAX_VALUE;
    if(n==1){
      System.out.println("0");
    }
    else{
    for(int i=0;i<n;i++)
    {
      long temp=0L;
      if(i==0){
         temp=Math.min(left[0]+right[1],right[0]);
         //System.out.println(i+" "+temp);

         max=Math.min(temp,max);
      }
      else if(i==n-1){
        temp=Math.min(left[n-1],right[n-1]+left[n-2]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
      else{
        temp=Math.min(left[i]+right[i+1],left[i-1]+right[i]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
    }
    if(inc)
      o.println(max+1);
    else
    o.println(max);
    o.flush();
    o.close();
   }}
}


import java.util.*;
import java.io.*;
public class Main{
   public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    PrintWriter o=new PrintWriter(System.out);
    int n=sc.nextInt();
    int a[]=new int[n];
    for (int i=0;i<n ;i++ ) {
      a[i]=sc.nextInt();
    }
    int left[]=new int[n];
    int right[]=new int[n];
    left[0]=0;
    right[n-1]=0;

    for(int i=1;i<n;i++)
    {

          left[i]=a[i-1]+left[i-1]+1-a[i];
          if(left[i]<0)
            left[i]=left[i-1];

    }
    for(int i=n-2;i>=0;i--)
    {

          right[i]=a[i+1]+right[i+1]+1-a[i];
        if(right[i]<0)
          right[i]=right[i+1];

    }
     for(int i=1;i<n;i++)
        left[i]=left[i]+left[i-1];

     for(int i=n-2;i>=0;i--)
          right[i]=right[i]+right[i+1];

   // for (int i=0;i<n ;i++ ) {
     // System.out.println(left[i]+" "+right[i]);
    //}
      int max=Integer.MAX_VALUE;
    if(n==1){
      System.out.println("0");
    }
    else{
    for(int i=0;i<n;i++)
    {
      int temp=0;
      if(i==0){
         temp=Math.min(left[0]+right[1],right[0]);
         //System.out.println(i+" "+temp);

         max=Math.min(temp,max);
      }
      else if(i==n-1){
        temp=Math.min(left[n-1],right[n-1]+left[n-2]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
      else{
        temp=Math.min(left[i]+right[i+1],left[i-1]+right[i]);
        //System.out.println(i+" "+temp);
        max=Math.min(temp,max);
      }
    }
    o.println(max);
    o.flush();
    o.close();
   }}
}


//package main;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int[] numbers = new int[size];
        for (int i = 0; i < size; i++) {
            numbers[i] = scanner.nextInt();
        }
        int left = 0, right = size - 1;
        int answer = 0;
        while (left < right - 1) {
            if (numbers[left] < numbers[right]) {
                if (numbers[left + 1] <= numbers[left]) {
                    answer += numbers[left] + 1 - numbers[left + 1];
                    numbers[left + 1] = numbers[left] + 1;
                }
                left++;
            } else {
                if (numbers[right - 1] <= numbers[right]) {
                    answer += numbers[right] + 1 - numbers[right - 1];
                    numbers[right - 1] = numbers[right] + 1;
                }
                right--;
            }
        }
        System.out.println(answer);
    }
}


//package main;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int[] numbers = new int[size];
        for (int i = 0; i < size; i++) {
            numbers[i] = scanner.nextInt();
        }
        int left = 0, right = size - 1;
        int answer = 0;
        while (left < right - 1) {
            if (numbers[left] < numbers[right]) {
                if (numbers[left + 1] <= numbers[left]) {
                    answer += numbers[left] + 1 - numbers[left + 1];
                    numbers[left + 1] = numbers[left] + 1;
                }
                left++;
            } else {
                if (numbers[right - 1] <= numbers[right]) {
                    answer += numbers[right] + 1 - numbers[right - 1];
                    numbers[right - 1] = numbers[right] + 1;
                }
                right--;
            }
        }
        if (numbers[left] == numbers[right] && left != right) {
            answer++;
        }
        System.out.println(answer);
    }
}


//package main;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        int[] numbers = new int[size];
        for (int i = 0; i < size; i++) {
            numbers[i] = scanner.nextInt();
        }
        int left = 0, right = size - 1;
        long answer = 0;
        while (left < right - 1) {
            if (numbers[left] < numbers[right]) {
                if (numbers[left + 1] <= numbers[left]) {
                    answer += numbers[left] + 1 - numbers[left + 1];
                    numbers[left + 1] = numbers[left] + 1;
                }
                left++;
            } else {
                if (numbers[right - 1] <= numbers[right]) {
                    answer += numbers[right] + 1 - numbers[right - 1];
                    numbers[right - 1] = numbers[right] + 1;
                }
                right--;
            }
        }
        if (numbers[left] == numbers[right] && left != right) {
            answer++;
        }
        System.out.println(answer);
    }
}


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

public class H {
	public static void main(String[] args) {
		FastScannerH sc = new FastScannerH(System.in);

		int N = sc.nextInt();
		int[] arr = new int[N + 2];
		arr[0] = Integer.MIN_VALUE;
		arr[N + 1] = Integer.MIN_VALUE;
		for(int i = 1; i<=N; i++) arr[i] = sc.nextInt();

		long[] cost_left = new long[N + 2];
		int[] max_left = new int[N + 2];
		long[] cost_right = new long[N + 2];
		int[] max_right = new int[N + 2];

		//left
		cost_left[0] = 0;
		max_left[0] = arr[0];
		for(int i = 1; i <= N; i++){
			max_left[i] = Math.max(max_left[i - 1] + 1, arr[i]);
			cost_left[i] = cost_left[i - 1] + max_left[i] - arr[i];
		}

		//right
		cost_right[N + 1] = 0;
		max_right[N + 1] = arr[N + 1];
		for(int i = N; i >= 1; i--){
			max_right[i] = Math.max(max_right[i + 1] + 1, arr[i]);
			cost_right[i] = cost_right[i + 1] + max_right[i] - arr[i];
		}

		long min_cost = Long.MAX_VALUE;
		for(int i = 1; i <= N; i++){
			long cost = cost_left[i - 1] + cost_right[i + 1] + Math.max(max_left[i], max_right[i]) - arr[i];
			min_cost = Math.min(min_cost, cost);
		}
		System.out.println(min_cost);
	}
}

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

    public FastScannerH(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 String next()
    {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do
        {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public double nextDouble() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        double res = 0;
        while (!isSpaceChar(c) && c != '.') {
            if (c == 'e' || c == 'E')
                return res * Math.pow(10, nextInt());
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        }
        if (c == '.') {
            c = read();
            double m = 1;
            while (!isSpaceChar(c)) {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                m /= 10;
                res += (c - '0') * m;
                c = read();
            }
        }
        return res * sgn;
    }

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

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

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

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


import java.util.*;
import java.io.*;
import java.math.BigInteger;

/**
 *
 * @author usquare
 */

public class ProblemB
{

    static int mod = (int) 1e9;
    static InputReader in;
    static PrintWriter out;

    static void solve()
    {

        in = new InputReader(System.in);
        out = new PrintWriter(System.out);

        int n = in.nextInt();
        long[] arr = new long[n + 2];
        long[] left = new long[n + 2];
        long[] lcount = new long[n + 2];
        long[] right = new long[n + 2];
        long[] rcount = new long[n + 2];

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

        for(int i = 1; i <= n; i++){
            left[i] = max(left[i - 1] + 1, arr[i]);
            lcount[i] = lcount[i - 1] - arr[i] + left[i];
        }

        for(int i = n ; i >= 1; i--){
            right[i] = max(right[i + 1] + 1, arr[i]);
            rcount[i] = rcount[i + 1] - arr[i] + right[i];
        }
        //debug(left);
        //debug(right);

        long ans = Long.MAX_VALUE;

        for(int i = 0; i <= n; i++)
            ans = min(ans, lcount[i] + rcount[i + 1]);

        out.println(ans);

        out.close();

    }

    public static void main(String[] args)
    {

        new Thread(null,new Runnable()
        {
            @Override
            public void run()
            {
                try{
                    solve();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        },"1",1<<26).start();
    }

    static class Pair implements Comparable<Pair>
    {

        int i;
        int x, y;

	Pair (int x,int y,int i)
        {
		this.x = x;
		this.y = y;
                this.i = i;
	}

        Pair(int x, int y){
            this.x = x;
            this.y = y;
        }

	public int compareTo(Pair o)
        {
            return Long.compare(this.i,o.i);
	}

        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 + " "+i;
        }

        public int hashCode()
        {
            return new Long(x).hashCode() * 31 + new Long(y).hashCode();
        }

    }

    static class Merge
    {

        public static void sort(int inputArr[])
        {
            int length = inputArr.length;
            doMergeSort(inputArr,0, length - 1);
        }

        private static void doMergeSort(int[] arr,int lowerIndex, int higherIndex)
        {
            if (lowerIndex < higherIndex)
            {
                int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
                doMergeSort(arr,lowerIndex, middle);
                doMergeSort(arr,middle + 1, higherIndex);
                mergeParts(arr,lowerIndex, middle, higherIndex);
            }
        }

        private static void mergeParts(int[]array,int lowerIndex, int middle, int higherIndex)
        {
            int[] temp=new int[higherIndex-lowerIndex+1];
            for (int i = lowerIndex; i <= higherIndex; i++)
            {
                temp[i-lowerIndex] = array[i];
            }
            int i = lowerIndex;
            int j = middle + 1;
            int k = lowerIndex;
            while (i <= middle && j <= higherIndex)
            {
                if (temp[i-lowerIndex] < temp[j-lowerIndex])
                {
                    array[k] = temp[i-lowerIndex];
                    i++;
                } else
                {
                    array[k] = temp[j-lowerIndex];
                    j++;
                }
                k++;
            }
            while (i <= middle)
            {
                array[k] = temp[i-lowerIndex];
                k++;
                i++;
            }
            while(j<=higherIndex)
            {
                array[k]=temp[j-lowerIndex];
                k++;
                j++;
            }
        }

    }

    static long add(long a,long b)
    {
        long x = a + b;
        if(x >= mod) x -= mod;
        return x;
    }

    static int add(int a,int b)
    {
        int x = a + b;
        if(x >= mod) x -= mod;
        return x;
    }
    static long sub(long a,long b)
    {
        long x= a - b ;
        if(x < 0) x += mod;
        return x;
    }

    static long mul(long a,long b)
    {
        a %= mod;
        b %= mod;
        long x = a*b;
        return x%mod;
    }

    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;
    }
    static String rev(String s)
    {
                StringBuilder sb=new StringBuilder(s);
                sb.reverse();
                return sb.toString();
    }

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

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

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

    static long mulmod(long  a,long b,long m)
    {
        if (m <= 1000000009) return a * b % m;

        long res = 0;
        while (a > 0)
        {
            if ((a&1)!=0)
            {
                res += b;
                if (res >= m) res -= m;
            }
            a >>= 1;
            b <<= 1;
            if (b >= m) b -= m;
        }
        return res;
    }

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

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

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

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

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

    static long pow(long n,long p,long m)
    {
         long  result = 1;
          if(p==0)
            return 1;

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

    static long pow(long n,long p)
    {
        long  result = 1;
          if(p==0)
            return 1;

        while(p!=0)
        {
            if(p%2==1)
                result *= n;
            p >>=1;
            n*=n;
        }
        return result;
    }

    static void debug(Object... o)
    {
            System.out.println(Arrays.deepToString(o));
    }

    static class InputReader
    {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

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

        public int read() {
                if (snumChars == -1)
                        throw new InputMismatchException();
                if (curChar >= snumChars) {
                        curChar = 0;
                        try {
                                snumChars = stream.read(buf);
                        } catch (IOException e) {
                                throw new InputMismatchException();
                        }
                        if (snumChars <= 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 {
                        res *= 10;
                        res += c - '0';
                        c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

        public long nextLong() {
                int c = read();
                while (isSpaceChar(c)) {
                        c = read();
                }
                int sgn = 1;
                if (c == '-') {
                        sgn = -1;
                        c = read();
                }
                long res = 0;
                do {
                        res *= 10;
                        res += c - '0';
                        c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

        public int[] nextIntArray(int n) {
                int a[] = new int[n];
                for (int i = 0; i < n; i++) {
                        a[i] = nextInt();
                }
                return a;
        }

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

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

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

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

    }
}


import java.util.*;
import java.io.*;
import java.math.BigInteger;

/**
 *
 * @author usquare
 */

public class ProblemB
{

    static int mod = (int) 1e9;
    static InputReader in;
    static PrintWriter out;

    static void solve()
    {

        in = new InputReader(System.in);
        out = new PrintWriter(System.out);

        int n = in.nextInt();
        long[] arr = new long[n + 2];
        long[] left = new long[n + 2];
        long[] lcount = new long[n + 2];
        boolean[] right = new boolean[n + 2];

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

        for(int i = 1; i <= n; i++){
            left[i] = max(left[i - 1] + 1, arr[i]);
            lcount[i] = lcount[i - 1] - arr[i] + left[i];
        }
        right[n + 1] = true;

        for(int i = n ; i >= 1; i--){
            right[i] = right[i + 1] & (arr[i] > arr[i + 1]);
        }

        long ans = Long.MAX_VALUE;

        for(int i = 0; i <= n; i++){
            if(right[i + 1]){
                long diff = max(arr[i + 1] - left[i] + 1, 0);
                ans = min(ans, diff * i  +  lcount[i]);
            }
        }

        out.println(ans);

        out.close();

    }

    public static void main(String[] args)
    {

        new Thread(null,new Runnable()
        {
            @Override
            public void run()
            {
                try{
                    solve();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        },"1",1<<26).start();
    }

    static class Pair implements Comparable<Pair>
    {

        int i;
        int x, y;

	Pair (int x,int y,int i)
        {
		this.x = x;
		this.y = y;
                this.i = i;
	}

        Pair(int x, int y){
            this.x = x;
            this.y = y;
        }

	public int compareTo(Pair o)
        {
            return Long.compare(this.i,o.i);
	}

        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 + " "+i;
        }

        public int hashCode()
        {
            return new Long(x).hashCode() * 31 + new Long(y).hashCode();
        }

    }

    static class Merge
    {

        public static void sort(int inputArr[])
        {
            int length = inputArr.length;
            doMergeSort(inputArr,0, length - 1);
        }

        private static void doMergeSort(int[] arr,int lowerIndex, int higherIndex)
        {
            if (lowerIndex < higherIndex)
            {
                int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
                doMergeSort(arr,lowerIndex, middle);
                doMergeSort(arr,middle + 1, higherIndex);
                mergeParts(arr,lowerIndex, middle, higherIndex);
            }
        }

        private static void mergeParts(int[]array,int lowerIndex, int middle, int higherIndex)
        {
            int[] temp=new int[higherIndex-lowerIndex+1];
            for (int i = lowerIndex; i <= higherIndex; i++)
            {
                temp[i-lowerIndex] = array[i];
            }
            int i = lowerIndex;
            int j = middle + 1;
            int k = lowerIndex;
            while (i <= middle && j <= higherIndex)
            {
                if (temp[i-lowerIndex] < temp[j-lowerIndex])
                {
                    array[k] = temp[i-lowerIndex];
                    i++;
                } else
                {
                    array[k] = temp[j-lowerIndex];
                    j++;
                }
                k++;
            }
            while (i <= middle)
            {
                array[k] = temp[i-lowerIndex];
                k++;
                i++;
            }
            while(j<=higherIndex)
            {
                array[k]=temp[j-lowerIndex];
                k++;
                j++;
            }
        }

    }

    static long add(long a,long b)
    {
        long x = a + b;
        if(x >= mod) x -= mod;
        return x;
    }

    static int add(int a,int b)
    {
        int x = a + b;
        if(x >= mod) x -= mod;
        return x;
    }
    static long sub(long a,long b)
    {
        long x= a - b ;
        if(x < 0) x += mod;
        return x;
    }

    static long mul(long a,long b)
    {
        a %= mod;
        b %= mod;
        long x = a*b;
        return x%mod;
    }

    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;
    }
    static String rev(String s)
    {
                StringBuilder sb=new StringBuilder(s);
                sb.reverse();
                return sb.toString();
    }

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

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

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

    static long mulmod(long  a,long b,long m)
    {
        if (m <= 1000000009) return a * b % m;

        long res = 0;
        while (a > 0)
        {
            if ((a&1)!=0)
            {
                res += b;
                if (res >= m) res -= m;
            }
            a >>= 1;
            b <<= 1;
            if (b >= m) b -= m;
        }
        return res;
    }

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

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

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

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

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

    static long pow(long n,long p,long m)
    {
         long  result = 1;
          if(p==0)
            return 1;

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

    static long pow(long n,long p)
    {
        long  result = 1;
          if(p==0)
            return 1;

        while(p!=0)
        {
            if(p%2==1)
                result *= n;
            p >>=1;
            n*=n;
        }
        return result;
    }

    static void debug(Object... o)
    {
            System.out.println(Arrays.deepToString(o));
    }

    static class InputReader
    {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

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

        public int read() {
                if (snumChars == -1)
                        throw new InputMismatchException();
                if (curChar >= snumChars) {
                        curChar = 0;
                        try {
                                snumChars = stream.read(buf);
                        } catch (IOException e) {
                                throw new InputMismatchException();
                        }
                        if (snumChars <= 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 {
                        res *= 10;
                        res += c - '0';
                        c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

        public long nextLong() {
                int c = read();
                while (isSpaceChar(c)) {
                        c = read();
                }
                int sgn = 1;
                if (c == '-') {
                        sgn = -1;
                        c = read();
                }
                long res = 0;
                do {
                        res *= 10;
                        res += c - '0';
                        c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

        public int[] nextIntArray(int n) {
                int a[] = new int[n];
                for (int i = 0; i < n; i++) {
                        a[i] = nextInt();
                }
                return a;
        }

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

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

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

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

    }
}


import java.util.*;
import java.io.*;
import java.math.BigInteger;

/**
 *
 * @author usquare
 */

public class ProblemB
{

    static int mod = (int) 1e9;
    static InputReader in;
    static PrintWriter out;

    static void solve()
    {

        in = new InputReader(System.in);
        out = new PrintWriter(System.out);

        int n = in.nextInt();
        long[] arr = new long[n + 2];
        long[] left = new long[n + 2];
        long[] lcount = new long[n + 2];
        long[] right = new long[n + 2];
        long[] rcount = new long[n + 2];

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

        for(int i = 1; i <= n; i++){
            left[i] = max(left[i - 1] + 1, arr[i]);
            lcount[i] = lcount[i - 1] - arr[i] + left[i];
        }

        for(int i = n ; i >= 1; i--){
            right[i] = max(right[i + 1] + 1, arr[i]);
            rcount[i] = rcount[i + 1] - arr[i] + right[i];
        }
        //debug(left);
        //debug(right);

        long ans = Long.MAX_VALUE;

        for(int i = 0; i <= n; i++){
            long diff = max(right[i + 1] - left[i] + 1, 0);
            ans = min(ans, lcount[i] + rcount[i + 1] + diff * i);
        }

        out.println(ans);

        out.close();

    }

    public static void main(String[] args)
    {

        new Thread(null,new Runnable()
        {
            @Override
            public void run()
            {
                try{
                    solve();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        },"1",1<<26).start();
    }

    static class Pair implements Comparable<Pair>
    {

        int i;
        int x, y;

	Pair (int x,int y,int i)
        {
		this.x = x;
		this.y = y;
                this.i = i;
	}

        Pair(int x, int y){
            this.x = x;
            this.y = y;
        }

	public int compareTo(Pair o)
        {
            return Long.compare(this.i,o.i);
	}

        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 + " "+i;
        }

        public int hashCode()
        {
            return new Long(x).hashCode() * 31 + new Long(y).hashCode();
        }

    }

    static class Merge
    {

        public static void sort(int inputArr[])
        {
            int length = inputArr.length;
            doMergeSort(inputArr,0, length - 1);
        }

        private static void doMergeSort(int[] arr,int lowerIndex, int higherIndex)
        {
            if (lowerIndex < higherIndex)
            {
                int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
                doMergeSort(arr,lowerIndex, middle);
                doMergeSort(arr,middle + 1, higherIndex);
                mergeParts(arr,lowerIndex, middle, higherIndex);
            }
        }

        private static void mergeParts(int[]array,int lowerIndex, int middle, int higherIndex)
        {
            int[] temp=new int[higherIndex-lowerIndex+1];
            for (int i = lowerIndex; i <= higherIndex; i++)
            {
                temp[i-lowerIndex] = array[i];
            }
            int i = lowerIndex;
            int j = middle + 1;
            int k = lowerIndex;
            while (i <= middle && j <= higherIndex)
            {
                if (temp[i-lowerIndex] < temp[j-lowerIndex])
                {
                    array[k] = temp[i-lowerIndex];
                    i++;
                } else
                {
                    array[k] = temp[j-lowerIndex];
                    j++;
                }
                k++;
            }
            while (i <= middle)
            {
                array[k] = temp[i-lowerIndex];
                k++;
                i++;
            }
            while(j<=higherIndex)
            {
                array[k]=temp[j-lowerIndex];
                k++;
                j++;
            }
        }

    }

    static long add(long a,long b)
    {
        long x = a + b;
        if(x >= mod) x -= mod;
        return x;
    }

    static int add(int a,int b)
    {
        int x = a + b;
        if(x >= mod) x -= mod;
        return x;
    }
    static long sub(long a,long b)
    {
        long x= a - b ;
        if(x < 0) x += mod;
        return x;
    }

    static long mul(long a,long b)
    {
        a %= mod;
        b %= mod;
        long x = a*b;
        return x%mod;
    }

    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;
    }
    static String rev(String s)
    {
                StringBuilder sb=new StringBuilder(s);
                sb.reverse();
                return sb.toString();
    }

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

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

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

    static long mulmod(long  a,long b,long m)
    {
        if (m <= 1000000009) return a * b % m;

        long res = 0;
        while (a > 0)
        {
            if ((a&1)!=0)
            {
                res += b;
                if (res >= m) res -= m;
            }
            a >>= 1;
            b <<= 1;
            if (b >= m) b -= m;
        }
        return res;
    }

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

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

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

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

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

    static long pow(long n,long p,long m)
    {
         long  result = 1;
          if(p==0)
            return 1;

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

    static long pow(long n,long p)
    {
        long  result = 1;
          if(p==0)
            return 1;

        while(p!=0)
        {
            if(p%2==1)
                result *= n;
            p >>=1;
            n*=n;
        }
        return result;
    }

    static void debug(Object... o)
    {
            System.out.println(Arrays.deepToString(o));
    }

    static class InputReader
    {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

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

        public int read() {
                if (snumChars == -1)
                        throw new InputMismatchException();
                if (curChar >= snumChars) {
                        curChar = 0;
                        try {
                                snumChars = stream.read(buf);
                        } catch (IOException e) {
                                throw new InputMismatchException();
                        }
                        if (snumChars <= 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 {
                        res *= 10;
                        res += c - '0';
                        c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

        public long nextLong() {
                int c = read();
                while (isSpaceChar(c)) {
                        c = read();
                }
                int sgn = 1;
                if (c == '-') {
                        sgn = -1;
                        c = read();
                }
                long res = 0;
                do {
                        res *= 10;
                        res += c - '0';
                        c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

        public int[] nextIntArray(int n) {
                int a[] = new int[n];
                for (int i = 0; i < n; i++) {
                        a[i] = nextInt();
                }
                return a;
        }

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

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

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

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

    }
}


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

public class Main {
    private FastScanner in;
    private PrintWriter out;

    private void solve() throws IOException {
        int n = in.nextInt();
        long a[] = new long[n];
        for (int i = 0; i < n; i++) a[i] = in.nextLong();

        long k = 0;
        int l = 0, r = --n;

        while (l < n && a[l + 1] > a[l]) l++;
        while (r > 1 && a[r - 1] > a[r]) r--;

        long al = a[l], ar = a[r];
        //out.println(k + " " + l + " " + al + " " + r + " " + ar);

        while (l + 1 < r) {
            if (al < ar) k += (++al) - a[++l];
            else if (al > ar) k += (++ar) - a[--r];
            else if (al + a[r - 1] < ar + a[l + 1]) k += (++al) - a[++l];
            else k += (++ar) - a[--r];

            while (l < n && a[l + 1] > al) al = a[++l];
            while (r > 1 && a[r - 1] > ar) ar = a[--r];

            //out.println(k + " " + l + " " + al + " " + r + " " + ar);
        }
        if (l < r && al == ar) k++;
        out.println(k);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

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

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

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean ready() throws IOException {
            return br.ready();
        }
    }

    private void run() throws IOException {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.flush();
        out.close();
    }

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


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

import static java.lang.Math.max;
import static java.lang.Math.min;

public class Main {
    private FastScanner in;
    private PrintWriter out;

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

        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextLong();

        long[] sumL = new long[n], l = new long[n];
        sumL[0] = 0;
        l[0] = a[0];
        for (int i = 1; i < n; i++) {
            l[i] = max(l[i - 1] + 1, a[i]);
            sumL[i] = sumL[i - 1] + (l[i] - a[i]);
        }

        long[] sumR = new long[n], r = new long[n];
        sumR[n - 1] = 0;
        r[n - 1] = a[n - 1];
        for (int i = n - 2; i >= 0; i--) {
            r[i] = max(r[i + 1] + 1, a[i]);
            sumR[i] = sumR[i + 1] + (r[i] - a[i]);
        }

        long ans = min(sumL[n - 1], sumR[0]);
        for (int i = 1; i + 1 < n; i++)
            ans = min(ans, sumL[i - 1] + min(sumR[i] - sumR[i + 1], sumL[i] - sumL[i - 1]) + sumR[i + 1]);

        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

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

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

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean ready() throws IOException {
            return br.ready();
        }

    }

    private void run() throws IOException {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.flush();
        out.close();
    }

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


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

public class Main {
    private FastScanner in;
    private PrintWriter out;

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

        long a[] = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextLong();

        long ans = 0;
        int l = 0, r = n - 1;
        while (l + 1 < n && a[l + 1] > a[l])
            l++;
        while (r - 1 > 0 && a[r - 1] > a[r])
            r--;

        while (l < r) {
            if (a[l] < a[r]) {
                ans += (a[l] + 1) - a[l + 1];
                a[l + 1] = a[l] + 1;
            } else if (a[l] > a[r]) {
                ans += (a[r] + 1) - a[r - 1];
                a[r - 1] = a[r] + 1;
            } else /* a[l] == a[r] */ {
                if (a[l] - a[l + 1] < a[r] - a[r - 1]) {
                    ans += (a[l] + 1) - a[l + 1];
                    a[l + 1] = a[l] + 1;
                } else {
                    ans += (a[r] + 1) - a[r - 1];
                    a[r - 1] = a[r] + 1;
                }
            }

            while (l + 1 < n && a[l + 1] > a[l])
                l++;
            while (r - 1 > 0 && a[r - 1] > a[r])
                r--;

            if (l + 1 == n) break;
            if (r == 1) break;
        }

        if (l < r && a[l] == a[r])
            ans++;

        out.println(ans);
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

        FastScanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

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

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

        String nextLine() throws IOException {
            return br.readLine();
        }

        boolean ready() throws IOException {
            return br.ready();
        }

    }

    private void run() throws IOException {
        in = new FastScanner(System.in);
        out = new PrintWriter(System.out);

        solve();

        out.flush();
        out.close();
    }

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