#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 2000000000;
const long long BIG = 1446803456761533460LL;
const int maxn = 100010;
int n, A[maxn], B[maxn], in[maxn], out[maxn];
map<int, vector<int> > g;
set<int> vis;
void dfs(int x) {
  vis.insert(x);
  for (auto y : g[x])
    if (vis.count(y) == 0) dfs(y);
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> A[i];
  for (int i = 0; i < n; ++i) cin >> B[i];
  int brk = 0, xr = 0;
  for (int i = 0; i < n; ++i) {
    xr ^= A[i];
    if (A[i] != B[i]) {
      ++brk;
      g[A[i]].emplace_back(B[i]);
      g[B[i]].emplace_back(A[i]);
      ++out[A[i]];
      ++in[B[i]];
    }
  }
  if (brk == 0) return cout << "0\n", 0;
  int comp = 0, st = -1;
  for (auto x : g) {
    if (out[x.first] < in[x.first]) {
      if (in[x.first] - out[x.first] == 1 && st == -1 && x.first == xr)
        st = x.first;
      else
        return cout << "-1\n", 0;
    }
    if (vis.count(x.first) == 0) ++comp, dfs(x.first);
  }
  if (vis.count(xr) == 0) ++comp;
  cout << brk + comp - 1 << '\n';
}
