#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, p[2 * N], vis[2 * N];
vector<int> a, b;
int find(int x) {
  if (p[x] == x) return x;
  return p[x] = find(p[x]);
}
bool join(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return 0;
  p[x] = y;
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  for (int i = 0; i < 2 * N; i++) {
    p[i] = i;
  }
  cin >> n;
  a.resize(n);
  b.resize(n);
  for (auto &i : a) cin >> i;
  for (auto &i : b) cin >> i;
  int x = 0;
  for (auto &i : a) x ^= i;
  a.push_back(x);
  x = 0;
  for (auto &i : b) x ^= i;
  b.push_back(x);
  if (a == b) return cout << 0 << endl, 0;
  multiset<int> g(a.begin(), a.end());
  multiset<int> h(b.begin(), b.end());
  if (g != h) return cout << -1 << endl, 0;
  map<int, int> mp;
  for (auto &i : a) mp[i];
  for (auto &i : b) mp[i];
  x = 0;
  for (auto &i : mp) i.second = x++;
  for (auto &i : a) i = mp[i];
  for (auto &i : b) i = mp[i];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == b[i]) continue;
    ans++;
    if (vis[a[i]] == 0) ans++;
    if (vis[b[i]] == 0) ans++;
    vis[a[i]] = vis[b[i]] = 1;
    ans -= join(a[i], b[i]);
  }
  if (vis[a.back()] == 0) ans++;
  if (vis[b.back()] == 0) ans++;
  cout << ans - 1 << endl;
}
