#include <bits/stdc++.h>
using namespace std;
pair<int, int> su[100005];
map<int, int> ch;
queue<int> q;
set<pair<int, int> > fnd;
int chk[100005];
int main() {
  int n;
  int xr = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &(su[i].first));
    xr = xr ^ su[i].first;
    ch[su[i].first]++;
  }
  ch[xr]++;
  int no = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &(su[i].second));
    if (ch[su[i].second] == 0) {
      no++;
    } else {
      ch[su[i].second]--;
    }
  }
  if (no > 0) {
    printf("-1\n");
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (su[i].first == su[i].second) {
      chk[i] = 1;
    } else if (su[i].second == xr) {
      q.push(i);
    }
  }
  for (int i = 0; i < n; i++) {
    if (chk[i] == 0) {
      fnd.insert({su[i].second, i});
    }
  }
  int ans = 0;
  ch.clear();
  while (!q.empty()) {
    int target = q.front();
    q.pop();
    fnd.erase({su[target].second, target});
    while (1) {
      ans++;
      chk[target] = 1;
      if (su[target].first == xr) {
        break;
      }
      auto it = fnd.lower_bound({su[target].first, -1});
      if (it == fnd.end() || (*it).first != su[target].first) break;
      target = (*it).second;
      fnd.erase(it);
    }
  }
  for (int i = 0; i < n; i++) {
    if (!chk[i]) {
      ans++;
      int target = i;
      int until = su[i].second;
      fnd.erase({su[i].second, i});
      while (1) {
        ans++;
        chk[target] = 1;
        auto it = fnd.lower_bound({su[target].first, -1});
        if (it == fnd.end() || (*it).first != su[target].first) break;
        target = (*it).second;
        fnd.erase(it);
      }
    }
  }
  printf("%d\n", ans);
}
