#include #include #include #include #include using namespace std; const int N = 1e5 + 5; int n; long long a[N] = {0}, b[N] = {0}; int m; int t[N] = {0}; int lowbit(int x) { return x & -x; } void mdf(int x, int v) { while (x <= n) t[x] += v, x += lowbit(x); } int qry(int x) { int ans = 0; while (x > 0) ans += t[x], x -= lowbit(x); return ans; } long long ta[N] = {0}, tb[N] = {0}; int cnt[N] = {0}; int pos[N] = {0}; bool init() { 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 false; for (int i = 1; i <= n; i++) { int x = lower_bound(ta + 1, ta + n + 1, a[i]) - ta; a[i] = x + cnt[x]++; // cout << a[i] << " " << x << " " << cnt[x] << endl; pos[a[i]] = i; } /// cout << endl; for (int i = 1; i <= n; i++) cnt[i] = 0; for (int i = 1; i <= n; i++) { int x = lower_bound(ta + 1, ta + n + 1, b[i]) - ta; b[i] = x + cnt[x]++; // cout << b[i] << " "; b[i] = pos[b[i]]; } // cout << endl; return true; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; if (a[1] != b[1]) { printf("-1\n"); return 0; } for (int i = 1; i < n; i++) a[i] ^= a[i + 1], b[i] ^= b[i + 1], swap(a[i], b[i]); n--; for (int i = 1; i <= n; i++) ta[i] = a[i], tb[i] = b[i]; if (!init()) { printf("-1\n"); return 0; } long long ans = 0ll; for (int i = 1; i <= n; i++) { ans += (i - 1) - qry(b[i] - 1); mdf(b[i], 1); } cout << ans << endl; return 0; }