#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 100;
map<int, int> ma;
int bza[N], bzb[N];
long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
int a[N], b[N], ans;
int ta[N], tb[N], ls[N];
void lisan() {
  sort(ls + 1, ls + ls[0] + 1);
  int now = 0;
  ma[ls[1]] = ++now;
  for (int i = 2; i <= ls[0]; ++i) {
    if (ls[i] != ls[i - 1]) ma[ls[i]] = ++now;
  }
}
vector<int> pos[N];
int nn;
int fa[N];
struct node {
  int u, v, nxt;
} e[N];
int ejs;
int K;
int head[N];
void add(int u, int v) {
  e[++ejs].u = u;
  e[ejs].v = v;
  e[ejs].nxt = head[u];
  head[u] = ejs;
}
int vis[N];
int dfs(int u) {
  if (vis[u]) return 1;
  vis[u] = 1;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].v;
    return dfs(v) - (ta[u] == K ? 1 : 0);
  }
  return 0;
}
int main() {
  int n = read();
  for (int i = 1; i <= n; ++i) a[i] = read(), K ^= a[i];
  for (int i = 1; i <= n; ++i) {
    b[i] = read();
    if (a[i] != b[i]) {
      ta[++nn] = a[i];
      tb[nn] = b[i];
      ls[++ls[0]] = a[i];
      ls[++ls[0]] = b[i];
    }
  }
  lisan();
  K = ma[K];
  for (int i = 1; i <= nn; ++i)
    ta[i] = ma[ta[i]], tb[i] = ma[tb[i]], pos[tb[i]].push_back(i);
  for (int i = 1; i <= nn; ++i) bza[ta[i]]++, bzb[tb[i]]++;
  int js = 0;
  bza[K]++;
  for (int i = 1; i <= nn; ++i) {
    js += abs(bza[ta[i]] - bzb[ta[i]]);
  }
  if (js > 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= nn; ++i) {
    if (pos[ta[i]].size() != 0) {
      add(i, pos[ta[i]][pos[ta[i]].size() - 1]);
      pos[ta[i]].pop_back();
    }
  }
  for (int i = 1; i <= nn; ++i) {
    if (!vis[i]) ans += max(0, dfs(i));
  }
  cout << nn + ans + 1;
  return 0;
}
