#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 20;
const double eps = 1e-8;
int n;
int a[N];
int tmp[N];
int b[N];
int vis[N];
vector<int> edg[N];
void dfs(int x) {
  if (vis[x]) return;
  vis[x] = 1;
  for (auto i : edg[x]) dfs(i);
  return;
}
int main() {
  while (cin >> n) {
    memset((vis), 0, sizeof(vis));
    int x, y;
    int flag = 0;
    map<int, int> q;
    for (int i = 1; i <= n; i++) {
      scanf("%d", a + i);
      tmp[i] = a[i];
      x ^= a[i];
      q[a[i]]++;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d", b + i);
      y ^= b[i];
      q[b[i]]--;
      if (q[b[i]] < 0) flag = 1;
    }
    q[x]++;
    a[0] = tmp[0] = x;
    b[0] = y;
    if (flag) {
      printf("-1\n");
      continue;
    }
    long long ans = 0;
    sort(tmp, tmp + n + 1);
    int pos = unique(tmp, tmp + n + 1) - tmp;
    for (int i = 1; i <= n; i++) {
      if (b[i] != a[i]) {
        int p = lower_bound(tmp, tmp + pos, a[i]) - tmp;
        int np = lower_bound(tmp, tmp + pos, b[i]) - tmp;
        edg[p].push_back((np));
        ans++;
      }
    }
    for (auto i : q) {
      if (i.second != 0) {
        int p = lower_bound(tmp, tmp + pos, a[0]) - tmp;
        int np = lower_bound(tmp, tmp + pos, i.first) - tmp;
        edg[p].push_back((np));
        ans++;
      }
    }
    for (int i = 0; i < pos; i++) {
      if (vis[i]) continue;
      dfs(i);
      if (edg[i].size() != 0) ans++;
    }
    printf("%d\n", ans - 2);
  }
  return 0;
}
