#include <bits/stdc++.h>
using namespace std;
int a[100001];
int n, m;
int ans;
int co;
vector<int> v[100001];
int ch[100001];
void check(int x) {
  ch[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    if (ch[v[x][i]] == 0) {
      check(v[x][i]);
    }
  }
  if (x != a[n]) ans++;
  return;
}
int main(void) {
  int b[100001];
  vector<pair<int, int> > aa;
  vector<pair<int, int> > bb;
  int c;
  int k;
  int s, t;
  int x, y, z;
  int max;
  int min;
  scanf("%d", &n);
  c = 0;
  x = 0;
  y = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    x = x ^ a[i];
    aa.push_back(make_pair(a[i], i));
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    y = y ^ b[i];
    bb.push_back(make_pair(b[i], i));
  }
  a[n] = x;
  b[n] = y;
  aa.push_back(make_pair(x, n));
  bb.push_back(make_pair(y, n));
  sort(aa.begin(), aa.end());
  sort(bb.begin(), bb.end());
  c = 0;
  for (int i = 0; i < n + 1; i++) {
    if (aa[i].first != bb[i].first) {
      printf("-1\n");
      return 0;
    }
    if (i > 0) {
      if (aa[i].first > aa[i - 1].first) c++;
    }
    a[aa[i].second] = c;
    b[bb[i].second] = c;
  }
  c++;
  if (a[n] != b[n]) ans--;
  for (int i = 0; i < n + 1; i++) {
    if (a[i] != b[i]) v[a[i]].push_back(b[i]);
  }
  ans = 0;
  for (int i = 0; i < c; i++) ch[i] = 0;
  for (int i = 0; i < c; i++) {
    if (ch[i] == 0) {
      if (v[i].size() > 0) {
        ans++;
        check(i);
      } else
        ch[i] = 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
