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.io.*;
import java.util.*;
public final class powers_of_two
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static Map<Integer,Integer> m1=new HashMap<Integer,Integer>();
	
	static int get(int val)
	{
		return ((m1.get(val)==null)?0:m1.get(val));
	}
	
	static void put(int num,int val)
	{
		if(m1.get(num)==null)
		{
			m1.put(num,1);
		}
		else
		{
			m1.put(num,m1.get(num)+val);
		}
	}
	
	public static void main(String args[]) throws Exception
	{
		int n=sc.nextInt();
		for(int i=0;i<n;i++)
		{
			int val=sc.nextInt();put(val,1);
			while(get(val)==2)
			{
				put(val,-2);
				put(val+1,1);
				val++;
			}
		}
		//out.println(m1);
		int max=-1;
		for(Map.Entry<Integer,Integer> curr:m1.entrySet())
		{
			int key=curr.getKey(),val=curr.getValue();
			if(val>0)
			{
				max=Math.max(max,key);
			}
		}
		int res=0;
		for(Map.Entry<Integer,Integer> curr:m1.entrySet())
		{
			int key=curr.getKey(),val=curr.getValue();
			if(key<=max && val>0)
			{
				res++;
			}
		}
		out.println(max+1-res);out.close();
	}
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }
	
    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
	
	public String next() throws Exception {
		return nextToken().toString();
	}
	
    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

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

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}