// https://atcoder.jp/contests/agc022/tasks/agc022_c
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

fn solve(mut a: Vec<usize>, b: Vec<usize>) -> i64 {
    let n = a.len();
    for i in 0..n {
        if a[i] != b[i] && b[i] != 0 && (a[i] + 1) / 2 <= b[i] {
            return -1;
        }
    }

    let mut table = dvec!(0; 51, 51, 51);
    for l in 0..51 {
        for f in 0..51 {
            for t in 0..51 {
                if f == t {
                    table[f][t][l] = 1;
                } else if (f + 1) / 2 <= t {
                    table[f][t][l] = 0;
                } else if l >= 1 {
                    let mut ok = table[f][t][l-1];
                    for w in 1..l+1 {
                        let tf = f%w;
                        ok = max(ok, table[tf][t][l-1]);
                    }
                    table[f][t][l] = ok;
                }
            }
        }
    }

    let INF = 60;
    let mut limit = dvec!(INF; 51, 51);
    for f in 0..51 {
        for t in 0..51 {
            for l in 0..51 {
                if table[f][t][l] == 1 {
                    limit[f][t] = l;
                    break;
                }
            }
        }
    }

    let mut aset = vec![HashSet::new(); n];
    for i in 0..n {
        aset[i].insert(a[i]);
    }

    let mut ans = 0;
    for l in (1..51).rev() {
        let mut has_limit = false;
        for i in 0..n {
            if aset[i].contains(&b[i]) {
                continue;
            }
            let mut ok = false;
            for &aa in &aset[i] {
                if limit[aa][b[i]] < l {
                    ok = true;
                    break;
                }
            }
            if !ok {
                has_limit = true;
                break;
            }
        }
        if has_limit {
            ans += 1<<l;
            for i in 0..n {
                if aset[i].contains(&b[i]) {
                    continue;
                }
                let mut newa = aset[i].clone();
                for &aa in &aset[i] {
                    newa.insert(aa%l);
                }
                aset[i] = newa;
            }
        }
    }
    ans
}

fn main() {
    input! {
        n: usize,
        a: [usize; n],
        b: [usize; n]
    };

    println!("{}", solve(a, b));
}


Measured memory_bytes: 4352