#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, a[N], b[N];
bool vis[N];
int ans;
set<pair<int, int> > s;
int getint(int v) {
  set<pair<int, int> >::iterator x = s.lower_bound(make_pair(v, 0));
  if (x == s.end()) {
    puts("-1");
    exit(0);
  }
  s.erase(x);
  return x->second;
}
int getxor(int *a, int *b) {
  int ret = 0;
  for (int *i = a; i != b; ++i) ret ^= (*i);
  return ret;
}
bool same(int *a, int *b, int *c, int *d) {
  set<int> s1, s2;
  for (int *i = a; i != b; ++i) s1.insert(*i);
  for (int *i = c; i != d; ++i) s2.insert(*i);
  return s1 == s2;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
  int dcmp = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i] == b[i]) {
      vis[i] = 1;
      ++dcmp;
    }
  if (dcmp == n) return puts("0"), 0;
  int t = getxor(a + 1, a + n + 1);
  int ans = 0;
  bool p = 0;
  for (int i = 1; i <= n; ++i)
    if (!vis[i] && b[i] == t) ans = -1, p = 1;
  if (ans <= 0) {
    for (int i = 1; i <= n; ++i)
      if (!vis[i]) s.insert(make_pair(b[i], i));
    ans = -2;
    a[n + 1] = t;
    t = getxor(b + 1, b + n + 1);
    s.insert(make_pair(t, n + 1));
    for (int i = 1; i <= n + 1; ++i)
      if (!vis[i]) {
        int p = i, cnt = 0;
        while (!vis[p]) {
          vis[p] = 1;
          ++cnt;
          p = getint(a[p]);
        }
        ans += cnt + 1;
      }
  }
  s.clear();
  if (same(a + 1, a + n + 1, b + 1, b + n + 1)) {
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; ++i) vis[i] = (a[i] == b[i]);
    int ret = 0 - p;
    for (int i = 1; i <= n; ++i)
      if (!vis[i]) s.insert(make_pair(b[i], i));
    for (int i = 1; i <= n; ++i)
      if (!vis[i]) {
        int p = i, cnt = 0;
        while (!vis[p]) {
          vis[p] = 1;
          ++cnt;
          p = getint(a[p]);
        }
        ret += cnt + 1;
      }
    ans = min(ans, ret);
  }
  cout << ans;
}
