#include <bits/stdc++.h>
using namespace std;
int n, i;
int a[100011], b[100011], xoro, dif;
map<int, int> M, M_a, M_b;
vector<int> vals;
vector<int> neg, poz;
void no_sol() {
  printf("-1");
  exit(0);
}
void give_sol(int x) {
  printf("%d", x);
  exit(0);
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]), xoro ^= a[i], vals.push_back(a[i]), M[a[i]]++;
  for (i = 1; i <= n; i++)
    scanf("%d", &b[i]), vals.push_back(b[i]), M[b[i]]--, dif += (a[i] != b[i]);
  for (i = 1; i <= n; i++) {
    if (a[i] == b[i]) continue;
    M_a[a[i]]++;
    M_b[b[i]]++;
  }
  sort(vals.begin(), vals.end());
  vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
  for (auto e : vals) {
    if (M[e] < -1 || M[e] > 1) no_sol();
    if (M[e] == -1) neg.push_back(e);
    if (M[e] == 1) poz.push_back(e);
  }
  if (neg.size() > 1) no_sol();
  if (poz.size() > 1) no_sol();
  if (poz.empty()) {
    if (dif == 0) give_sol(0);
    if (dif == 2) give_sol(2);
    give_sol(dif + 1);
    if (M_a[xoro] > 0)
      give_sol(dif);
    else
      give_sol(dif + 1);
  } else {
    if (dif == 1) give_sol(1);
    if (dif == 2) give_sol(2);
    give_sol(dif + 1);
    int from = poz[0];
    int to = neg[0];
    int c1, c2;
    c1 = c2 = 0;
    for (i = 1; i <= n; i++) {
      if (a[i] == b[i]) continue;
      if (a[i] != from) continue;
      if (b[i] == to)
        c1++;
      else
        c2++;
    }
    if (c2 == 0)
      give_sol(dif + 1);
    else
      give_sol(dif);
  }
  return 0;
}
