#include <bits/stdc++.h>
using namespace std;
long long pos = 0, n, ans = 0, tot = 0;
long long a[200005], b[200005];
map<long long, long long> mp;
int next[200005];
bool vis[200005];
bool flag2;
struct zero {
  int nxt, to;
} edge[400005];
int head[200005];
void add_edge(int a, int b) {
  edge[++tot].to = b;
  edge[tot].nxt = head[a];
  head[a] = tot;
}
void dfs(int k) {
  vis[k] = true;
  for (int i = head[k]; i; i = edge[i].nxt) {
    if (!vis[edge[i].to])
      dfs(edge[i].to);
    else {
      flag2 = true;
    }
  }
}
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &b[i]);
    mp[b[i]]--;
  }
  for (int i = 1; i <= n; i++) {
    pos ^= a[i];
    mp[a[i]]++;
  }
  mp[pos]++;
  bool flag1 = true;
  bool gg = true;
  for (int i = 1; i <= n; i++) {
    if (b[i] == pos && gg) {
      ans--;
      gg = false;
    }
    if (mp[b[i]] < 0) {
      flag1 = false;
      break;
    }
  }
  if (!flag1) {
    cout << -1;
    return 0;
  }
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      ans++;
      int k = 0;
      bool flag = false;
      while (k >= 0 && k <= n) {
        k = find(b + k + 1, b + n + 1, a[i]) - b;
        if (a[i] != b[k] || k > n || k < 0) break;
        add_edge(i, k);
      }
    } else
      vis[i] = true;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      flag2 = false;
      dfs(i);
      if (flag2) ans++;
    }
  cout << ans;
}
