#include using namespace std; const int N = 1e5 + 7; int p[N], n, bit[N]; std::pair a[N], b[N]; void add(int x) { while(x) { ++bit[x]; x -= x & -x; } } int ask(int x) { int ret = 0; while(x <= n) { ret += bit[x]; x += x & -x; } return ret; } int main() { scanf("%d", &n); for(int i = 0, pre = 0, x; i < n; ++i) { scanf("%d", &x); a[i] = {x ^ pre, i}; pre = x; } for(int i = 0, pre = 0, x; i < n; ++i) { scanf("%d", &x); b[i] = {x ^ pre, i}; pre = x; } std::sort(a + 1, a + n); std::sort(b + 1, b + n); for(int i = 0; i < n; ++i) { if(a[i].first != b[i].first) { puts("-1"); return 0; } p[a[i].second] = b[i].second; } long long ans = 0; for(int i = 1; i < n; ++i) { ans += ask(p[i]); add(p[i]); } printf("%lld\n", ans); return 0; }