import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;


struct FenwickTree(T) {
    private {
        int _size;
        T[] _data;
    }

    this(int N) {
        _size = N;
        _data = new T[](_size + 1);
    }

    void add(int i, T x) {
        i++;
        while (i <= _size) {
            _data[i] += x;
            i += i & (-i);
        }
    }

    T sum(int r) {
        T res = 0;
        while (r > 0) {
            res += _data[r];
            r -= r & (-r);
        }
        return res;
    }
}

unittest {
    auto ft = FenwickTree!(int)(10);
    ft.add(0, 3);
    ft.add(2, 4);
    ft.add(5, 1);
    assert(ft.sum(0) == 0);
    assert(ft.sum(1) == 3);
    assert(ft.sum(2) == 3);
    assert(ft.sum(3) == 7);
    assert(ft.sum(5) == 7);
    assert(ft.sum(6) == 8);
}

void main() {
    int N;
    scan(N);
    auto a = readln.split.to!(int[]);
    auto b = readln.split.to!(int[]);

    foreach (i ; 0 .. N) {
        if (a[i] != b[i] && b[i] >= (a[i] + 1) / 2) {
            writeln(-1);
            return;
        }
    }

    auto d = new long[][](50 + 1, 50 + 1);
    fillAll(d, infl);
    foreach (i ; 0 .. 51) d[i][i] = 0;
    foreach (i ; 1 .. 51) {
        foreach (j ; 1 .. i + 1) {
            chmin(d[i][i % j], 1L << j);
        }
    }

    debug {
        writefln("%(%s\n%)", d);
    }

    foreach (i ; 0 .. 51) {
        foreach (j ; 0 .. 51) {
            foreach (k ; 0 .. 51) {
                chmin(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }

    auto s = new long[](N);
    foreach (i ; 0 .. N) {
        s[i] = 1L << a[i];
    }

    auto f = new bool[](N);

    long ans;

    foreach (_ ; 0 .. N) {
        int im;
        long cm;

        foreach (i ; 0 .. N) {
            if (f[i]) continue;
            long cst = infl;
            foreach (j ; 0 .. 51) {
                if (s[i] & 1L << j) {
                    chmin(cst, d[j][b[i]]);
                }
            }
            if (cm < cst) {
                cm = cst;
                im = i;
            }
        }

        f[im] = true;
        ans |= cm;

        debug {
            writefln("i: %s, cm: %08b", im, cm);
            writefln("%(%020b %)", s);
        }

        foreach (i ; 0 .. N) {
            foreach (j ; 0 .. 51) if (s[i] & 1L << j) {
                foreach (k ; 1 .. 51) if (cm & 1L << k) {
                    s[i] |= 1L << (j % k);
                }
            }
        }
    }

    writeln(ans);
}






void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x > arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x < arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

void yes(bool ok, string y = "Yes", string n = "No") {
    return writeln(ok ? y : n);
}


Measured memory_bytes: 256