// Original C++ solution (slower):
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

 

using namespace std;

using namespace __gnu_pbds;

 

#define fi first

#define se second

#define mp make_pair

#define pb push_back

#define fbo find_by_order

#define ook order_of_key

 

typedef long long ll;

typedef pair<ll,ll> ii;

typedef vector<ll> vi;

typedef long double ld; 

typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;

typedef set<ll>::iterator sit;

typedef map<ll,ll>::iterator mit;



ll a[100011];

ll b[100011];



struct DSU

{

	int S;

	

	struct node

	{

		int p; ll sum;

	};

	vector<node> dsu;

	

	DSU(int n)

	{

		S = n;

		for(int i = 0; i < n; i++)

		{

			node tmp;

			tmp.p = i; tmp.sum = 0;

			dsu.pb(tmp);

		}

	}

	

	void reset(int n)

	{

		dsu.clear();

		S = n;

		for(int i = 0; i < n; i++)

		{

			node tmp;

			tmp.p = i; tmp.sum = 0;

			dsu.pb(tmp);

		}

	}

	

	int rt(int u)

	{

		if(dsu[u].p == u) return u;

		dsu[u].p = rt(dsu[u].p);

		return dsu[u].p;

	}

	

	void merge(int u, int v)

	{

		u = rt(u); v = rt(v);

		if(u == v) return ;

		if(rand()&1) swap(u, v);

		dsu[v].p = u;

		dsu[u].sum += dsu[v].sum;

	}

	

	bool sameset(int u, int v)

	{

		if(rt(u) == rt(v)) return true;

		return false;

	}

	

	ll getstat(int u)

	{

		return dsu[rt(u)].sum;

	}

};



bool used[100001];

bool req[100001];

int main()

{

	ios_base::sync_with_stdio(0); cin.tie(0);

	int n; cin>>n;

	vector<ll> v1,v2;

	for(int i=0;i<n;i++)

	{

		cin>>a[i];

		a[n]^=a[i];

		v1.pb(a[i]);

	}

	v1.pb(a[n]);

	for(int i=0;i<n;i++)

	{

		cin>>b[i];

		b[n]^=b[i];

		v2.pb(b[i]);

	}

	v2.pb(b[n]);

	sort(v1.begin(),v1.end());

	sort(v2.begin(),v2.end());

	for(int i=0;i<=n;i++)

	{

		if(v1[i]!=v2[i])

		{

			cout<<-1<<'\n';

			return 0;

		}

	}

	//swap a[n] with a[i] how many times to reach b?

	v1.erase(unique(v1.begin(),v1.end()),v1.end());

	int ans=0;

	for(int i = 0; i <= n; i++)

	{

		a[i]=lower_bound(v1.begin(),v1.end(),a[i])-v1.begin();

		b[i]=lower_bound(v1.begin(),v1.end(),b[i])-v1.begin();

		if(i<n&&a[i]!=b[i]) ans++;

		if(a[i]!=b[i])

		{

			req[a[i]]=req[b[i]]=1;

		}

	}

	DSU dsu(int(v1.size()));

	for(int i=0;i<=n;i++)

	{

		dsu.merge(a[i],b[i]);

	}

	for(int i=0;i<v1.size();i++)

	{

		if(dsu.rt(i)==i)

		{

			used[i]=1;

		}

	}

	int holy1 = dsu.rt(a[n]);

	int holy2 = dsu.rt(b[n]);

	for(int i=0;i<v1.size();i++)

	{

		if(i!=holy1&&i!=holy2&&req[i]&&used[i]) ans++;

	}

	cout<<ans<<'\n';

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<map>

#include<vector>

#include<algorithm>

using namespace std;

const int MAXN=200005;



int N,M,A[MAXN],B[MAXN];

map<int,int> cnt;



int val[MAXN];



int fa[MAXN],sz[MAXN];

int Root(int x)

{

    if(fa[x]==0)

        return x;

    return fa[x]=Root(fa[x]);

}

void Union(int x,int y)

{

    int r1=Root(x),r2=Root(y);

    if(r1==r2)

        return;

    fa[r1]=r2;

    sz[r2]+=sz[r1];

}



int main()

{

    int sum=0,tmp=0;

    scanf("%d",&N);

    for(int i=1;i<=N;i++)

    {

        scanf("%d",&A[i]);

        cnt[A[i]]++;

        sum^=A[i];

    }

    cnt[sum]++;

    A[N+1]=sum;

    for(int i=1;i<=N;i++)

    {

        scanf("%d",&B[i]);

        tmp^=B[i];

    }

    B[N+1]=tmp^sum;

    for(int i=1;i<=N;i++)

    {

        if(cnt[B[i]]==0)

        {

            puts("-1");

            return 0;

        }

        cnt[B[i]]--;

    }

    for(int i=1;i<=N+1;i++)

    {

        val[i*2-1]=A[i];

        val[i*2]=B[i];

    }

    sort(val+1,val+(N+1)*2+1);

    M=unique(val+1,val+(N+1)*2+1)-val-1;

    for(int i=1;i<=N+1;i++)

    {

        A[i]=lower_bound(val+1,val+M+1,A[i])-val;

        B[i]=lower_bound(val+1,val+M+1,B[i])-val;

    }

    sum=find(val+1,val+M+1,sum)-val;



    fill(sz+1,sz+M+1,1);



    int ans=0;

    for(int i=1;i<=N;i++)

        if(A[i]!=B[i])

        {

            Union(A[i],B[i]);

            ans++;

        }

    for(int i=1;i<=M;i++)

        if(Root(i)==i&&sz[i]>1)

            ans++;

    if(Root(sum)!=sum||sz[sum]>1)

        ans--;

    printf("%d\n",ans);



    return 0;

}


// Fastest known solution for this problem: