#include <bits/stdc++.h>
using namespace std;
int n, a[100000 + 5], b[100000 + 5];
int lsh[100000 + 5], lcnt;
int vis[100000 + 5], setfa[100000 + 5];
int ru[100000 + 5], chu[100000 + 5];
bool del[100000 + 5];
void Init() {
  for (int i = 0; i <= 100000 + 3; i++) setfa[i] = i;
}
int Findfa(int x) {
  if (x == setfa[x]) return x;
  return setfa[x] = Findfa(setfa[x]);
}
void Union(int x, int y) {
  int fx = Findfa(x), fy = Findfa(y);
  if (fx == fy) return;
  setfa[fx] = fy;
}
int main() {
  Init();
  scanf("%d", &n);
  int x = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), x ^= a[i];
  a[n + 1] = x;
  x = 0;
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), x ^= b[i];
  b[++n] = x;
  for (int i = 1; i <= n; i++) lsh[++lcnt] = a[i], lsh[++lcnt] = b[i];
  sort(lsh + 1, lsh + 1 + lcnt);
  lcnt = unique(lsh + 1, lsh + 1 + lcnt) - lsh - 1;
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(lsh + 1, lsh + 1 + lcnt, a[i]) - lsh;
    b[i] = lower_bound(lsh + 1, lsh + 1 + lcnt, b[i]) - lsh;
  }
  int ans = 0;
  for (int i = 1; i <= n - 1; i++)
    if (a[i] != b[i]) {
      ru[a[i]]++, chu[b[i]]++;
      Union(a[i], b[i]);
      ans++;
    } else
      del[i] = true;
  if (ans == 0) {
    printf("0\n");
    return 0;
  }
  for (int i = 1; i <= n - 1; i++) {
    int f = Findfa(i);
    if (ru[i] == chu[i] - 1)
      vis[f]++;
    else if (ru[i] == chu[i] + 1)
      vis[f]++;
    else if (ru[i] != chu[i]) {
      printf("-1\n");
      return 0;
    }
  }
  int cnt = 0;
  for (int i = 1; i <= n - 1; i++) {
    if (vis[i] > 2) {
      printf("-1\n");
      return 0;
    }
    if (Findfa(i) == i && del[i] == false) cnt++;
  }
  if (cnt > 1) cnt--;
  if (a[n] == b[n] && ru[a[n]] == 0 && chu[b[n]] == 0) cnt++;
  ans += cnt;
  printf("%d\n", ans);
  return 0;
}
