#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
map<int, int> Map;
map<int, vector<int> > pos;
int n, m, x, y, A[N], B[N], C[N];
int fa[N];
int getf(int x) { return (fa[x] != x) ? fa[x] = getf(fa[x]) : x; }
int main() {
  scanf("%d", &n);
  int cur = 0;
  for (int i = 1; i <= n; ++i) scanf("%d", &A[i]), ++Map[A[i]], cur ^= A[i];
  ++Map[cur];
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &B[i]);
    --Map[B[i]];
    if (Map[B[i]] < 0) {
      exit(0 * puts("-1"));
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (A[i] != B[i]) fa[i] = i, pos[A[i]].push_back(i), pos[B[i]].push_back(i);
  }
  for (map<int, vector<int> >::iterator it = pos.begin(); it != pos.end();
       ++it) {
    vector<int> cur = it->second;
    for (int j = 0; j < (int)cur.size() - 1; ++j) {
      fa[getf(cur[j + 1])] = getf(cur[j]);
    }
  }
  int tot = n - 1;
  for (int i = 1; i <= n; ++i) {
    if (A[i] == B[i])
      --tot;
    else if (getf(i) == i)
      ++tot;
  }
  cout << tot << endl;
}
