#include<bits/stdc++.h>
#define X first
#define Y second
#define pb emplace_back
#define FOR(i,a,b) for(int (i)=(a);i<(b);++(i))
#define EFOR(i,a,b) for(int (i)=(a);i<=(b);++(i))
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)),(X).end())

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef ll LL;
typedef pii PII;
typedef pll PLL;
template<class T> using vv=vector<vector<T>>;
template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;}
const ll MOD=1e9+7;

int N;
int A[114514];
int B[114514];
map<int, multiset<int>> es;

int main() {
  scanf("%d", &N);

  int x = 0;
  rep(i, N) {
    scanf("%d", &A[i]);
    x ^= A[i];
  }

  rep(i, N) {
    scanf("%d", &B[i]);
    if (A[i] != B[i]) es[B[i]].insert(A[i]);
  }

  int defx = x;
  int ans = 0;
  while (!es.empty()) {
    if (!es.count(x)) {
      assert(!es.empty());
      ans++;
      int want = es.begin()->X;
      assert(!es[want].empty());
      int give = *es[want].begin();
      if (defx == give) given = *es[want].rbegin();
      es[want].erase(es[want].find(give));
      es[want].insert(x);
      x = give;
    }

    if (!es.count(x)) {
      puts("-1");
      return 0;
    }

    ans++;
    int give = *es[x].begin();
    if (defx == give) given = *es[x].rbegin();
    es[x].erase(es[x].find(give));
    if (es[x].empty()) es.erase(x);
    x = give;
  }
  printf("%d\n", ans);
}
