#include <bits/stdc++.h>
using namespace std;
vector<int> v0;
map<int, vector<int> > Ma, Mb;
int n, a[100050], b[100050], c[100050], d[100050], i, j, rs[100050], p[100050];
bool e[100050], vis[100050];
int main() {
  scanf("%d", &n);
  int wtf1 = 0, wtf2 = 0, ans = 0;
  c[0] = -1;
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), c[i] = a[i], wtf1 ^= a[i];
  for (i = 1; i <= n; i++) scanf("%d", &b[i]), d[i] = b[i], wtf2 ^= b[i];
  sort(c + 1, c + n + 1);
  sort(d + 1, d + n + 1);
  for (i = 1; i <= n; i++)
    if (c[i] != d[i]) {
      ans = -1;
      n++;
      a[n] = wtf1;
      b[n] = wtf2;
      c[n] = wtf1;
      d[n] = wtf2;
      break;
    }
  sort(c + 1, c + n + 1);
  sort(d + 1, d + n + 1);
  for (i = 1; i <= n; i++)
    if (c[i] != d[i]) {
      puts("-1");
      return 0;
    }
  for (i = 1; i <= n; i++)
    if (a[i] == wtf1 || a[i] == wtf2) e[i] = true;
  for (i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      ans++;
      if (!Ma.count(a[i])) Ma[a[i]] = v0;
      Ma[a[i]].push_back(i);
    }
  for (i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      if (!Mb.count(b[i])) Mb[b[i]] = v0;
      Mb[b[i]].push_back(i);
    }
  for (i = 1; i <= n; i++)
    if (c[i] != c[i - 1]) {
      int m = Ma[c[i]].size();
      for (j = 0; j < m; j++) rs[j] = j;
      for (j = 0; j < m; j++) swap(rs[j], rs[rand() % (j + 1)]);
      for (j = 0; j < m; j++) p[Ma[c[i]][j]] = Mb[c[i]][rs[j]];
    }
  for (i = 1; i <= n; i++)
    if (a[i] != b[i])
      vis[i] = false;
    else
      vis[i] = true;
  for (i = 1; i <= n; i++)
    if (!vis[i]) {
      int gh = 1, j = i;
      while (!vis[j]) {
        if (e[j]) gh = 0;
        vis[j] = true;
        j = p[j];
      }
      ans += gh;
    }
  printf("%d\n", ans);
  return 0;
}
