#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, tot, sum, flag, t1 = -1, t2 = -1;
int a[N], b[N], arr[N], num[N], vis[N], now[N];
map<int, int> Map;
vector<int> vec[N];
void check() {
  for (int i = 1; i <= n; ++i) Map[a[i]]++;
  for (int i = 1; i <= n; ++i) Map[b[i]]--;
  map<int, int>::iterator it;
  for (it = Map.begin(); it != Map.end(); ++it) {
    if (it->second > 0) {
      if (it->second > 1) {
        puts("-1"), exit(0);
      }
      if (t1 == -1)
        t1 = it->first;
      else
        puts("-1"), exit(0);
    }
    if (it->second < 0) {
      if (it->second < -1) {
        puts("-1"), exit(0);
      }
      if (sum != it->first) puts("-1"), exit(0);
      if (t2 == -1)
        t2 = it->first;
      else
        puts("-1"), exit(0);
    }
  }
  if (t1 != -1) {
    flag = 1;
    for (int i = 1; i <= n; ++i)
      if (b[i] == sum) {
        b[i] = t1;
        break;
      }
  }
}
void Solve() {
  for (int i = 1; i <= n; ++i) num[i] = a[i];
  for (int i = 1; i <= n; ++i) num[i + n] = b[i];
  sort(num + 1, num + n + n + 1);
  tot = unique(num + 1, num + n + n + 1) - num - 1;
  for (int i = 1; i <= n; ++i)
    a[i] = lower_bound(num + 1, num + tot + 1, a[i]) - num;
  for (int i = 1; i <= n; ++i)
    b[i] = lower_bound(num + 1, num + tot + 1, b[i]) - num;
  for (int i = 1; i <= n; ++i) {
    vec[b[i]].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    arr[i] = vec[a[i]][now[a[i]]++];
  }
}
int dfs(int x) {
  vis[x] = 1;
  if (vis[arr[x]])
    return 1;
  else
    return 1 + dfs(arr[x]);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), sum ^= a[i];
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
  int m = 0;
  for (int i = 1; i <= n; ++i) {
    if (b[i] == sum) flag = 1;
    if (a[i] == b[i]) continue;
    ++m;
    a[m] = a[i];
    b[m] = b[i];
  }
  n = m;
  check();
  Solve();
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) ans += dfs(i) + 1;
  }
  cout << ans - flag << endl;
  return 0;
}
