#include "bits/stdc++.h"
using namespace std;

int main() {
	int N;
	cin >> N;
	vector<int> S(N), T(N);
	vector<long long> U(N), V(N);
	vector<vector<int> > X(N, vector<int>(64)), Y(N, vector<int>(64));
	for (int i = 0; i < N; i++) cin >> S[i];
	for (int i = 0; i < N; i++) cin >> T[i];
	for (int i = 0; i < N; i++) cin >> U[i];
	for (int i = 0; i < N; i++) cin >> V[i];
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < 64; j++) {
			X[i][j] = U[i] % 2;
			U[i] >>= 1;
			Y[i][j] = V[i] % 2;
			V[i] >>= 1;
		}
	}
	vector<vector<vector<int> > > ANS(N, vector<vector<int> >(N, vector<int>(64, -1)));
	vector<vector<vector<bool> > > B(2, vector<vector<bool> >(N, vector<bool>(64, false)));
	for (int i = 0; i < N; i++) {
		if (S[i] == 0) {
			for (int j = 0; j < 64; j++) {
				if (X[i][j] == 1) {
					for (int k = 0; k < N; k++) {
						ANS[i][k][j] = 1;
					}
					B[0][i][j] = true;
				}
			}
		}
		if (T[i] == 0) {
			for (int j = 0; j < 64; j++) {
				if (Y[i][j] == 1) {
					for (int k = 0; k < N; k++) {
						ANS[k][i][j] = 1;
					}
					B[1][i][j] = true;
				}
			}
		}
	}
	for (int i = 0; i < N; i++) {
		if (S[i] == 1) {
			for (int j = 0; j < 64; j++) {
				if (X[i][j] == 0) {
					for (int k = 0; k < N; k++) {
						if (ANS[i][k][j] == 1) {
							cout << -1 << endl;
							return 0;
						}
						ANS[i][k][j] = 0;
					}
					B[0][i][j] = true;
				}
			}
		}
		if (T[i] == 1) {
			for (int j = 0; j < 64; j++) {
				if (Y[i][j] == 0) {
					for (int k = 0; k < N; k++) {
						if (ANS[k][i][j] == 1) {
							cout << -1 << endl;
							return 0;
						}
						ANS[i][k][j] = 0;
					}
					B[1][i][j] = true;
				}
			}
		}
	}
	for (int i = 0; i < 64; i++) {
		vector<vector<int> > Z(N, vector<int>(N));
		for (int j = 0; j < N; j++) {
			for (int k = 0; k < N; k++) {
				Z[j][k] = ANS[j][k][i];
			}
		}
		for (int j = 0; j < N; j++) {
			if (S[j] == 0 && X[j][i] == 0) {
				bool check = true;
				for (int k = 0; k < N; k++) {
					if (Z[j][k] == 0) check = false;
				}
				if (check) {
					cout << -1 << endl;
					return 0;
				}
			}
			if (S[j] == 1 && X[j][i] == 1) {
				bool check = true;
				for (int k = 0; k < N; k++) {
					if (Z[j][k] == 1) check = false;
				}
				if (check) {
					cout << -1 << endl;
					return 0;
				}
			}
			if (T[j] == 0 && Y[j][i] == 0) {
				bool check = true;
				for (int k = 0; k < N; k++) {
					if (Z[k][j] == 0) check = false;
				}
				if (check) {
					cout << -1 << endl;
					return 0;
				}
			}
			if (T[j] == 1 && Y[j][i] == 1) {
				bool check = true;
				for (int k = 0; k < N; k++) {
					if (Z[k][j] == 1) check = false;
				}
				if (check) {
					cout << -1 << endl;
					return 0;
				}
			}
		}
	}
}