#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;
const int MAX_N = 100000;
vector<int> A, B;
int edge[MAX_N];
bool vis[MAX_N];
int dfs(int curr) {
  if (vis[curr]) return 0;
  vis[curr] = true;
  return 1 + dfs(edge[curr]);
}
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;
  }
  map<int, vector<int> > rB;
  {
    int xorAll = 0;
    for (int i = (int)(0); i < (int)(N - 1); i++) {
      scanf("%d", &B[i]);
      xorAll ^= B[i];
      rB[B[i]].push_back(i);
    }
    B[N - 1] = xorAll;
    rB[xorAll].push_back(N - 1);
  }
  for (int i = (int)(0); i < (int)(N); i++) {
    if (rB[A[i]].empty()) {
      puts("-1");
      return 0;
    }
    edge[i] = rB[A[i]].back();
    rB[A[i]].pop_back();
  }
  int ans = dfs(N - 1) - 1;
  for (int i = (int)(0); i < (int)(N); i++)
    if (vis[i] == false) {
      int t = dfs(i);
      if (t <= 1) continue;
      ans += t + 1;
    }
  printf("%d\n", ans);
  return 0;
}
