#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N, A[MAXN], B[MAXN], P[MAXN];
map<int, stack<int>> pos;
bool vis[MAXN];
set<int> Q;
int main() {
  int sum = 0;
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &A[i]);
    sum ^= A[i];
  }
  for (int i = 1; i <= N; i++) {
    scanf("%d", &B[i]);
    pos[B[i]].push(i);
  }
  for (int i = 1; i <= N; i++)
    if (A[i] != B[i]) Q.insert(i);
  int ans = 0;
  int val = sum;
  for (;;) {
    int u = -1;
    if (pos[val].empty()) {
      for (auto v = Q.begin(); v != Q.end();) {
        if (vis[*v]) {
          v = Q.erase(v);
          continue;
        } else if (A[*v] != val) {
          u = *v;
          break;
        }
        v++;
      }
      if (Q.empty()) break;
    } else {
      u = pos[val].top();
      pos[val].pop();
    }
    if (u == -1) {
      puts("-1");
      return 0;
    }
    swap(A[u], val);
    if (A[u] == B[u]) vis[u] = true;
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}
