#include <bits/stdc++.h>
using namespace std;
int a[200000], b[200000];
int main() {
  int n;
  cin >> n;
  map<int, int> mp;
  int x = 0;
  for (int i = 0; i < (n); i++) {
    scanf("%d", &a[i]);
    mp[a[i]]++;
    x ^= a[i];
  }
  a[n] = x;
  mp[x]++;
  x = 0;
  for (int i = 0; i < (n); i++) {
    scanf("%d", &b[i]);
    mp[b[i]]--;
    x ^= b[i];
  }
  b[n] = x;
  mp[x]--;
  for (auto p : mp) {
    if (p.second) {
      puts("-1");
      return 0;
    }
  }
  map<int, vector<int>> want;
  for (int i = 0; i < (n); i++) {
    if (a[i] != b[i]) {
      want[b[i]].push_back(i);
    }
  }
  if (want.empty()) {
    puts("0");
    return 0;
  }
  int cnt = 0;
  int b = 0;
  while (!want.empty()) {
    int d = a[n];
    if (want.count(d)) {
      cnt++;
      int x = want[d].back();
      swap(a[x], a[n]);
      want[d].pop_back();
      if (want[d].empty()) want.erase(d);
    } else {
      b = 1;
      int x = want.begin()->second.back();
      swap(a[x], a[n]);
    }
  }
  cout << cnt + b << endl;
}
