#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T& a, const T& b) {
  return a < b ? (a = b, true) : false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  return b < a ? (a = b, true) : false;
}
int N;
vector<int> A, B;
int main() {
  scanf("%d", &N);
  N++;
  A.assign(N, 0);
  B.assign(N, 0);
  {
    int xorAll = 0;
    for (int i = (int)(0); i < (int)(N - 1); i++) {
      scanf("%d", &A[i]);
      xorAll ^= A[i];
    }
    A[N - 1] = xorAll;
  }
  {
    int xorAll = 0;
    for (int i = (int)(0); i < (int)(N - 1); i++) {
      scanf("%d", &B[i]);
      xorAll ^= B[i];
    }
    B[N - 1] = xorAll;
  }
  map<int, vector<int> > rB;
  for (int i = (int)(0); i < (int)(N - 1); i++)
    if (A[i] != B[i]) {
      rB[B[i]].push_back(i);
    }
  int ans = 0, cnt = 0, idx = 0;
  for (int i = (int)(0); i < (int)(N - 1); i++) cnt += A[i] == B[i];
  while (cnt < N - 1) {
    int x = A[N - 1];
    if (rB[x].empty() == false) {
      swap(A[rB[x].back()], A[N - 1]);
      rB[x].pop_back();
      cnt++;
      ans++;
    } else if (A[N - 1] == B[N - 1]) {
      while (idx < N - 1 && A[idx] == B[idx]) idx++;
      if (idx == N - 1) {
        puts("-1");
        return 0;
      }
      swap(A[idx], A[N - 1]);
      ans++;
    } else {
      puts("-1");
      return 0;
    }
  }
  printf("%d\n", ans);
  return 0;
}
