#include <bits/stdc++.h>
using namespace std;
int a[100005], b[100005], ans, tota, n;
map<int, int> tot, tot2, vis;
map<int, vector<int> > ed;
void dfs(int node) {
  if (vis[node]) return;
  vis[node] = 1;
  tota += ((int)ed[node].size());
  for (int i : ed[node]) {
    dfs(i);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    tot[a[i]]++;
    a[n + 1] ^= a[i];
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
    tot2[b[i]]++;
    b[n + 1] ^= b[i];
  }
  n++;
  tot[a[n]]++;
  tot2[b[n]]++;
  for (auto val : tot) {
    if (tot2[val.first] != val.second) {
      printf("-1");
      return 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == b[i]) {
      tot[a[i]]--;
      tot2[b[i]]--;
      continue;
    }
    ed[a[i]].push_back(b[i]);
    ed[b[i]].push_back(a[i]);
  }
  for (auto val : tot) {
    if (val.second && !vis[val.first]) {
      ans++;
      tota = 0;
      dfs(val.first);
      ans += tota / 2;
    }
  }
  ans--;
  printf("%d", ans);
}
