#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N, A[MAXN], B[MAXN], P[MAXN];
map<int, vector<int>> pos;
set<int> visB;
bool vis[MAXN];
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]);
    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++)
    if (A[i] != B[i]) pos[A[i]].push_back(i);
  for (int i = 1; i <= N + 1; i++)
    if (A[i] != B[i]) {
      if (pos[B[i]].empty()) {
        puts("-1");
        return 0;
      }
      P[i] = pos[B[i]].back();
      pos[B[i]].resize(pos[B[i]].size() - 1);
    }
  int ans = 0;
  for (int i = 1; i <= N + 1; i++)
    if (A[i] != B[i] && !vis[i]) {
      int u = i, cnt = 0, flag = 1;
      while (!vis[u]) {
        vis[u] = true;
        if (u == N + 1)
          flag = -1;
        else if (B[u] == sum)
          flag = 0;
        cnt++;
        u = P[u];
      }
      ans += cnt + flag;
    }
  printf("%d\n", ans);
  return 0;
}
