#include <bits/stdc++.h>
using namespace std;
using lint = long long;
vector<int> g[111111];
int cs[111111];
int cn[111111];
bool used[111111];
void dfs(int v, int c) {
  cs[c]++;
  cn[v] = c;
  used[v] = true;
  for (int i : g[v]) {
    if (!used[i]) {
      dfs(i, c);
    }
  }
}
void solve(istream& cin, ostream& cout) {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  int x = 0;
  for (int i = (0); i < int(n); ++i) {
    cin >> a[i];
    x ^= a[i];
  }
  a[n] = x;
  vector<int> b(n + 1);
  x = 0;
  for (int i = (0); i < int(n); ++i) {
    cin >> b[i];
    x ^= b[i];
  }
  b[n] = x;
  auto ax = a;
  auto bx = b;
  sort(ax.begin(), ax.end());
  sort(bx.begin(), bx.end());
  if (ax != bx) {
    cout << -1;
    return;
  }
  x = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      x++;
      int p = lower_bound(ax.begin(), ax.end(), a[i]) - ax.begin();
      int q = lower_bound(ax.begin(), ax.end(), b[i]) - ax.begin();
      g[p].push_back(q);
      g[q].push_back(p);
    }
  }
  if (x == 0) {
    cout << 0;
    return;
  }
  x--;
  int cc = 0;
  for (int i = 0; i <= n; i++) {
    if (!used[i]) {
      dfs(i, cc++);
    }
  }
  for (int i = 0; i < cc; i++) {
    if (cs[i] >= 2) {
      x++;
    }
  }
  if (cs[cn[n]] == 1) {
    x++;
  }
  cout << x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve(cin, cout);
  return 0;
}
