#include using namespace std; typedef long long ll; const int N = 1e5 + 5; int n, a[N], b[N], c[N], d[N], cnt[N], pos[N]; struct BIT { int t[N]; void modify(int x, int v) {for (; x <= n; x += (x & -x)) t[x] += v;} int query(int x) {int ans = 0; for (; x; x -= (x & -x)) ans += t[x]; return ans;} } T; bool check() { sort(c + 1, c + n + 1), sort(d + 1, d + n + 1); for (int i = 1; i <= n; i++) if (c[i] != d[i]) return 0; for (int i = 1; i <= n; i++) { int x = lower_bound(c + 1, c + n + 1, a[i]) - c; a[i] = x + cnt[x], cnt[x]++, pos[a[i]] = i; } for (int i = 1; i <= n; i++) cnt[i] = 0; for (int i = 1; i <= n; i++) { int x = lower_bound(c + 1, c + n + 1, b[i]) - c; b[i] = x + cnt[x], cnt[x]++, b[i] = pos[b[i]]; } return 1; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); 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]) cout << "-1\n", exit(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++) c[i] = a[i], d[i] = b[i]; if (!check()) cout << "-1\n", exit(0); ll ans = 0; for (int i = 1; i <= n; i++) ans += (i - 1) - T.query(b[i] - 1), T.modify(b[i], 1); cout << ans << '\n'; return 0; }