#include <bits/stdc++.h>
using namespace std;
void output(int x) {
  cout << x << endl;
  exit(0);
}
void solve(long long N, vector<long long> a, vector<long long> b) {
  if (a == b) output(0);
  long long x = 0;
  for (int i = 0; i < a.size(); i++) x ^= a[i];
  bool in = count(a.begin(), a.end(), x) != 0;
  multiset<int> mB(b.begin(), b.end());
  int eX = -1;
  for (int i : a) {
    auto it = mB.find(i);
    if (it != mB.end()) {
      mB.erase(mB.find(i));
    } else {
      eX = i;
    }
  }
  if (mB.size() >= 2) output(-1);
  if (mB.size() == 1) {
    if ((*mB.begin()) != x) output(-1);
  } else {
    int ans = 0;
    for (int i = 0; i < a.size(); i++) {
      if (a[i] != b[i]) {
        ans++;
      }
    }
    if (!in) {
      output(ans + 1);
    } else {
      for (int i = 0; i < a.size(); i++) {
        if (b[i] == x and a[i] != x) {
          output(ans);
        }
      }
    }
  }
}
int main() {
  long long N;
  scanf("%lld", &N);
  vector<long long> a(N - 1 + 1);
  vector<long long> b(N - 1 + 1);
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 0; i <= N - 1; i++) {
    scanf("%lld", &b[i]);
  }
  solve(N, a, b);
  return 0;
}
