#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <cstring>
#include <string>
#include <cctype>
#include <cassert>

using namespace std;

#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i = 0; i < (n); i++)
#define repr(i,b,e) for(int i = (b); i <= (e); i++)
#define INF (1001001001)
#define EPS (1e-15)

#define pr(x) do{cout << (#x) << " = " << (x) << endl;}while(0)
#define pri(x,i) do{cout << (#x) << "[" << i << "] = " << (x[i]) << endl;}while(0)
#define pra(x,n) rep(__i,n) pri(x,__i);
#define prar(x,b,e) repr(__i,b,e) pri(x,__i);

typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;

int in() {
	int a;
	scanf("%d ", &a);
	return a;
}

int main() {
	int N = in();
	vint a(N+1), b(N+1);
	int hoge = 0, fuga = 0;
	rep(i,N) hoge ^= a[i] = in();
	rep(i,N) fuga ^= b[i] = in();
	a[N] = hoge, b[N] = fuga;
	vint c = a, d = b;
	sort(c.begin(), c.end());
	sort(d.begin(), d.end());
	rep(i,N+1) {
		if (c[i] != d[i]) {
			cout << -1 << endl;
			return 0;
		}
	}
	
	map<int, vint*> data;
	int siz = 0;
	set<int> ind;
	rep(i,N) {
		if (a[i] == b[i]) continue;
		ind.insert(i);
		if (data.find(b[i]) == data.end()) {
			data[b[i]] = new vint(1,i);
		}
		else {
			data[b[i]]->pb(i);
		}
		siz++;
	}
	int ans = 0;
	int t = a[N];
	for (; siz > 0;) {
		ans++;
		vint *v = data[t];
		if (v && v->size() > 0) {
			int j = v->at(v->size() - 1);
			v->pop_back();
			ind.erase(j);
			int temp = a[j];
			a[j] = t;
			t = temp;
			siz--;
		}
		else {
			int j = *(ind.begin());
			for (set<int>::iterator i = ind.rbegin(); i != ind.rend(); ++i) {
				if (a[*i] == t) continue;
				j = *i;
				if (a[j] != b[N]) break;
			}
			int temp = a[j];
			a[j] = t;
			t = temp;
		}
	}
	cout << ans << endl;
	return 0;
}
