#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstring>
#include <set>
#include <utility>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cmath>
#include <deque>
#include <bitset>
#define ll long long
#define PI 3.1415926535897932384626433832795
#define read(x) scanf("%d",&x);
#define readll(x) cin>>x;
#define FOR(x,a,b) for(int x=a;x<b;x++)
#define MP make_pair
#define PB push_back
#define pii pair<int,int>
#define readN(N,X) for(int i=0;i<N;i++) cin>>X[i];
#define pff pair<double,double>
using namespace std;

map<int, queue<int> > to;

int N;
int a[100005];
int b[100005];
int vis[100005];
map<int,int> ca,cb;
void No(){
	cout<<"-1"<<endl;
	exit(0);
}

int main () {
	std::ios::sync_with_stdio(false); 
	cin>>N;
	readN(N,a);
	readN(N,b);
	FOR(i,0,N){
		ca[a[i]]++;
		cb[b[i]]++;
		//cout<<"dari "<<b[i]<<" ke "<<a[i]<<endl;
	}
	int res = 0;

	int kagi = 0;
	FOR(i,0,N) kagi ^= a[i];
	
	FOR (i,0,N){
		if (a[i] == b[i]) continue;
		to[b[i]].push(i);
	}
	bool mul = 0;
	//cout<<kagi<<endl;
	FOR (i,0,N){
		if (a[i] != b[i] && b[i] == kagi) {
			int p = i;
			while (!vis[p]) {
				//cout<<p<<endl;
				vis[p] = 1;
				res++;
				if (to[ a[p] ].size() == 0) break;
				int tmp = to[ a[p] ].front();
				to[ a[p] ].pop();
				p =tmp;
			}
			mul=1;
			kagi = a[p];
			break;
		}	
	}
	//cout<<kagi<<endl;
	//cout<<"AA"<<endl;
	bool gagal = 0;
	FOR(i,0,N){
		if (!vis[i] && a[i] != b[i]) gagal=1;
		//if () gagal=1;
	}
	if (!gagal) {
		cout<<res<<endl;
		return 0;
	}
	if (mul || to[kagi].size() == 0) res++;

	res+=mul;
	FOR(i,0,N){
		if (a[i] == b[i]) continue;
		if (vis[i]) continue;
		int p = i;
		while (!vis[p]) {
			//cout<<p<<endl;
			vis[p] = 1;
			res++;
			if (to[ a[p] ].size() == 0) No();
			while (1){
			int tmp = to[ a[p] ].front();
			to[ a[p] ].pop();
			if (!vis[tmp] ||to[ a[p] ].size() == 0 ) break;
			}
			
			p =tmp;
		}
		//cout<<"1loop "<<endl;
		res++;
	}

	cout<<res - 1<<endl;
}
