stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <cstdio>

#include <iostream>

#include <cstring>

#include <algorithm> 

#include <vector>

#include <map>

using namespace std;

#define Max(a,b) ((a)>(b)?(a):(b))

#define Min(a,b) ((a)<(b)?(a):(b))

#define ll long long 

#define ri register int

#define f(i,a,b) for(ri i=a;i<=b;i++)

#define rf(i,b,a) for(ri i=b;i>=a;i--)

const int N=1e5+10;

int n,cnt,ans;

int a[N],b[N],x[N],y[N];

bool vis[N];

vector<int>Q[N];

map<int,int>d;

inline void read(int &x) {

    x=0;int f=1;char c=getchar();

    while (c>'9'||c<'0') {if (c=='-') f=-1;c=getchar();}

    while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}

    x*=f;

}

void dfs(int x)

{

    vis[x]=1;

    for (int i=0;i<Q[x].size();i++) 

    	if (!vis[Q[x][i]]) dfs(Q[x][i]);

}

int main()

{

   	read(n);

    for (int i=1;i<=n;i++) {

        read(a[i]),a[n+1]^=a[i];

        if (!d[a[i]]) d[a[i]]=++cnt;

    }

    if (!d[a[n+1]]) d[a[n+1]]=++cnt;

    for (int i=1;i<=n;i++) {

        read(b[i]),b[n+1]^=b[i];

        if (!d[b[i]]) d[b[i]]=++cnt;

    }

    if (!d[b[n+1]]) d[b[n+1]]=++cnt;

    memcpy(x,a,sizeof x),memcpy(y,b,sizeof y);

    sort(x+1,x+n+2),sort(y+1,y+n+2);

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

    	if (x[i]!=y[i]) {printf("-1");return 0;}

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

    	a[i]=d[a[i]],b[i]=d[b[i]];

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

    	if (a[i]!=b[i]) ans++,Q[a[i]].push_back(b[i]);

    if (a[n+1]!=b[n+1]) Q[a[n+1]].push_back(b[n+1]);

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

    	if (Q[i].size()&&!vis[i]) dfs(i),ans++;

    if (!vis[cnt]) ans++;

    printf("%d",ans-1);

}

// Faster C++ solution for the same problem (measured speedup: 5.233539548945717x):
#include<bits/stdc++.h>

using namespace std;

int n, a[100007], b[100007], cnt, f[200007], aa[100007], bb[100007], ss[200007], tot;

int read()

{

	int num = 0;

	char c = getchar();

	while (c < '0' || c>'9')c = getchar();

	while (c >= '0' && c <= '9')num = num * 10 + c - '0', c = getchar();

	return num;

}

int find(int x)

{

	if (f[x] == x)return x;

	else return f[x] = find(f[x]);

}

int main()

{

	n = read();

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

	{

		aa[i] = a[i] = read();

		a[n + 1] ^= a[i];

	}

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

	{

		bb[i] = b[i] = read();

		b[n + 1] ^= b[i];

	}

	aa[n + 1] = a[n + 1];

	bb[n + 1] = b[n + 1];

	n++;

	sort(aa + 1, aa + n + 1);

	sort(bb + 1, bb + n + 1);

	for (int i = 1; i <= n - 1; i++)

		if (aa[i] != bb[i])

		{

			cout << -1 << endl;

			return 0;

		}

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

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

		{

			ss[++tot] = a[i];

			ss[++tot] = b[i];

			if (i != n)cnt++;

		}

	sort(ss + 1, ss + tot + 1);

	tot = unique(ss + 1, ss + tot + 1) - ss - 1;

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

		f[i] = i;

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

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

		{

			int x = lower_bound(ss + 1, ss + tot + 1, a[i]) - ss;

			int y = lower_bound(ss + 1, ss + tot + 1, b[i]) - ss;

			f[find(x)] = find(y);

		}

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

		if (f[i] == i)cnt++;

	cout << cnt - 1 << endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define maxn 100010

#define pb push_back

using namespace std;



int a[maxn],b[maxn],c[maxn],d[maxn],fa[maxn],sz[maxn],n,cnt=0,cnt2=0;

bool vis[maxn];

vector<int> v;

int find(int x){

	return fa[x]==x?x:fa[x]=find(fa[x]);

}

int main(){

	ios::sync_with_stdio(0);

	cin>>n;

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

		cin>>a[i],a[n+1]^=a[i],v.pb(a[i]);

	v.pb(a[n+1]);

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

		cin>>b[i],b[n+1]^=b[i];

	memcpy(c,a,sizeof(a));

	memcpy(d,b,sizeof(b));

	sort(c+1,c+n+2),sort(d+1,d+n+2);

	for(int i=1;i<=n+1;i++){

		if(c[i]!=d[i])

			return cout<<"-1"<<endl,0;

	}

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

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

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

		fa[i]=i,sz[i]=1;

	for(int i=1;i<=n+1;i++){

		int x=lower_bound(v.begin(),v.end(),a[i])-v.begin();

		int y=lower_bound(v.begin(),v.end(),b[i])-v.begin();

		if(a[i]!=b[i]||i==n+1)

			cnt+=i<=n,vis[x]=vis[y]=1;

		if(find(x)!=find(y))

			fa[find(x)]=find(y);

	}

	if(!cnt)

		return cout<<"0"<<endl,0;

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

		cnt+=vis[i]&&find(i)==i;

	cout<<cnt-1<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.8072427372041253x):
#include<stdio.h>

#include<map>

using namespace std;

int n,ch[1000005],sh[1000005],ans,sum,fa[1000005],tot,mk[1000005];

map <int,int> mp,mmp;

int gf(int p){

	if(fa[p]==p)return p;

	return fa[p]=gf(fa[p]);

}

void mg(int a,int b){

	int fx=gf(a),fy=gf(b);

	if(fx!=fy){

		fa[fx]=fy;

	}

}

int main(){

//	freopen("duliu.in","r",stdin);

//	freopen("duliu.out","w",stdout);

	scanf("%d",&n);

	for(int i=1;i<=n;i++){

		scanf("%d",&sh[i]);sum^=sh[i];mp[sh[i]]++;

	}mp[sum]++;

	for(int i=1;i<=n;i++){

		scanf("%d",&ch[i]);mp[ch[i]]--;

		if(mp[ch[i]]<0){

			ans=-999999999;

		}

		if(sh[i]!=ch[i]){mk[i]=1;

			ans++;

		}

	}

	sh[n+1]=sum;

	for(int i=1;i<=n+1;i++){

		if(mp[sh[i]]>0){

			ch[n+1]=sh[i];break;

		}

	}

	n++;tot=n+n;

	mk[n]=1;

	for(int i=1;i<=n;i++){

		if(!mk[i])continue;

		if(mmp[sh[i]]==0)mmp[sh[i]]=++tot;

		fa[i]=i;fa[i+n]=i+n;

	}

	for(int i=1;i<=tot;i++)fa[i]=i;

	for(int i=1;i<=n;i++){

		if(!mk[i]){

			fa[i]=fa[i+n]=0;

		}

	}

	for(int i=1;i<=n;i++){

		if(sh[i]!=ch[i])mg(i,i+n);

		mg(i,mmp[sh[i]]);mg(i+n,mmp[ch[i]]);

	}

	for(int i=1;i<=tot;i++){

		if(fa[i]==i){//printf("i:%d\n",i);

			ans++;

		}

	}

	ans--;

	if(ans<0)printf("-1");

	else printf("%d",ans);

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define maxn 100010

#define pb push_back

using namespace std;



int a[maxn],b[maxn],c[maxn],d[maxn],fa[maxn],sz[maxn],n,cnt=0,cnt2=0;

bool vis[maxn];

vector<int> v;

int find(int x){

	return fa[x]==x?x:fa[x]=find(fa[x]);

}

int main(){

	ios::sync_with_stdio(0);

	cin>>n;

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

		cin>>a[i],a[n+1]^=a[i],v.pb(a[i]);

	v.pb(a[n+1]);

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

		cin>>b[i],b[n+1]^=b[i];

	memcpy(c,a,sizeof(a));

	memcpy(d,b,sizeof(b));

	sort(c+1,c+n+2),sort(d+1,d+n+2);

	for(int i=1;i<=n+1;i++){

		if(c[i]!=d[i])

			return cout<<"-1"<<endl,0;

	}

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

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

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

		fa[i]=i,sz[i]=1;

	for(int i=1;i<=n+1;i++){

		int x=lower_bound(v.begin(),v.end(),a[i])-v.begin();

		int y=lower_bound(v.begin(),v.end(),b[i])-v.begin();

		if(a[i]!=b[i]||i==n+1)

			cnt+=i<=n,vis[x]=vis[y]=1;

		if(find(x)!=find(y))

			fa[find(x)]=find(y);

	}

	if(!cnt)

		return cout<<"0"<<endl,0;

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

		cnt+=vis[i]&&find(i)==i;

	cout<<cnt-1<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.583627014334788x):
#include <cstdio>

#include <iostream>

#include <algorithm>

#include <cstring>

#include <string>

#include <map>

#include <vector>

#include <queue>

using namespace std;

typedef long long LL;



LL n,m,k,tot;

LL a[500005],b[500005],c[500005],d[500005];

LL vis[500005] = {0};

string s;

map <LL,LL> mp;



LL fa[500005];

LL getf(LL x){

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

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

}

void unionn(LL x,LL y){

	x = getf(x); y = getf(y);

	if(x == y) return;

	tot --; fa[x] = y;

}



int main(){

	cin >> n;

	for(LL i = 1;i <= n;i ++){ cin >> a[i]; c[i] = a[i]; a[n + 1] ^= a[i]; }

	for(LL i = 1;i <= n;i ++){ cin >> b[i]; d[i] = b[i]; b[n + 1] ^= b[i]; }

	n ++; c[n] = a[n]; d[n] = b[n];

	sort(c + 1,c + 1 + n); sort(d + 1,d + 1 + n);

	for(LL i = 1;i <= n;i ++){

		if(c[i] != d[i]){

			cout << -1 << endl;

			return 0;

		}

	}

	

	tot = 0;

	for(LL i = 1;i < n;i ++){

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

			if(mp.find(a[i]) == mp.end()) mp[a[i]] = ++ tot;

			if(mp.find(b[i]) == mp.end()) mp[b[i]] = ++ tot;

		}

	}

	if(mp.find(a[n]) == mp.end()) mp[a[n]] = ++ tot;

	if(mp.find(b[n]) == mp.end()) mp[b[n]] = ++ tot;

	for(LL i = 1;i <= n;i ++){

		a[i] = mp[a[i]];

		b[i] = mp[b[i]];

	}

	

	LL tmp = 0;

	for(LL i = 1;i <= tot;i ++) fa[i] = i;

	for(LL i = 1;i < n;i ++){

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

			tot ++; tmp = 1;

			unionn(a[i],b[i]);

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

		}

	}

	unionn(a[n],b[n]);

	tot --;

	cout << tot << endl;

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define maxn 100010

#define pb push_back

using namespace std;



int a[maxn],b[maxn],c[maxn],d[maxn],fa[maxn],sz[maxn],n,cnt=0,cnt2=0;

bool vis[maxn];

vector<int> v;

int find(int x){

	return fa[x]==x?x:fa[x]=find(fa[x]);

}

int main(){

	ios::sync_with_stdio(0);

	cin>>n;

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

		cin>>a[i],a[n+1]^=a[i],v.pb(a[i]);

	v.pb(a[n+1]);

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

		cin>>b[i],b[n+1]^=b[i];

	memcpy(c,a,sizeof(a));

	memcpy(d,b,sizeof(b));

	sort(c+1,c+n+2),sort(d+1,d+n+2);

	for(int i=1;i<=n+1;i++){

		if(c[i]!=d[i])

			return cout<<"-1"<<endl,0;

	}

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

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

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

		fa[i]=i,sz[i]=1;

	for(int i=1;i<=n+1;i++){

		int x=lower_bound(v.begin(),v.end(),a[i])-v.begin();

		int y=lower_bound(v.begin(),v.end(),b[i])-v.begin();

		if(a[i]!=b[i]||i==n+1)

			cnt+=i<=n,vis[x]=vis[y]=1;

		if(find(x)!=find(y))

			fa[find(x)]=find(y);

	}

	if(!cnt)

		return cout<<"0"<<endl,0;

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

		cnt+=vis[i]&&find(i)==i;

	cout<<cnt-1<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.6244202903416474x):
#include<bits/stdc++.h>

#define int long long

using namespace std;

const int N=4e5+10;

int a[N],b[N];

int ta[N],tb[N];

int dp[N],fa[N];

int n,m,ans;

int find(int x){

	return fa[x]==x?x:fa[x]=find(fa[x]);

}

signed main(){

	scanf("%lld",&n);

	for(int i=1;i<=n;++i){

		scanf("%lld",&a[i]);

		a[n+1]^=a[i];

		ta[i]=a[i];

	}

	for(int i=1;i<=n;++i){

		scanf("%lld",&b[i]);

		b[n+1]^=b[i];

		tb[i]=b[i];

	}

	++n;

	ta[n]=a[n];tb[n]=b[n];

	sort(ta+1,ta+n+1);sort(tb+1,tb+n+1);

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

		if(ta[i]!=tb[i]){

			printf("-1");

			return 0;

		}

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

		if(a[i]!=b[i]||i==n){

			dp[++m]=a[i];

			dp[++m]=b[i];

			if(i<n)++ans;

		}

	if(!ans){

		printf("0");

		return 0;

	}

	sort(dp+1,dp+m+1);

	m=unique(dp+1,dp+m+1)-dp-1;

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

		fa[i]=i;

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

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

			a[i]=lower_bound(dp+1,dp+m+1,a[i])-dp,

			b[i]=lower_bound(dp+1,dp+m+1,b[i])-dp;

			fa[find(a[i])]=find(b[i]);

		}

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

		if(fa[i]==i)

			++ans;

	printf("%lld",ans-1);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

const int MAXN = 2e5 + 10;

using namespace std;

int n;

int A[MAXN], B[MAXN], ta[MAXN], tb[MAXN], sa, sb, tot = 0, Do[MAXN], fa[MAXN];

int find(int x) {

    return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);

}

int unionn(int x, int y) {

    fa[x] = y;

}

int main() {

    scanf("%d",&n);

    for(int i = 1; i <= n; i++)scanf("%d",&A[i]),sa ^= A[i]; A[n + 1] = sa;

    for(int i = 1; i <= n; i++)scanf("%d",&B[i]),sb ^= B[i]; B[n + 1] = sb;

    n++;

    memcpy(ta, A, sizeof(A)); memcpy(tb, B, sizeof(B));

    sort(ta + 1, ta + n + 1); sort(tb + 1, tb + n + 1);

    for(int i = 1; i <n; i++) if(ta[i] != tb[i]) return puts("-1"), 0;

 

    int ans = 0, sum = 0;

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

        if(A[i] != B[i] || (i == n)) {

            Do[++sum] = A[i]; Do[++sum] = B[i];

            if(i < n)ans++;

        }

    if(ans == 0) return puts("0"), 0;

 

    sort(Do + 1, Do + sum + 1);

    sum = unique(Do + 1, Do + sum + 1) - Do - 1;

    for(int i = 1; i <= sum; i++) fa[i] = i;

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

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

            A[i] = lower_bound(Do + 1, Do + sum + 1, A[i]) - Do;

            B[i] = lower_bound(Do + 1, Do + sum + 1, B[i]) - Do;

            unionn(find(A[i]), find(B[i]));

        }

 

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

        if(fa[i] == i) ans++;

    printf("%d", ans - 1);

 

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.331199620711278x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

int S[100005],T[100005],n,ans,sum;

int A[100005],B[100005],sa,sb;

int fa[100005],Do[200005];

int gg(){printf("-1");return 0;}

int ask(int x){if(fa[x]==x)return x;return fa[x]=ask(fa[x]);}

void unite(int x,int y){fa[x]=y;}

int main(){

//  freopen("duliu.in","r",stdin);

//  freopen("duliu.out","w",stdout);

    scanf("%d",&n);

    for(int i=1;i<=n;i++)scanf("%d",&A[i]),sa^=A[i];A[n+1]=sa;

    for(int i=1;i<=n;i++)scanf("%d",&B[i]),sb^=B[i];B[n+1]=sb;

    memcpy(S,A,sizeof A);memcpy(T,B,sizeof B);

    n++;sort(S+1,S+1+n);sort(T+1,T+1+n);

    for(int i=1;i<n;i++)if(S[i]!=T[i])return gg();

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

    	if(A[i]!=B[i]||(i==n)){

			Do[++sum]=A[i];

			Do[++sum]=B[i];

			if(i<n)ans++;

		}

	if(ans==0)return printf("0"),0;

	sort(Do+1,Do+1+sum);

	sum=unique(Do+1,Do+sum+1)-Do-1;

	for(int i=1;i<=sum;i++)fa[i]=i;

	for(int i=1;i<=n;i++){

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

			A[i]=lower_bound(Do+1,Do+1+sum,A[i])-Do;

			B[i]=lower_bound(Do+1,Do+1+sum,B[i])-Do;

			unite(ask(A[i]),ask(B[i]));

		}

	}

	for(int i=1;i<=sum;i++)ans+=(fa[i]==i);

	printf("%d",ans-1);

}

/*

3

1 2 3

1 3 2

*/

/*

4

1 4 2 6

1 2 4 6

*/

/*

10

1 3 3 9 4 5 1 6 4 0

4 1 3 9 10 4 3 1 6 5

*/

/*

5

1 2 4 8 0

2 1 15 8 4

*/

/*

10

1 2 3 4 4 5 6 7 8 12

2 1 4 3 4 0 5 6 7 8

*/

/*

10

1 2 3 4 5 6 7 8 9 10

10 1 2 3 4 5 6 7 8 9

*/

/*

10

1 2 3 4 5 6 7 8 9 10

2 1 4 3 6 5 8 7 10 9

*/

/*

9

1 2 3 4 5 6 7 8 9

9 1 2 3 4 5 6 7 8

*/
// Slower C++ solution:
#include<bits/stdc++.h>

const int MAXN = 2e5 + 10;

using namespace std;

int n;

int ans=0,sum=0;

int A[MAXN], B[MAXN], ta[MAXN], tb[MAXN], sa, sb, tot = 0, Do[MAXN], fa[MAXN];

int find(int x) {

    return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);

}

int unionn(int x, int y) {

    fa[x] = y;

}

int main() {

    scanf("%d",&n);

    for(int i=1;i<=n;i++)scanf("%d",&A[i]),sa^=A[i];A[n+1]=sa;

    for(int i=1;i<=n;i++)scanf("%d",&B[i]),sb^=B[i];B[n+1]=sb;

    memcpy(ta,A,sizeof(A));memcpy(tb,B,sizeof(B));

    n++;sort(ta+1,ta+n+1);sort(tb+1,tb+n+1);

    for(int i=1;i<n;i++)if(ta[i]!=tb[i])return puts("-1"), 0;

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

        if(A[i]!=B[i]||(i==n)){

            Do[++sum]=A[i]; 

			Do[++sum]=B[i];

            if(i<n)ans++;

        }

    if(ans==0)return puts("0"), 0;

    sort(Do+1,Do+sum+1);

    sum=unique(Do+1,Do+sum+1)-Do-1;

    for(int i=1;i<=sum;i++)fa[i]=i;

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

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

            A[i]=lower_bound(Do+1,Do+sum+1,A[i])-Do;

            B[i]=lower_bound(Do+1,Do+sum+1,B[i])-Do;

            unionn(find(A[i]),find(B[i]));

        }

    for(int i=1;i<=sum;i++)ans+=(fa[i]==i);

    printf("%d", ans - 1);

 

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.3310263696543276x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

int S[100005],T[100005],n,ans,sum;

int A[100005],B[100005],sa,sb;

int fa[100005],Do[200005];

int gg(){printf("-1");return 0;}

int ask(int x){if(fa[x]==x)return x;return fa[x]=ask(fa[x]);}

void unite(int x,int y){fa[x]=y;}

int main(){

//  freopen("duliu.in","r",stdin);

//  freopen("duliu.out","w",stdout);

    scanf("%d",&n);

    for(int i=1;i<=n;i++)scanf("%d",&A[i]),sa^=A[i];A[n+1]=sa;

    for(int i=1;i<=n;i++)scanf("%d",&B[i]),sb^=B[i];B[n+1]=sb;

    memcpy(S,A,sizeof A);memcpy(T,B,sizeof B);

    n++;sort(S+1,S+1+n);sort(T+1,T+1+n);

    for(int i=1;i<n;i++)if(S[i]!=T[i])return gg();

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

    	if(A[i]!=B[i]||(i==n)){

			Do[++sum]=A[i];

			Do[++sum]=B[i];

			if(i<n)ans++;

		}

	if(ans==0)return printf("0"),0;

	sort(Do+1,Do+1+sum);

	sum=unique(Do+1,Do+sum+1)-Do-1;

	for(int i=1;i<=sum;i++)fa[i]=i;

	for(int i=1;i<=n;i++){

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

			A[i]=lower_bound(Do+1,Do+1+sum,A[i])-Do;

			B[i]=lower_bound(Do+1,Do+1+sum,B[i])-Do;

			unite(ask(A[i]),ask(B[i]));

		}

	}

	for(int i=1;i<=sum;i++)ans+=(fa[i]==i);

	printf("%d",ans-1);

}

/*

3

1 2 3

1 3 2

*/

/*

4

1 4 2 6

1 2 4 6

*/

/*

10

1 3 3 9 4 5 1 6 4 0

4 1 3 9 10 4 3 1 6 5

*/

/*

5

1 2 4 8 0

2 1 15 8 4

*/

/*

10

1 2 3 4 4 5 6 7 8 12

2 1 4 3 4 0 5 6 7 8

*/

/*

10

1 2 3 4 5 6 7 8 9 10

10 1 2 3 4 5 6 7 8 9

*/

/*

10

1 2 3 4 5 6 7 8 9 10

2 1 4 3 6 5 8 7 10 9

*/

/*

9

1 2 3 4 5 6 7 8 9

9 1 2 3 4 5 6 7 8

*/
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define fr(i,x,y) for(int i=x;i<=y;i++)

#define rf(i,x,y) for(int i=x;i>=y;i--)

using namespace std;

const int N=100002;

int n;

struct data{

	int x,y;

}a[N];

map<int,int> mp;

int f[N],sz[N];

vector<int> w[N];

int b[N];



void read(int &x){

	char ch=getchar();x=0;

	for(;ch<'0'||ch>'9';ch=getchar());

	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';

}



int gf(int x){

	return f[x]==x?x:f[x]=gf(f[x]);

}



void unio(int x,int y){

	int fx=gf(x),fy=gf(y);

	if (fx!=fy){

		f[fx]=fy;

		sz[fy]+=sz[fx];

	}

}



int main(){

	read(n);

	int xr=0;

	fr(i,1,n) read(a[i].x),xr^=a[i].x,mp[a[i].x]++;

	a[n+1].x=xr;mp[xr]++;

	int tot=0;

	/*for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)

	 it->second=++tot;*/

	int flag=0;

	fr(i,1,n){

		read(a[i].y);

		if (!mp.count(a[i].y)){

			printf("-1\n");

			return 0;

		}

		mp[a[i].y]--;

		if (mp[a[i].y]==0) mp.erase(a[i].y);

		if (a[i].y==xr&&a[i].x!=a[i].y) flag=1;

	}

	a[n+1].y=mp.begin()->first;

	

	//sort(a+1,a+1+n,cmp)

	mp.clear();

	fr(i,1,n+1) if (a[i].x!=a[i].y) mp[a[i].x]=1,mp[a[i].y]=1;

	tot=0;

	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)

	 it->second=++tot;

	fr(i,1,n+1) if (a[i].x!=a[i].y) a[i].x=mp[a[i].x],a[i].y=mp[a[i].y];

	//fr(i,1,n+1) if (a[i].x!=a[i].y) w[a[i].y].push_back(i);

	fr(i,1,tot) f[i]=i;

	fr(i,1,n+1) if (a[i].x!=a[i].y) sz[a[i].x]++;

	fr(i,1,n+1)

	 if (a[i].x!=a[i].y){

	 	//b[a[i].x]=1;

		unio(a[i].x,a[i].y);

	 } 

	int ans=0;

	fr(i,1,tot) if (gf(i)==i) ans+=sz[i]+1;

	if (ans==0){

		printf("0\n");

		return 0;

	}

	//cout<<ans<<endl;

	if (flag) ans-=2;

	if (flag&&a[n+1].x==a[n+1].y) ans++;

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.7979833334331405x):
#include<cstdio>

#include<map>

#define maxn 100005

using namespace std;

int n,a[maxn],b[maxn],fa[maxn],siz[maxn],cnt,ans;

map<int,int> mp,val;

int find(int x)

{

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

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

}

int main()

{

	scanf("%d",&n);

	int x=0;

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

	{

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

		mp[a[i]]++; x^=a[i];

		if(!val[a[i]]) val[a[i]]=++cnt;

	}

	mp[x]++;

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

	{

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

		if(!mp[b[i]]) { printf("-1\n"); return 0; }

		mp[b[i]]--;

		if(!val[b[i]]) val[b[i]]=++cnt;

	}

	for(int i=1;i<=cnt;i++) fa[i]=i,siz[i]=1;

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

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

		{

			int r1=find(val[a[i]]),r2=find(val[b[i]]);

			if(r1!=r2) fa[r1]=r2,siz[r2]+=siz[r1];

			ans++;

		}

	bool f=true;

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

		if(a[i]!=b[i]) { f=false; break; }

	if(f) { printf("0\n"); return 0; }

	if(!val[x]) ans++;

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

		if(find(i)==i&&siz[i]>1) ans++;

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

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define fr(i,x,y) for(int i=x;i<=y;i++)

#define rf(i,x,y) for(int i=x;i>=y;i--)

using namespace std;

const int N=100002;

int n;

struct data{

	int x,y;

}a[N];

map<int,int> mp;

int f[N],sz[N];

vector<int> w[N];

int b[N];



void read(int &x){

	char ch=getchar();x=0;

	for(;ch<'0'||ch>'9';ch=getchar());

	for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';

}



int gf(int x){

	return f[x]==x?x:f[x]=gf(f[x]);

}



void unio(int x,int y){

	int fx=gf(x),fy=gf(y);

	if (fx!=fy){

		f[fx]=fy;

		sz[fy]+=sz[fx];

	}

}



int main(){

	read(n);

	int xr=0;

	fr(i,1,n) read(a[i].x),xr^=a[i].x,mp[a[i].x]++;

	a[n+1].x=xr;mp[xr]++;

	int tot=0;

	/*for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)

	 it->second=++tot;*/

	int flag=0;

	fr(i,1,n){

		read(a[i].y);

		if (!mp.count(a[i].y)){

			printf("-1\n");

			return 0;

		}

		mp[a[i].y]--;

		if (mp[a[i].y]==0) mp.erase(a[i].y);

		if (a[i].y==xr&&a[i].x!=a[i].y) flag=1;

	}

	a[n+1].y=mp.begin()->first;

	

	//sort(a+1,a+1+n,cmp)

	mp.clear();

	fr(i,1,n+1) if (a[i].x!=a[i].y) mp[a[i].x]=1,mp[a[i].y]=1;

	tot=0;

	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)

	 it->second=++tot;

	fr(i,1,n+1) if (a[i].x!=a[i].y) a[i].x=mp[a[i].x],a[i].y=mp[a[i].y];

	//fr(i,1,n+1) if (a[i].x!=a[i].y) w[a[i].y].push_back(i);

	fr(i,1,tot) f[i]=i;

	fr(i,1,n+1) if (a[i].x!=a[i].y) sz[a[i].x]++;

	fr(i,1,n+1)

	 if (a[i].x!=a[i].y){

	 	//b[a[i].x]=1;

		unio(a[i].x,a[i].y);

	 } 

	int ans=0;

	fr(i,1,tot) if (gf(i)==i) ans+=sz[i]+1;

	if (ans==0){

		printf("0\n");

		return 0;

	}

	//cout<<ans<<endl;

	if (flag) ans-=2;

	if (flag&&a[n+1].x==a[n+1].y) ans++;

	cout<<ans<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.4359997980617x):
#include <bits/stdc++.h>

using namespace std;

 

#define endl '\n'

#define die(x) return cout << x << endl, 0

#define all(v) v.begin(), v.end()

#define sz(x) (int)(x.size())

const int N = 1e5 + 5;



int A[N], B[N], P[N];



int GetPar(int v) { return P[v] == v ? v : P[v] = GetPar(P[v]); }

void Merge(int v, int u) {

	v = GetPar(v), u = GetPar(u);

	if (u == v) return;

	P[v] = u;

}



int main() {

 

	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);



	int n, x = 0, ans = 0; cin >> n;

	vector<int> a, b;

	for (int i = 0; i < n; i++) cin >> A[i], x ^= A[i];

	A[n] = x,  x = 0;

	for (int i = 0; i < n; i++) cin >> B[i], x ^= B[i];

	B[n] = x, x = 0;

	for (int i = 0; i < n; i++) if (A[i] != B[i]) a.push_back(A[i]), b.push_back(B[i]), ans++;

	a.push_back(A[n]), b.push_back(B[n]);

	sort(all(a)), sort(all(b));

	if (a != b) die(-1);

	a.resize(unique(all(a)) - a.begin()), b.resize(unique(all(b)) - b.begin());

	for (int i = 0; i < sz(a); i++) P[i] = i;

	for (int i = 0; i <= n; i++) if (A[i] != B[i]) {

		int v = lower_bound(all(a), A[i]) - a.begin();

		int u = lower_bound(all(b), B[i]) - b.begin();

		Merge(v, u);

	}

	for (int i = 0; i < sz(a); i++) ans += (GetPar(i) == i);

	cout << ans - 1 << endl;



	return 0;

}
// Slower C++ solution:
#include<stdio.h>

#include<algorithm>

#include<string.h>

#include<math.h>

#include<stdlib.h>

#include<bitset>

#include<map>

#include<queue>

#include<vector>

#include<set>

#include<iostream>

#include<string>

#define ll long long

#define N 505

#define For(i,x,y) for(int i=x;i<=y;i++)

#define Rep(i,x,y) for(int i=x;i>=y;i--)

#define Forn(i,x,y) for(int i=x;i<y;i++)

using namespace std;

int read(){int x=0,f=0;char ch=getchar();for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1;for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-48;return f?-x:x;}

void write(int x){if(x<10) putchar(x+48);else write(x/10),putchar(x%10+48);}

void writeln(int x){if(x<0) putchar('-'),x=-x;write(x);putchar('\n');}

int n,m;

char s[N][N];

int main(){

	n=read();m=read();

	For(i,1,n) scanf("%s",s[i]+1);

	For(i,1,n) if(i&1){

		For(j,1,m-1) cout<<"#";

		cout<<'.'<<endl;

	}else{

		For(j,1,m) if(s[i][j]=='#'||j==1) cout<<'#';else cout<<".";

		cout<<endl;

	}

	For(i,1,n) if(!(i&1)){

		cout<<".";

		For(j,2,m) cout<<"#";

		cout<<endl;

	}else{

		For(j,1,m) if(s[i][j]=='#'||j==m) cout<<"#";else cout<<".";

		cout<<endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.927810642656025x):
#include<cstdio>

#include<algorithm>

#include<cstring>

#define MN 505

using namespace std;

char ch[MN][MN];int n,m;bool a[MN][MN],b[MN][MN];

int main(){

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++)scanf("%s",ch[i]+1);

	for(int i=1;i<=n;i++)a[i][1]=b[i][m]=1;

//	for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)printf("%c",a[i][j]?'#':'.');puts("");}

//	for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)printf("%c",b[i][j]?'#':'.');puts("");}

	for(int i=1;i*2<=n;i++){

		for(int j=2;j<m;j++)a[i*2-1][j]=b[i*2][j]=1;

	}

	if(n%2)for(int i=2;i<m;i++)a[n][i]=1;

	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)a[i][j]|=(ch[i][j]=='#'),b[i][j]|=(ch[i][j]=='#');

	for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)printf("%c",a[i][j]?'#':'.');puts("");}

	for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)printf("%c",b[i][j]?'#':'.');puts("");}

}