#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n, a[MAXN], b[MAXN];
void input() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", a + i);
  for (int i = 0; i < n; ++i) scanf("%d", b + i);
}
namespace Check {
map<int, int> mp;
bool check() {
  int x = 0;
  for (int i = 0; i < n; ++i) {
    ++mp[a[i]];
    x ^= a[i];
  }
  ++mp[x];
  for (int i = 0; i < n; ++i) {
    if (mp[b[i]] == 0) return false;
    --mp[b[i]];
  }
  return true;
}
}  // namespace Check
using Check::check;
namespace Solve {
set<int> s;
map<int, vector<int> > mp;
void prelude() {
  for (int i = 0; i < n; ++i)
    if (a[i] != b[i]) {
      s.insert(i);
      mp[b[i]].push_back(i);
    }
}
void solve() {
  prelude();
  int x = 0;
  for (int i = 0; i < n; ++i) x ^= a[i];
  int ans = 0;
  while (!s.empty()) {
    if (mp[x].empty()) {
      int pos = *s.begin();
      swap(a[pos], x);
    } else {
      int pos = mp[x].back();
      mp[x].pop_back();
      swap(a[pos], x);
      s.erase(pos);
    }
    ++ans;
  }
  printf("%d\n", ans);
}
}  // namespace Solve
using Solve::solve;
int main() {
  input();
  if (!check()) {
    puts("-1");
    return 0;
  }
  solve();
  return 0;
}
