#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif


#define ll long long
#define FOR(i,a) for(ll i=0;i<(ll)a.size();i++)
#define pb push_back
#define ld long double
#define mp make_pair
#define F first
#define S second
#define pii pair<ll,ll> 

using namespace :: std;

const ll maxn=1e5+500;
const ll inf=1e9;


ll a[maxn];
ll b[maxn];
multiset<ll> st;
map<ll,vector<ll> > ger;
map<ll,bool> vis;

void dfs(ll a){
	vis[a]=1;
	FOR(i,ger[a]){
		ll v=ger[a][i];
		if(vis[v]==0){
			dfs(v);
		}
	}
}
int main(){
	ll n;
	cin>>n;
	for(ll i=0;i<n;i++){
		cin>>a[i];
		a[n]^=a[i];
		st.insert(a[i]);
	}
	st.insert(a[n]);
	for(ll i=0;i<n;i++){
		cin>>b[i];
		if(st.find(b[i])==st.end()){
			cout<<-1;
			return 0;
		}
		st.erase(st.find(b[i]));
	}
	b[n]=(*st.begin());
	ll ans=-2;
	for(ll i=0;i<=n;i++){
		if(a[i]!=b[i]){
			ger[a[i]].pb(b[i]);
			ger[b[i]].pb(a[i]);
		}
		ans+=(a[i]!=b[i]);
	}
	for(ll i=0;i<=n;i++){
		if(a[i]!=b[i] && vis[a[i]]==0){
			dfs(a[i]);
			ans++;
		}	
	}
	if(b[n]==a[n])ans++;
	ans=max(ans,(ll)0);
	cout<<ans-1;












