#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, sum, sum2, tot, ans, a[N], b[N];
map<int, int> mp, vis;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    vis[a[i]]++;
    sum ^= a[i];
  }
  vis[sum]++;
  for (int i = 1; i <= n; i++) {
    scanf("%d", b + i);
    if (!vis.count(b[i]) || !vis[b[i]]) {
      puts("-1");
      return 0;
    }
    vis[b[i]]--;
    if (a[i] != b[i]) mp[b[i]] = a[i];
    sum2 ^= b[i];
  }
  if (!vis.count(sum2) || !vis[sum2]) {
    puts("-1");
    return 0;
  }
  while (mp.count(sum)) {
    int tmp = mp[sum];
    mp.erase(sum);
    sum = tmp;
    ans++;
  }
  printf("%d", ans + 3 * max((int)mp.size() - 1, 0));
  return 0;
}
