#include <bits/stdc++.h>
using namespace std;
const int N = 400005;
int n, s, cnt, num1, num2, ans;
bool ok;
int b[N], din[N], dout[N];
pair<int, int> a[N];
bool vis[N];
map<int, int> g, id;
vector<int> v[N];
bool sam() {
  for (int i = 1; i <= n; ++i)
    if (a[i].first != a[i].second) return 0;
  return 1;
}
void Discreting() {
  for (int i = 1; i <= n; ++i) b[++cnt] = a[i].first, b[++cnt] = a[i].second;
  b[++cnt] = s;
  sort(b + 1, b + 1 + cnt);
  cnt = unique(b + 1, b + 1 + cnt) - b - 1;
  for (int i = 1; i <= cnt; ++i) id[b[i]] = i;
}
void add(int x, int y) {
  ++dout[x], ++din[y], v[x].push_back(y);
  vis[x] = vis[y] = 0;
}
void dfs(int x) {
  vis[x] = 1;
  for (int i = 0, y; i < v[x].size(); ++i) {
    y = v[x][i];
    if (!vis[y]) dfs(y);
  }
}
int main() {
  freopen("1_68.txt", "r", stdin);
  cin >> n, g.clear();
  for (int i = 1, x; i <= n; ++i)
    scanf("%d", &x), s ^= x, ++g[x], a[i].first = x;
  ++g[s];
  for (int i = 1, x; i <= n; ++i) {
    scanf("%d", &x), a[i].second = x;
    if (g[x] == 0) return puts("-1"), 0;
    --g[x];
  }
  if (sam())
    return puts("0"), 0;
  else
    Discreting();
  memset(vis, 1, sizeof vis);
  for (int i = 1; i <= n; ++i)
    if (a[i].first != a[i].second) add(id[a[i].second], id[a[i].first]), ++ans;
  dfs(id[s]);
  for (int i = 1; i <= cnt; ++i)
    if (!vis[i]) dfs(i), ++ans;
  cout << ans << endl;
  return 0;
}
