Ivan has got an array of n non-negative integers a1, a2, ..., an. Ivan knows that the array is sorted in the non-decreasing order. 

Ivan wrote out integers 2a1, 2a2, ..., 2an on a piece of paper. Now he wonders, what minimum number of integers of form 2b (b ≥ 0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2v - 1 for some integer v (v ≥ 0). 

Help Ivan, find the required quantity of numbers.

Input

The first line contains integer n (1 ≤ n ≤ 105). The second input line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 2·109). It is guaranteed that a1 ≤ a2 ≤ ... ≤ an.

Output

Print a single integer — the answer to the problem.

Examples

Input

4
0 1 1 1


Output

0


Input

1
3


Output

3

Note

In the first sample you do not need to add anything, the sum of numbers already equals 23 - 1 = 7.

In the second sample you need to add numbers 20, 21, 22.
import java.util.*;
import java.io.*;

public class Main implements Runnable {
		
	public void solve() throws IOException {
		NavigableMap<Integer, Integer> nmap = new TreeMap<Integer, Integer>();
                int N = nextInt();
                for(int i = 0; i < N; i++){
                    int n = nextInt();
                    Integer curVal = nmap.get(n);
                    if(curVal == null) nmap.put(n, 1);
                    else nmap.put(n, curVal + 1);
                }
                
                for(Integer thisValue = nmap.firstKey(); thisValue != null; thisValue = nmap.higherKey(thisValue)){
                    int cur = nmap.get(thisValue);
                    if(cur == 1) continue;
                    
                    int giveNext = cur / 2;
                    int withMe = cur % 2;
                    
                    if(withMe == 0) nmap.remove(thisValue);
                    else nmap.put(thisValue, 1);
                    
                    
                    Integer alreadyNext = nmap.get(thisValue + 1);
                    if(alreadyNext == null) nmap.put(thisValue + 1, giveNext);
                    else nmap.put(thisValue + 1, alreadyNext + giveNext);
                    
                }
                
                out.println(nmap.lastKey() + 1 - nmap.size());
	}

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

        public void debug(Object... arr){
            System.out.println(Arrays.deepToString(arr));
        }

        public void print1Int(int[] a){
                for(int i = 0; i < a.length; i++)
                        System.out.print(a[i] + " ");
                System.out.println();
        }
        
        public void print2Int(int[][] a){
                for(int i = 0; i < a.length; i++){
                        for(int j = 0; j < a[0].length; j++){
                                System.out.print(a[i][j] + " ");
                        }
                        System.out.println();
                }
        }
        
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
                        out = new PrintWriter(System.out);
			tok = null;
			solve();
			in.close();
                        out.close();
		} catch (IOException e) {
			System.exit(0);
		}
	}

	public String nextToken() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

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

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

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
        PrintWriter out;
	BufferedReader in;
	StringTokenizer tok;
}