#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
const int maxn = 100005;
int head[maxn], cnt[maxn], vis[maxn], node = 1, sum = 0;
struct Node {
  int data, id;
} a[maxn], b[maxn];
struct Edge {
  int v, next;
} e[maxn];
int read() {
  char c = getchar();
  int x = 0;
  while (c < '0' || '9' < c) c = getchar();
  while ('0' <= c && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
  return x;
}
void add(int u, int v) {
  e[++node] = (Edge){v, head[u]};
  head[u] = node;
}
bool cmp1(Node x, Node y) { return x.data < y.data; }
bool cmp2(Node x, Node y) { return x.id < y.id; }
void dfs(int x) {
  int i;
  for (i = head[x]; i; i = e[i].next) {
    if (vis[e[i].v] == cnt[e[i].v]) continue;
    vis[e[i].v]++;
    sum++;
    dfs(e[i].v);
  }
}
int main() {
  int n, i, j, Max = 0;
  n = read();
  for (i = 1; i <= n; i++) a[i].data = read(), a[i].id = i;
  for (i = 1; i <= n; i++) b[i].data = read(), b[i].id = i;
  a[n + 1] = (Node){a[1].data, n + 1};
  b[n + 1] = (Node){b[1].data, n + 1};
  for (i = 2; i <= n; i++)
    a[n + 1].data ^= a[i].data, b[n + 1].data ^= b[i].data;
  sort(a + 1, a + n + 2, cmp1);
  sort(b + 1, b + n + 2, cmp1);
  int ans = -1, co = 0;
  for (i = 1; i <= n + 1; i++) {
    if (a[i].data != b[i].data) {
      printf("-1\n");
      return 0;
    }
    if (a[i].data != ans) co++;
    ans = a[i].data;
    a[i].data = co;
    b[i].data = co;
    cnt[co]++;
  }
  sort(a + 1, a + n + 2, cmp2);
  sort(b + 1, b + n + 2, cmp2);
  for (i = 1; i <= n; i++) {
    if (a[i].data != b[i].data)
      add(b[i].data, a[i].data);
    else
      cnt[a[i].data]--;
  }
  vis[a[n + 1].data]++;
  dfs(a[n + 1].data);
  for (i = 1; i <= n; i++) {
    if (vis[i] < cnt[i]) {
      sum += 2;
      vis[i]++;
      dfs(i);
    }
  }
  printf("%d\n", sum);
  return 0;
}
