#include <bits/stdc++.h>
using namespace std;
const int N = 500000;
const int M = 70;
const int MOD = 1e9 + 7;
int n, a[N], b[N];
pair<int, int> p[N];
map<int, vector<int> > q;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      q[b[i]].push_back(i);
    }
  int x = 0;
  for (int i = 1; i <= n; i++) x ^= a[i];
  int need = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      need++;
    }
  int ans = 0;
  int p = 0;
  int it = 3 * n;
  while (it-- && need) {
    if (q.find(x) != q.end()) {
      ans++;
      int j = q[x].back();
      q[x].pop_back();
      if (q[x].empty()) q.erase(x);
      x = a[j];
      a[j] = b[j];
      need--;
    } else {
      ans++;
      if (q.empty()) break;
      int s = (*q.begin()).first;
      int j = q[s].back();
      int tmp = a[j];
      a[j] = x;
      x = tmp;
    }
  }
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) ans = -1;
  cout << ans;
  return 0;
}
