#include <bits/stdc++.h>
using namespace std;
const char *FILE_IN = "simple.in";
const char *FILE_OUT = "simple.out";
const int MAX_N = 270000;
unordered_map<long long, int> a;
unordered_map<long long, int> b;
vector<long long> cnt_a[MAX_N];
vector<long long> cnt_b[MAX_N];
vector<pair<int, int> > sz_a;
vector<pair<int, int> > sz_b;
long long mem_a[MAX_N];
void read_multimap(int n, unordered_map<long long, int> &m) {
for (int i = 0; i < n; i++) {
long long x; scanf("%I64d", &x);
m[x] += 1;
}
}
void create_histogram(int n, const unordered_map<long long, int> &m, vector<long long> cnt_m[]) {
for (auto p : m) {
cnt_m[p.second].push_back(p.first);
}
}
vector<pair<int, int> > get_sizes(const vector<long long> cnt_m[]) {
vector<pair<int, int> > result;
for (int i = 0; i < MAX_N; i++) {
if (cnt_m[i].size()) {
result.push_back({i, cnt_m[i].size()});
}
}
return result;
}
void solve() {
time_t start = clock();
int n; scanf("%d", &n);
read_multimap(n, a);
read_multimap(n, b);
create_histogram(n, a, cnt_a);
create_histogram(n, b, cnt_b);
sz_a = get_sizes(cnt_a);
sz_b = get_sizes(cnt_b);
if (sz_a != sz_b) {
cout << -1 << "\n";
exit(0);
}
int h = 0;
for (auto p : a) {
for (int j = 0; j < p.second; j++) {
mem_a[h++] = p.first;
}
}
long long result = -1;
int len = sz_a[0].first;
for (int i = 1; i < sz_a.size(); i++) {
if (cnt_a[len].size() > sz_a[i].second) {
len = sz_a[i].first;
}
}
len = sz_a[rand() % sz_a.size()].first;
random_shuffle(mem_a, mem_a + h);
long long value = mem_a[0];
len = a[value];
random_shuffle(cnt_b[len].begin(), cnt_b[len].end());
random_shuffle(sz_a.begin(), sz_a.end());
vector<long long> candidates;
for (int i = 0; i < cnt_b[len].size(); i++) {
candidates.push_back(value ^ cnt_b[len][i]);
}
sort(candidates.begin(), candidates.end());
for (long long x : candidates) {
if (1.0 * (clock() - start) / CLOCKS_PER_SEC > 1.40) {
// cout << i << " of " << cnt_b[len].size() << "\n";
break;
}
bool flag = true;
for (int j = 0; j < sz_a.size(); j++) {
int l = sz_a[j].first;
int cnt = sz_a[j].second;
for (int k = 0; k < cnt; k++) {
auto it = b.find(cnt_a[l][k] ^ x);
if (it == b.end() || it->second != a[cnt_a[l][k]]) {
flag = false;
break;
}
}
if (!flag) {
break;
}
}
if (flag) {
result = x;
break;
}
}
printf("%I64d\n", result);
}
int main() {
srand(8132);
#ifndef PENGUINS
freopen(FILE_IN, "r", stdin);
freopen(FILE_OUT, "w", stdout);
#endif
int cases = 1;
for (int i = 0; i < cases; i++) {
solve();
}
return 0;
}