#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100010;
int n, A[Maxn], B[Maxn], Start, N;
struct node {
  int x, y;
};
node Node[Maxn], Node2[Maxn];
bool Use, Vis[Maxn];
int Cnt;
bool cmp(node x, node y) { return x.y < y.y; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &A[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &B[i]);
  for (int i = 1; i <= n; ++i) Start ^= A[i];
  for (int i = 1; i <= n; ++i)
    if (A[i] != B[i]) {
      ++N;
      A[N] = A[i];
      B[N] = B[i];
    }
  if (N == 0) {
    printf("0\n");
    return 0;
  }
  for (int i = 1; i <= N; ++i) Node[i] = (node){A[i], B[i]};
  sort(Node + 1, Node + N + 1, cmp);
  for (int i = 1; i <= N; ++i) Node2[i] = (node){i, Node[i].x};
  sort(Node2 + 1, Node2 + N + 1, cmp);
  for (int i = 1; i <= N; ++i)
    if (Node2[i].y != Node[i].y) {
      Node2[i].y = Start;
      break;
    }
  sort(Node2 + 1, Node2 + N + 1, cmp);
  for (int i = 1; i <= N; ++i)
    if (Node2[i].y != Node[i].y) {
      printf("-1\n");
      return 0;
    }
  for (int i = 1; i <= N; ++i)
    if (Node[i].y == Start) {
      Use = true;
      break;
    }
  Cnt = 0;
  memset(Vis, 0, sizeof(Vis));
  for (int i = 1; i <= N; ++i) {
    if (Vis[i]) continue;
    Cnt++;
    for (int j = i; !Vis[j]; j = Node2[j].x) Vis[j] = true;
  }
  if (Use)
    printf("%d\n", N + Cnt - 1);
  else
    printf("%d\n", N + Cnt);
  return 0;
}
