const val INF = 1L shl 60

fun main(args: Array<String>) {
  val n = readLine()!!.toInt()
  val xs = readLine()!!.split(" ").map(String::toInt)
  val ys = readLine()!!.split(" ").map(String::toInt)

  var currentCost = 0L
  while (true) {
    // dp[i][v]: Min cost to make xs[i] to v.
    val dp = Array(n, { LongArray(51, { INF }) })
    for (i in 0 until n) {
      dp[i][xs[i]] = 0L
    }

    for (k in 50 downTo 1) {
      for (i in 0 until n) {
        for (v in 0..50) {
          if (dp[i][v] < INF) {
            if (currentCost and (1L shl k) == (1L shl k)) {
              dp[i][v % k] = Math.min(dp[i][v % k], dp[i][v])
            } else {
              dp[i][v % k] = Math.min(dp[i][v % k], dp[i][v] + (1L shl k))
            }
          }
        }
      }
    }
    var maxCost = 0L
    for (i in 0 until n) {
      maxCost = Math.max(maxCost, dp[i][ys[i]])
    }
    if (maxCost == INF) {
      println(-1)
      return
    }
    if (maxCost == 0L) {
      println(currentCost)
      return
    }
    currentCost = currentCost or java.lang.Long.highestOneBit(maxCost)
  }
}
