#include <bits/stdc++.h>
using namespace std;
namespace Tarjan {
const int MAXN = 200020;
stack<int> S;
vector<int> E[MAXN];
int disc[MAXN];
int comp_id[MAXN];
bool stk[MAXN];
int tick;
int dfs(int x) {
  int lowlink = disc[x] = tick++;
  stk[x] = true;
  S.push(x);
  for (int y : E[x])
    if (!disc[y])
      lowlink = min(lowlink, dfs(y));
    else if (stk[y])
      lowlink = min(lowlink, disc[y]);
  if (lowlink == disc[x]) {
    while (stk[x]) {
      comp_id[S.top()] = x;
      stk[S.top()] = false;
      S.pop();
    }
  }
  return lowlink;
}
void run(int n) {
  tick = 1;
  for (int i = (0); i < (n); ++i) disc[i] = 0;
  for (int i = (0); i < (n); ++i)
    if (!disc[i]) dfs(i);
}
};  // namespace Tarjan
map<int, vector<int>> in, out;
int deg(int x) { return (int)in[x].size() - (int)out[x].size(); }
void done(int x) {
  printf("%d\n", x);
  exit(0);
}
int main(void) {
  int n;
  scanf("%d", &n);
  vector<int> a(n), b(n);
  for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
  for (int i = (0); i < (n); ++i) scanf("%d", &b[i]);
  int x = 0;
  for (int i = (0); i < (n); ++i) x ^= a[i];
  for (int i = (0); i < (n); ++i) {
    if (a[i] != b[i]) {
      out[b[i]].push_back(a[i]);
      in[a[i]].push_back(b[i]);
    }
  }
  vector<int> all;
  for (int i = (0); i < (n); ++i)
    for (int x : {a[i], b[i]}) {
      if (in[x].size() || out[x].size()) {
        all.push_back(x);
      }
    }
  int ret = 0;
  if (deg(x) == -1) {
    int y = -1;
    for (int i : all) {
      if (deg(i) == +1) {
        y = i;
        break;
      }
    }
    if (y == -1) done(-1);
    for (int i = (0); i < (n); ++i) {
      if (x != b[i] && y != b[i] && deg(b[i]) != 0) done(-1);
    }
    out[y].push_back(x);
    ret -= 2;
  } else if (out[x].size()) {
    ret--;
  }
  sort(all.begin(), all.end());
  all.erase(unique(all.begin(), all.end()), all.end());
  map<int, int> ind;
  int N = all.size();
  for (int i = (0); i < (N); ++i) ind[all[i]] = i;
  for (int i = (0); i < (N); ++i) {
    int x = all[i];
    for (int y : out[x]) {
      Tarjan::E[i].push_back(ind[y]);
    }
  }
  Tarjan::run(N);
  set<int> comp_ids;
  for (int i = (0); i < (N); ++i) {
    comp_ids.insert(Tarjan::comp_id[i]);
  }
  for (int i = (0); i < (N); ++i) {
    int x = all[i];
    for (int y : out[x]) {
      if (Tarjan::comp_id[i] != Tarjan::comp_id[ind[y]]) {
        done(-1);
      }
    }
  }
  ret += comp_ids.size();
  for (int i = (0); i < (N); ++i) ret += out[all[i]].size();
  done(ret);
  return 0;
}
