#include <bits/stdc++.h>
using namespace std;
int a[100100], b[100100];
int ta[100100], tb[100100];
int n;
map<int, int> mp;
int cnt;
vector<int> g[100100];
bool ok[100100], vis[100100];
void dfs(int u) {
  vis[u] = true;
  for (int i = 0; i < (int)g[u].size(); ++i) {
    int v = g[u][i];
    if (!vis[v]) dfs(v);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
  for (int i = 1; i <= n; ++i) {
    a[0] ^= a[i];
    b[0] ^= b[i];
    ta[i] = a[i];
    tb[i] = b[i];
    if (i == n) {
      ta[0] = a[0];
      tb[0] = b[0];
    }
  }
  sort(ta, ta + n + 1);
  sort(tb, tb + n + 1);
  for (int i = 0; i <= n; ++i)
    if (ta[i] != tb[i]) {
      puts("-1");
      exit(0);
    }
  for (int i = 0; i <= n; ++i) {
    if (!mp[a[i]]) mp[a[i]] = ++cnt;
    if (!mp[b[i]]) mp[b[i]] = ++cnt;
    a[i] = mp[a[i]];
    b[i] = mp[b[i]];
  }
  int ans = 0;
  for (int i = 0; i <= n; ++i) {
    if (a[i] == b[i]) continue;
    if (i > 0) ++ans;
    g[a[i]].push_back(b[i]);
    ok[a[i]] = true;
  }
  for (int i = 0; i <= n; ++i)
    if (ok[i] && !vis[i]) {
      dfs(i);
      ++ans;
    }
  if (ans > 0) ans--;
  printf("%d\n", ans);
  return 0;
}
