#include <bits/stdc++.h>
class DSU {
  std::vector<int> dat;

 public:
  DSU(int n) : dat(n, -1) {}
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x != y) {
      if (dat[x] > dat[y]) {
        std::swap(x, y);
      }
      dat[x] += dat[y];
      dat[y] = x;
    }
  }
  int find(int x) {
    if (dat[x] < 0) {
      return x;
    }
    return dat[x] = find(dat[x]);
  }
};
int main() {
  int n;
  std::cin >> n;
  std::vector<int> a(n);
  std::vector<int> b(n);
  std::map<int, int> ma;
  std::map<int, int> mb;
  std::set<int> st;
  int x = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    ma[a[i]]++;
    x ^= a[i];
    st.insert(a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    mb[b[i]]++;
    st.insert(b[i]);
  }
  if (a == b) {
    puts("0");
    return 0;
  }
  int less = -1;
  int over = -1;
  for (int e : st) {
    int c = ma[e] - mb[e];
    if (std::abs(c) >= 3) {
      puts("-1");
      return 0;
    }
    if (c == 2) {
      if (over != -1 || less != -1) {
        puts("-1");
        return 0;
      }
      over = e;
      less = e;
    } else if (c == -2) {
      if (over != -1 || less != -1) {
        puts("-1");
        return 0;
      }
      less = e;
      over = e;
    } else if (c == 1) {
      if (over != -1) {
        puts("-1");
        return 0;
      }
      over = e;
    } else if (c == -1) {
      if (less != -1) {
        puts("-1");
        return 0;
      }
      less = e;
    }
  }
  if ((less != -1) ^ (over != -1)) {
    puts("-1");
    return 0;
  }
  if (less != -1 && less != x) {
    puts("-1");
    return 0;
  }
  int ans = less != -1;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      ans++;
    }
  }
  int tmp = -1;
  if (less != -1) {
    for (int i = 0; i < n; i++) {
      if (b[i] == less) {
        tmp = a[i];
        a[i] = x;
        ans--;
        break;
      }
    }
    for (int i = 0; i < n; i++) {
      if (a[i] == over) {
        a[i] = tmp;
        break;
      }
    }
  }
  std::map<int, std::vector<int>> un;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      un[b[i]].push_back(i);
    }
  }
  DSU dsu(n);
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      int k = un[a[i]].back();
      un[a[i]].pop_back();
      dsu.unite(i, k);
    }
  }
  std::map<int, bool> roots;
  for (int i = 0; i < n; i++) {
    if (a[i] != b[i]) {
      roots[dsu.find(i)] |= b[i] == x;
    }
  }
  for (auto kv : roots) {
    if (!kv.second) {
      ans++;
    }
  }
  std::cout << ans << std::endl;
}
