#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 9;
const long long mod = 1e9 + 7;
vector<int> g[maxn];
map<int, int> num, mp;
set<int> st;
int a[maxn], b[maxn];
bool vis[maxn];
void add(int n) {
  num.clear();
  for (int i = 0; i < n; i++) num[a[i]]++;
}
void clr(int n) {
  for (int i = 0; i < n; i++) num[b[i]]--;
}
void clearg() {
  for (int i = 0; i < maxn; i++) g[i].clear();
}
int dfs(int v) {
  vis[v] = true;
  int res = 1;
  for (auto u : g[v])
    if (!vis[u]) res += dfs(u);
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int X = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    X ^= a[i];
  }
  bool f = false;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    if (b[i] == X) f = true;
  }
  add(n);
  clr(n);
  bool ok = true;
  for (auto x : num)
    if (x.second != 0) ok = false;
  int ans = -1;
  if (ok) {
    for (int i = 0; i < n; i++) st.insert(a[i]);
    int id = 0;
    for (auto x : st) mp[x] = id++;
    for (int i = 0; i < n; i++) {
      int f1 = mp[a[i]], f2 = mp[b[i]];
      if (f1 - f2) g[f1].push_back(f2);
    }
    int cur = 0;
    for (int i = 0; i < n; i++)
      if (!vis[i] && g[i].size()) cur += dfs(i) + 1;
    ans = cur;
  }
  if (f) {
    add(n);
    bool ff = true;
    int pos;
    for (int i = 0; i < n; i++) {
      if (b[i] == X && ff) {
        ff = false;
        pos = i;
        continue;
      }
      num[b[i]]--;
    }
    int cnt = 0, val;
    ok = true;
    for (auto x : num) {
      if (x.second > 1 || x.second < 0) return cout << "-1\n", 0;
      if (x.second) {
        cnt++;
        val = x.first;
      }
    }
    if (cnt > 1) return cout << "-1\n", 0;
    b[pos] = val;
    memset(vis, 0, sizeof vis);
    st.clear();
    mp.clear();
    clearg();
    for (int i = 0; i < n; i++) st.insert(a[i]);
    int id = 0;
    for (auto x : st) mp[x] = id++;
    for (int i = 0; i < n; i++) {
      int f1 = mp[a[i]], f2 = mp[b[i]];
      if (f1 - f2) g[f1].push_back(f2);
    }
    int cur = 0;
    for (int i = 0; i < n; i++)
      if (!vis[i] && g[i].size()) cur += dfs(i) + 1;
    ans = ((ans == -1) ? cur - 1 : min(ans, cur - 1));
  }
  cout << ans << " \n";
}
