#include <bits/stdc++.h>
using namespace std;
void File() {
  freopen("duliu.in", "r", stdin);
  freopen("duliu.out", "w", stdout);
}
const int maxn = 2e5 + 10;
int n, a[maxn], b[maxn], ans;
int val[maxn << 1], tot, cnt[maxn];
int fa[maxn], sz[maxn];
vector<int> pos[maxn];
vector<int>::iterator it;
int cc, ss;
bool vis[maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  sz[x] += sz[y];
  fa[y] = x;
}
int main() {
  bool flag = false;
  scanf("%d", &n);
  for (int i = 1, i_end_ = n; i <= i_end_; ++i)
    scanf("%d", &a[i]), val[++tot] = a[i];
  for (int i = 1, i_end_ = n; i <= i_end_; ++i)
    scanf("%d", &b[i]), val[++tot] = b[i];
  sort(val + 1, val + tot + 1);
  tot = unique(val + 1, val + tot + 1) - val - 1;
  for (int i = 1, i_end_ = n; i <= i_end_; ++i) {
    a[i] = lower_bound(val + 1, val + tot + 1, a[i]) - val;
    b[i] = lower_bound(val + 1, val + tot + 1, b[i]) - val;
    ++cnt[a[i]];
    --cnt[b[i]];
  }
  int s = 0, v1 = 0, v2 = 0, sum = 0;
  for (int i = 1, i_end_ = tot; i <= i_end_; ++i) {
    s += abs(cnt[i]);
    if (cnt[i] == -1) v1 = i;
    if (cnt[i] == 1) v2 = i;
  }
  if (s > 2) {
    puts("-1");
    return 0;
  }
  for (int i = 1, i_end_ = n; i <= i_end_; ++i) sum ^= val[a[i]];
  if (s == 2 && sum != val[v1]) {
    puts("-1");
    return 0;
  }
  if (s == 2)
    for (int i = 1, i_end_ = n; i <= i_end_; ++i)
      if (b[i] == v1) {
        if (a[i] == v2) flag = true;
        b[i] = v2;
        break;
      }
  for (int i = 1, i_end_ = n; i <= i_end_; ++i) {
    if (a[i] != b[i]) pos[b[i]].push_back(i);
    fa[i] = i;
    sz[i] = 1;
  }
  for (int i = 1, i_end_ = n; i <= i_end_; ++i) {
    if (a[i] == b[i]) continue;
    it = pos[a[i]].end() - 1;
    merge(i, *it);
    pos[a[i]].pop_back();
  }
  for (int i = 1, i_end_ = tot; i <= i_end_; ++i)
    for (int j = 1, j_end_ = pos[i].size() - 1; j <= j_end_; ++j)
      merge(pos[i][0], pos[i][j]);
  for (int i = 1, i_end_ = n; i <= i_end_; ++i) {
    if (vis[find(i)]) continue;
    if (sz[find(i)] == 1) continue;
    vis[find(i)] = 1;
    ++cc;
    ss += sz[find(i)];
  }
  if (s == 0)
    printf("%d\n", cc + ss);
  else
    printf("%d\n", cc + ss - 1 + flag * 2);
  return 0;
}
