// Problem: C - Swaps 2
// Contest: AtCoder - AtCoder Regular Contest 120
// URL: https://atcoder.jp/contests/arc120/tasks/arc120_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcc,abm,mmx,avx,avx2,tune=native")
#include <stdio.h>
#include <string.h>
#include <set>
#include <map>
#include <algorithm>
#include <queue>
#define od(x) printf("%d",x)
#define odb(x) printf("%d ",x)
#define odl(x) printf("%d\n",x)
#define odp(x,y) printf("%d %d\n",x,y)
#define ol(x) puts("")
#define old(x) printf("%lld",x)
#define oldb(x) printf("%lld ",x)
#define oldl(x) printf("%lld\n",x)
#define oldp(x,y) printf("%lld %lld\n",x,y)
#define int long long
inline int read()
{
	int num=0,f=1;char c=getchar();
	while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
	while(c>47&&c<58)num=num*10+(c^48),c=getchar();
	return num*f;
}
inline int re1d()
{
	char c=getchar();
	while(c<48||c>49)c=getchar();
	return c&1;
}
inline int min(int a,int b){return a>b?b:a;}
inline int max(int a,int b){return a<b?b:a;}
int n;std::map<int,int>m;std::set<int>x[200005],y[500005];
int a[200005],b[200005],c[200005];
namespace szsz{
	int a[200005];
	inline int lowbit(int x){return x&-x;}
	inline void add(int x,int p){while(x<=n)a[x]+=p,x+=lowbit(x);}
	inline int query(int x){int res=0;while(x)res+=a[x],x-=lowbit(x);return res;}
}using szsz::add;using szsz::query;
signed main()
{
	n=read();
	int A=0,B=0;
	for(int i=1;i<=n;i++)A+=a[i]=read()+i;
	for(int i=1;i<=n;i++)B+=b[i]=read()+i;
	if(A!=B)return puts("-1")&0;
	int cc=0;
	for(int i=1;i<=n;i++)
	{
		if(m.count(a[i]));else m[a[i]]=++cc;
		x[m[a[i]]].insert(i);
	}
	for(int i=1;i<=n;i++)
	{
		if(m.count(b[i]))y[m[b[i]]].insert(i);
		else return puts("-1")&0;
	}
	int ans=0;
	for(int i=1;i<=cc;i++)
	{
		if(x[i].size()==y[i].size())
		{
			std::set<int>::iterator A=x[i].begin(),B=y[i].begin();
			for(;A!=x[i].end();A++,B++)c[*B]=*A;
		}
		else return puts("1")&0;
	}
	A=0;
	for(int i=1;i<=n;i++)A+=(i-1)-query(c[i]),add(c[i],1);
	oldl(A);
	return 0;
}
/*
*   C. ORXOR.cpp
*   Created : 28/03/21 10.54.57
*/

#include <bits/stdc++.h>

using namespace std;

void run_case(){
   int n;
   cin >> n;
   vector<long long> v(n);
   for(long long &i : v){
       cin >> i;
   }
   long long res = LLONG_MAX;
   for(int i = 0; i < (1 << (n - 1)); i++){
       long long Or = 0;
       long long Xor = 0;
       for(int j = 0; j < n; j++){
            Or |= v[j];
           if(i >> j & 1){
               Xor ^= Or;
               Or = 0;
           }
       }
       Xor ^= Or;
       res = min(res, Xor);
   }
   cout << res;
}

int main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);

   int tt = 1;
//    cin >> tt;
   while(tt--){
      run_case();
   }
   return 0;
}