#include <bits/stdc++.h>
using namespace std;
int a[100010], b[100010], n;
map<int, int> mp, mp1;
int cnt, tot, in[200010], dfn[200010], low[200010];
stack<int> st;
int use;
int vis[200010];
struct edge {
  int to, lt;
} e[200010];
int sz;
void add(int x, int y) {
  e[++tot].to = y;
  e[tot].lt = in[x];
  in[x] = tot;
}
void Tarjan(int x) {
  low[x] = dfn[x] = ++use;
  vis[x] = 1;
  st.push(x);
  for (int i = in[x]; i; i = e[i].lt) {
    int y = e[i].to;
    if (vis[y] == 1)
      low[x] = min(low[x], low[y]);
    else if (vis[y] == 0)
      Tarjan(y), low[x] = min(low[x], dfn[y]);
  }
  if (low[x] == dfn[x]) {
    sz++;
    while (!st.empty()) {
      int u = st.top();
      st.pop();
      vis[u] = -1;
      if (u == x) break;
    }
  }
}
int main() {
  int i, j, k, x = 0, ans = 1;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), x ^= a[i], mp[a[i]]++;
  mp[x]++;
  for (i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    if (!mp[b[i]])
      ans = 0;
    else
      mp[b[i]]--;
  }
  if (!ans) {
    printf("-1\n");
    return 0;
  } else {
    ans = 0;
    for (i = 1; i <= n; i++) {
      if (a[i] != b[i]) {
        ans++;
        if (!mp1[a[i]]) mp1[a[i]] = ++cnt;
        if (!mp1[b[i]]) mp1[b[i]] = ++cnt;
        add(mp1[a[i]], mp1[b[i]]);
      }
    }
    for (i = 1; i <= cnt; i++)
      if (!dfn[i]) Tarjan(i);
    if (ans)
      printf("%d\n", ans + sz - 1);
    else
      printf("0\n");
  }
  return 0;
}
