using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var n=sc.I;
		DateTime st=DateTime.Now;
		var a=sc.La;
		long p=0,d=0;
		for(int i = 0;i<n;i++) {
			p^=a[i];
			if(i>1){d^=a[i];}
		}
		int m=Binary(d,0).Length;
		var b0=a[0];
		var b1=a[1];
		var td=Binary(d,m);
		long ans=0;
		if(d==0){
			if((a[0]-a[1])%2==0&&a[0]>=a[1]){
				ans=(a[0]-a[1])/2;
				if(a[0]-ans>0){
					Console.WriteLine(ans);
					return;
				}
			}
			Console.WriteLine(-1);
			return;
		}
		while(true){
			var t0=Binary(b0,m);
			var t1=Binary(b1,m);
			int mm=Max(td.Length,Max(t0.Length,t1.Length));
			if(b0<=0){
				Console.WriteLine(-1);
				return;
			}
			if((b0^b1^d)==0){
				Console.WriteLine(ans);
				return;
			}
			for(int i = 0;i<mm;i++) {
				long z=1L<<(mm-i-1);
				if((d&z)!=0){
					if(((b0^b1)&z)==0){
						var x=(b0&(z-1))+1;
						x=Min(x,(z-((z-1)&b1)));
						b0-=x;
						b1+=x;
						ans+=x;
						break;
					}
				}
				else{
					if(((b0^b1)&z)!=0){
						var x=(b0&(z-1))+1;
						x=Min(x,(z-((z-1)&b1)));
						b0-=x;
						b1+=x;
						ans+=x;
						break;
					}
				}
			}
		}
	}
	static string Binary(long a,int b){
		string s=Convert.ToString(a,2);
		return (b>s.Length?new string('0',b-s.Length):"")+s;
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}

Measured memory_bytes: 29948