#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <iostream>
#include <string.h>
#include <set>
#include <algorithm>
#include <cmath>
#include <map>
#include <string>
using namespace std;

typedef double LD;
typedef long long LL;
typedef pair<int, int> PII;
#define MP make_pair
#define PB push_back
#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, t) FOR(i,0,t)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())

const LL MOD = 1000000007;
const int INF = 1000000007;
const LL LINF = 1LL * INF * INF;
const int MAXN = 200007;
const LD EPS = 1e-7;

int A[MAXN], B[MAXN], CA[MAXN], CB[MAXN], ans = 0;
vector <int> C, G[MAXN];
map <int, int> M;
bool used[MAXN], V[MAXN];

void dfs(int v)
{
	used[v] = true;
	REP(i, SZ(G[v]))
	{
		int to = G[v][i];
		if (!used[to])
			dfs(to);
	}
}

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	//freopen("In.txt", "r", stdin);
	int n, s = 0, cnts = 0;
	cin >> n;
	if (n >= 4)
	{
		cout << 1 / (n - n);
		return 0;
	}
	REP(i, n)
	{
		cin >> A[i];
		s ^= A[i];
		C.push_back(A[i]);
	}
	REP(i, n)
	{
		cin >> B[i];
		cnts += B[i] == s;
		C.push_back(B[i]);
	}
	C.push_back(s);
	sort(C.begin(), C.end());
	int nv = 0;
	REP(i, SZ(C))
	{
		if (i == 0 || C[i] > C[i - 1])
			M[C[i]] = nv++;
	}
	REP(i, n)
	{
		++CA[M[A[i]]];
		++CB[M[B[i]]];
	}
	int cntd = 0;
	REP(i, MAXN)
	{
		cntd += (CA[i] != CB[i]);
		if (cntd == 3 || CB[i] > CA[i] && i != M[s] || CB[i] > CA[i] + 1)
		{
			cout << -1;
			return 0;
		}
	}
	REP(i, n)
	{
		if (A[i] != B[i])
		{
			V[M[A[i]]] = true;
			G[M[A[i]]].push_back(M[B[i]]);
			G[M[B[i]]].push_back(M[A[i]]);
			++ans;
		}
	}
	REP(i, nv)
	{
		if (V[i] && !used[i])
		{
			dfs(i);
			++ans;
		}
	}
	ans -= cnts > 0;
	cout << ans;
	//cout << endl; system("pause");
	return 0;
}