#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 99;
int n, c, x, mv, ans, res, a[N], b[N], mark[N];
map<int, int> t, tn;
vector<int> g[N];
void dfs(int x) {
  mark[x] = 1;
  for (int i = 0; i < g[x].size(); i++)
    if (!mark[g[x][i]]) dfs(g[x][i]);
  ans += g[x].size();
}
int main() {
  cin >> n;
  for (int i = 1; i < n + 1; i++) {
    scanf("%d", &a[i]);
    ;
    a[0] ^= a[i];
    t[a[i]]++;
  }
  t[a[0]]++;
  for (int i = 1; i < n + 1; i++) {
    scanf("%d", &b[i]);
    ;
    b[0] ^= b[i];
  }
  for (int i = 0; i < n + 1; i++) {
    t[b[i]]--;
    if (t[b[i]] < 0) return cout << -1, 0;
  }
  for (int i = 0; i < n + 1; i++) {
    if (!tn[a[i]]) {
      c++;
      tn[a[i]] = c;
    }
  }
  for (int i = 0; i < n + 1; i++) {
    a[i] = tn[a[i]];
    b[i] = tn[b[i]];
    if (a[i] != b[i]) g[a[i]].push_back(b[i]);
  }
  for (int i = 1; i < n + 1; i++)
    if (b[i] == a[0] && b[0] == a[0]) mv = 1;
  for (int i = 1; i < n + 2; i++)
    if (!mark[i] && int(g[i].size())) {
      res++;
      dfs(i);
    }
  cout << ans + res - (a[0] != b[0]) * 2 - mv;
}
