#include<bits/stdc++.h>
#include<bits/extc++.h>

using namespace std;

#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define go(x, i) for(register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define FOR(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define debug(x) cout << #x << " = " << x << endl
#define mem(a, b) memset(a, b, sizeof(a))
#define cpy(a, b) memcpy(a, b, sizeof(a))
#define min(a, b) (a < b ? a : b)
#define max(a, b) (b < a ? a : b)
#define inf (0x3f3f3f3f)
#define INF (1e18)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define y1 orzorz


typedef unsigned long long ull;
typedef unsigned int uint;
typedef long long ll;
typedef pair<ll, int> PLI;
typedef pair<int, int> PII;
typedef long double ldb;
typedef double db;

template<class T>inline bool chkmax(T &_, T __) {return _ < __ ? _ = __, 1 : 0;}
template<class T>inline bool chkmin(T &_, T __) {return _ > __ ? _ = __, 1 : 0;}

inline void procStatus() {
	ifstream t("/proc/self/status");
	cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>());
}

const int N = 1e5 + 10;

vector<int> want[N];
int ans, n, a[N], b[N], now[N];

bool nosolution() {
	int tmpa[N], tmpb[N];
	For(i, 0, n) tmpa[i] = a[i], tmpb[i] = b[i];
	sort(tmpa, tmpa + n + 1), sort(tmpb, tmpb + n + 1);
	For(i, 0, n) if(tmpa[i] ^ tmpb[i]) return true;
	For(i, 0, n) 
		a[i] = lower_bound(tmpa, tmpa + n + 1, a[i]) - tmpa + 1, 
		b[i] = lower_bound(tmpb, tmpb + n + 1, b[i]) - tmpb + 1;
	return false;
}

void work() {
	for(; !want[now[0]].empty(); ++ ans) {
		int pos = want[now[0]].back();
		want[now[0]].pop_back();
		swap(now[0], now[pos]);
	}
}

int main() {
	//file("duliu");

	scanf("%d", &n);
	For(i, 1, n) scanf("%d", &a[i]), a[0] ^= a[i];
	For(i, 1, n) scanf("%d", &b[i]), b[0] ^= b[i];

	if(nosolution()) return puts("-1"), 0;

	For(i, 1, n) {
		if(a[i] ^ b[i]) want[b[i]].pb(i);
		now[i] = a[i];
	}

	//For(i, 1, n) cout << now[i] << ' '; cout << endl;
	//For(i, 1, n) cout << b[i] << ' '; cout << endl;

	now[0] = a[0], work();

	//For(i, 1, n) cout << now[i] << ' '; cout << endl;
	//For(i, 1, n) cout << b[i] << ' '; cout << endl;

	For(i, 1, n) while((now[i] ^ b[i]) && !want[now[i]].empty()) 
		swap(now[i], now[0]), ++ ans, work();

	cout << ans << endl;

	return 0;
}
