#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-') ch = getchar();
  if (ch == '-') f = -1, ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f == 1 ? x : -x;
}
const int N = 100005;
int n;
int a[N], b[N];
int A[N], B[N];
bool check() {
  int m = n + 1;
  A[m] = B[m] = 0;
  for (int i = 1; i <= n; i++) {
    A[i] = a[i];
    B[i] = b[i];
    A[m] ^= a[i];
    B[m] ^= b[i];
  }
  sort(A + 1, A + m + 1);
  sort(B + 1, B + m + 1);
  for (int i = 1; i <= m; i++)
    if (A[i] != B[i]) return 0;
  return 1;
}
void clr() {
  for (int i = 1; i <= n; i++) A[i] = a[i], B[i] = b[i];
  int t = 0;
  for (int i = 1; i <= n; i++)
    if (A[i] != B[i]) a[++t] = A[i], b[t] = B[i];
  n = t;
}
int Ha[N], hs = 0;
void Hash() {
  for (int i = 1; i <= n; i++) Ha[++hs] = a[i];
  sort(Ha + 1, Ha + hs + 1);
  hs = unique(Ha + 1, Ha + hs + 1) - Ha - 1;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(Ha + 1, Ha + hs + 1, a[i]) - Ha;
    b[i] = lower_bound(Ha + 1, Ha + hs + 1, b[i]) - Ha;
  }
}
vector<int> p[N];
int vis[N];
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read();
  for (int i = 1; i <= n; i++) b[i] = read();
  if (!check()) return puts("-1"), 0;
  int s = 0, s2 = 0;
  for (int i = 1; i <= n; i++) s ^= a[i], s2 ^= b[i];
  clr();
  Ha[++hs] = s;
  Hash();
  s = lower_bound(Ha + 1, Ha + hs + 1, s) - Ha;
  s2 = lower_bound(Ha + 1, Ha + hs + 1, s2) - Ha;
  for (int i = 1; i <= hs; i++) p[i].clear();
  for (int i = 1; i <= n; i++) p[b[i]].push_back(i);
  memset(vis, 0, sizeof vis);
  int ans = 0;
  if (s != s2) {
    vis[s2] = 1;
    while (1) {
      int now = p[s].back();
      ans += s != a[now];
      if (vis[now]) break;
      p[s].pop_back();
      vis[now] = 1;
      swap(a[now], s);
    }
  }
  for (int i = 1; i <= n; i++) {
    int now = i;
    if (!vis[now]) {
      ans += s != a[now];
      vis[now] = 1;
      swap(s, a[now]);
      while (1) {
        int now = p[s].back();
        ans += s != a[now];
        if (vis[now]) break;
        p[s].pop_back();
        vis[now] = 1;
        swap(a[now], s);
      }
    }
  }
  cout << ans;
  return 0;
}
