#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;

map <int, int> Map;
map <int, vector <int> > pos;

int n, m, x, y, A[N], B[N], C[N];

#define Exit exit(0 * puts("-1"))

map <int, int> fa;

int getf(int x) {
  if(fa[x] == x) return x;
  return fa[x] = getf(fa[x]);
}

int main() {
  scanf("%d", &n);
  int cur = 0;
  for(int i = 1; i <= n; ++ i)
    scanf("%d", &A[i]), ++ Map[A[i]], cur ^= A[i];
  A[n + 1] = cur;
  int now = 0;
  ++ Map[cur];
  for(int i = 1; i <= n; ++ i) {
    scanf("%d", &B[i]);
    now ^= B[i];
    -- Map[B[i]];
    if(Map[B[i]] < 0) {
      //assert(0);
      Exit;
    }
  }
  //throw;
  B[++ n] = now;
  for(int i = 1; i <= n; ++ i) if(A[i] != B[i] || i == n){
      fa[A[i]] = A[i];
      fa[B[i]] = B[i];
  }
  int tot = 0;
  for(int i = 1; i < n; ++ i) tot += (A[i] != B[i]);
  if(!tot) {
    puts("0");
    return 0;
  }
  for(int i = 1; i <= n; ++ i) {
    if(A[i] != B[i] || i == n) {
      fa[getf(A[i])] = getf(B[i]);
    }
  }
  //cerr << "HERE" << endl;
  set <int> S;
  for(int i = 1; i <= n; ++ i) {
    if(fa.count(A[i])) S.insert(getf(A[i]));
    if(fa.count(B[i])) S.insert(getf(B[i]));
  }
  tot += S.size();
  -- tot;
  cout << tot << endl;
}
