#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 6;
int a[MAX_N];
int b[MAX_N];
struct DisjointSet {
  int p[MAX_N];
  void init(int n) {
    for (int i = 0; n >= i; i++) {
      p[i] = i;
    }
  }
  int Find(int x) { return x == p[x] ? x : p[x] = Find(p[x]); }
  void Union(int x, int y) { p[Find(x)] = Find(y); }
} djs;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> v1, v2;
  int kirino = 0;
  for (int i = 1; n >= i; i++) {
    scanf("%d", &a[i]);
    v1.push_back(a[i]);
    kirino ^= a[i];
  }
  for (int i = 1; n >= i; i++) {
    scanf("%d", &b[i]);
    v2.push_back(b[i]);
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  int dif = 0;
  int haha = -1;
  int meruru = -1;
  for (int i = 0; n > i; i++) {
    dif += (v1[i] != v2[i]);
    if (v1[i] != v2[i]) {
      if (v2[i] != kirino)
        dif = 2;
      else {
        meruru = v1[i];
      }
    }
  }
  v1.resize(unique(v1.begin(), v1.end()) - v1.begin());
  v2.resize(unique(v2.begin(), v2.end()) - v2.begin());
  for (int i = 1; n >= i; i++) {
    int tmp = a[i];
    a[i] = lower_bound(v1.begin(), v1.end(), a[i]) - v1.begin() + 1;
    if (meruru == tmp) meruru = a[i];
  }
  int hhh = kirino;
  for (int i = 1; n >= i; i++) {
    int tmp = b[i];
    if (tmp == kirino) {
      b[i] = n + 1;
      kirino = -1;
    } else
      b[i] = lower_bound(v1.begin(), v1.end(), b[i]) - v1.begin() + 1;
  }
  kirino = n + 1;
  djs.init(n * 2);
  if (dif >= 2) {
    puts("-1");
    return 0;
  }
  if (dif == 1) {
    bool can = false;
    int ans = 0;
    for (int i = 1; n >= i; i++) {
      ans += (a[i] != b[i]);
      if (a[i] != meruru && b[i] == kirino) can = true;
      djs.Union(a[i], b[i]);
    }
    if (can) {
      int aa = 0;
      for (int i = 1; n + 1 >= i; i++) {
        aa += (djs.Find(i) == i);
      }
      printf("%d\n", ans + aa - 2);
    } else {
      if (ans == 1)
        puts("1");
      else
        printf("%d\n", ans + 1);
    }
  } else {
    int ans = 0;
    for (int i = 1; n >= i; i++) {
      ans += (a[i] != b[i]);
    }
    if (ans == 0)
      puts("0");
    else
      printf("%d\n", ans + 1);
  }
}
