#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], b[N], f[N];
map<int, int> mp;
int tot = 0;
bool bz[N];
int find(int x) { return !f[x] ? x : f[x] = find(f[x]); }
void mg(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy) f[fy] = fx;
}
int main() {
  int n, s = 0, ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), s ^= a[i];
  if (!mp[s]) mp[s] = ++tot;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &b[i]);
    if (a[i] != b[i] && !mp[a[i]]) mp[a[i]] = ++tot;
  }
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) mg(mp[a[i]], mp[b[i]]), ans++;
  for (int i = 1; i <= tot; ++i) bz[find(i)] = 1;
  for (int i = 1; i <= tot; ++i)
    if (bz[i]) ans++;
  sort(a + 1, a + n + 1), sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) {
      a[i] = s;
      break;
    }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) return printf("-1"), 0;
  printf("%d", ans - 1);
}
