#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)
	
using namespace std;

const int MAX_N=1e5;
int N,a[MAX_N+1],b[MAX_N+1];
vector<int> G[MAX_N+1];
bool f[MAX_N+1];
void dfs(int v){
	f[v]=true;
	REP(i,(int)G[v].size()){
		if (!f[G[v][i]]){
			dfs(G[v][i]);
		}
	}
}

int main(){
	cin>>N;
	{
		int xa,xb;
		xa=xb=0;
		REP(i,N){
			cin>>a[i];
			xa^=a[i];
		}
		REP(i,N){
			cin>>b[i];
			xb^=b[i];
		}
		a[N]=xa;
		b[N]=xb;
	}
	{
		map<int,int> ma,mb;
		REP(i,N+1){
			ma[a[i]]++;
			mb[b[i]]++;
		}
		bool flg=true;
		REP(i,N+1){
			if (ma[a[i]]!=mb[a[i]]){
				flg=false;
				break;
			}
		}
		if (!flg){
			cout<<-1<<endl;
			return 0;
		}
	}
	{
		vector<int> vec;
		REP(i,N+1){
			vec.push_back(a[i]);
		}
		sort(vec.begin(),vec.end());
		vec.erase(unique(vec.begin(),vec.end()),vec.end());
		REP(i,N+1){
			a[i]=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin();
			b[i]=lower_bound(vec.begin(),vec.end(),b[i])-vec.begin();
		}
	}
	int edge=0,con=0;
	{
		REP(i,N+1){
			if (a[i]!=b[i] || i==N){
				G[a[i]].push_back(b[i]);
				edge++;
			}
		}
		REP(i,N+1){
			if (G[a[i]].size()!=0 && !f[a[i]]){
				con++;
				dfs(a[i]);
			}
		}
	}
	cout<<edge+con-2<<endl;
	return 0;
}