#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f2f1f0f;
const long long LINF = 1ll * INF * INF;
int N;
vector<int> A, B;
vector<int> sA, sB;
void input() {
  cin >> N;
  int all = 0;
  all = 0;
  for (int(i) = 0; (i) < (int)(N); (i)++) {
    int x;
    scanf("%d", &x);
    all ^= x;
    A.push_back(x);
  }
  A.push_back(all);
  all = 0;
  for (int(i) = 0; (i) < (int)(N); (i)++) {
    int x;
    scanf("%d", &x);
    all ^= x;
    B.push_back(x);
  }
  B.push_back(all);
  N++;
  sA = A;
  sB = B;
  sort((sA).begin(), (sA).end());
  sort((sB).begin(), (sB).end());
}
const int MAX_N = 1e5 + 100;
vector<int> Ed[MAX_N];
int main() {
  input();
  bool isSame = true;
  for (int(i) = 0; (i) < (int)(N); (i)++)
    if (sA[i] != sB[i]) isSame = false;
  if (!isSame) {
    puts("-1");
    return 0;
  }
  sA.erase(unique((sA).begin(), (sA).end()), sA.end());
  int diff = 0, comp = 0;
  for (int(i) = 0; (i) < (int)(N); (i)++) {
    A[i] = lower_bound((sA).begin(), (sA).end(), A[i]) - sA.begin();
    B[i] = lower_bound((sA).begin(), (sA).end(), B[i]) - sA.begin();
    if (A[i] != B[i]) {
      Ed[A[i]].push_back(B[i]);
      diff++;
    }
  }
  if (diff == 0) {
    printf("0\n");
    return 0;
  }
  if (A[N - 1] == B[N - 1]) comp++;
  int sN = ((int)(sA).size());
  vector<bool> vis(sN, false);
  queue<int> Q;
  for (int i = 0; i < sN; i++) {
    if (vis[i] == true) continue;
    if (Ed[i].size() == 0) continue;
    Q.push(i);
    vis[i] = true;
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      for (int w : Ed[v])
        if (vis[w] == false) {
          vis[w] = true;
          Q.push(w);
        }
    }
    comp++;
  }
  printf("%d\n", diff - 1 + comp - 1);
  return 0;
}
