import java.lang.Long.highestOneBit
import java.util.*

fun main() {
  val n = readLine()!!.toInt()
  val a = readLine()!!.split(' ').map { it.toLong() }
  check(a.size == n)
  println(solve(a))
}

private fun solve(a: List<Long>): Long {
  val xor = a.drop(2).fold(0L, Long::xor)
  val sum = a[0] + a[1]
  if (xor % 2 != sum % 2) return -1
  val and = (sum - xor) / 2
  if (and and xor != 0L) return -1
  if (and > a[0]) return -1
  var res = and
  var bit = highestOneBit(xor)
  while (bit > 0) {
    if (res + bit <= a[0]) res += bit
    bit = highestOneBit(xor and (bit - 1))
  }
  return if (res > 0) a[0] - res else -1
}