#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int N, M, A[MAXN], B[MAXN], P[MAXN];
vector<int> pos[MAXN];
int pcnt[MAXN];
int hjb[MAXN];
set<int> visB;
bool vis[MAXN];
int val[MAXN];
vector<int> adj[MAXN];
int sum, cnt, flag;
void dfs(int u) {
  cnt += hjb[u];
  vis[u] = true;
  if (u == sum && A[N + 1] != B[N + 1])
    flag = -1;
  else if (u == sum)
    flag = 0;
  for (auto v : adj[u]) {
    if (vis[v]) continue;
    dfs(v);
  }
}
int main() {
  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]);
    visB.insert(B[i]);
  }
  B[N + 1] = sum;
  for (int i = 1; i <= N; i++)
    if (visB.find(A[i]) == visB.end()) {
      B[N + 1] = A[i];
      break;
    }
  A[N + 1] = sum;
  for (int i = 1; i <= N + 1; i++) {
    val[i * 2 - 1] = A[i];
    val[i * 2] = B[i];
  }
  sort(val + 1, val + (N + 1) * 2 + 1);
  M = unique(val + 1, val + (N + 1) * 2 + 1) - val - 1;
  for (int i = 1; i <= N + 1; i++) {
    A[i] = find(val + 1, val + M + 1, A[i]) - val;
    B[i] = find(val + 1, val + M + 1, B[i]) - val;
  }
  sum = find(val + 1, val + M + 1, sum) - val;
  for (int i = 1; i <= N + 1; i++)
    if (A[i] != B[i]) {
      hjb[A[i]]++;
      pos[B[i]].push_back(i);
      pcnt[B[i]]++;
    }
  for (int i = 1; i <= N + 1; i++)
    if (A[i] != B[i]) {
      if (pcnt[A[i]] == 0) {
        puts("-1");
        return 0;
      }
      pcnt[A[i]]--;
      if (vis[A[i]]) continue;
      for (auto j : pos[B[i]]) adj[A[i]].push_back(B[j]);
      vis[A[i]] = true;
    }
  memset(vis, 0, sizeof vis);
  int ans = 0;
  for (int i = 1; i <= N + 1; i++)
    if (A[i] != B[i] && !vis[A[i]]) {
      cnt = 0, flag = 1;
      dfs(A[i]);
      ans += cnt + flag;
    }
  printf("%d\n", ans);
  return 0;
}
