#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int n, a[N], b[N], p1[N], p2[N];
map<int, queue<int> > mp;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), a[n + 1] ^= a[i], p1[i] = a[i];
  p1[n + 1] = a[n + 1];
  for (int i = 1; i <= n; i++)
    scanf("%d", &b[i]), b[n + 1] ^= b[i], p2[i] = b[i];
  p2[n + 1] = b[n + 1];
  sort(p1 + 1, p1 + n + 2);
  sort(p2 + 1, p2 + n + 2);
  for (int i = 1; i <= n + 1; i++)
    if (p1[i] != p2[i]) {
      puts("-1");
      return 0;
    }
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) mp[a[i]].push(i);
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      if (a[n + 1] == b[i]) {
        cnt++;
        swap(a[i], a[n + 1]);
      } else {
        while (!mp[b[i]].empty()) {
          int u = mp[b[i]].front();
          mp[b[i]].pop();
          if (a[u] != b[u]) {
            cnt++;
            swap(a[u], a[n + 1]);
            if (a[n + 1] != b[u]) mp[a[n + 1]].push(u);
            break;
          }
        }
        cnt++;
        swap(a[i], a[n + 1]);
      }
    }
  printf("%d\n", cnt);
}
