#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<iostream>

#define fi first
#define se second
#define mk make_pair
#define pb push_back

#define ls (t<<1)
#define rs ((t<<1)|1)
#define mid ((l+r)>>1)

#define N 300005

using namespace std;

int A[N], B[N], C[N], n, tree[N];

multiset<int> st, num[N];

int lowbit(int x) { return x & -x; }

int ask(int x)
{
	int s = 0;
	for ( ; x; x -= lowbit(x)) s += tree[x];
	return s;
}

void insert(int x)
{
	for ( ; x <= n; x += lowbit(x)) tree[x] += 1; 
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) 
	{
		scanf("%d", &A[i]);
		A[i] += i;
		st.insert(A[i]);
		C[++C[0]] = A[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		scanf("%d", &B[i]); B[i] += i;
		if (st.find(B[i]) == st.end())
		{
			puts("-1");
			return 0;
		}
		st.erase(st.find(B[i]));
	}
	sort(C + 1, C + C[0] + 1);
	C[0] = unique(C + 1, C + C[0] + 1) - (C + 1);
	for (int i = 1; i <= n; ++i)
	{
		A[i] = lower_bound(C + 1, C + C[0] + 1, A[i]) - C;
		num[A[i]].insert(i);
		B[i] = lower_bound(C + 1, C + C[0] + 1, B[i]) - C;
	}
	long long ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		int pos = *num[B[i]].begin();
		num[B[i]].erase(pos);
		int realpos = pos + (i - 1) - ask(pos);
		insert(pos);
		ans += realpos - i;
	}
	printf("%lld\n", ans);
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount

const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

struct num {
  ll x, i;
  bool operator < (const num &n) const { return x<n.x||(x==n.x&&i<n.i); };
};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

struct SegTree {
  ll size;
  vector<ll> pos;
  SegTree(ll N) { size = 1; while(size<N) size<<=1; pos.resize(2*size,0); }
  ll operator[](const ll &x) const { return pos[x+size]; }

  void set(ll x, const ll v) { pos[x+size] = v; }
  void update(ll x, const ll v) { set(x,v); x+=size; while (x>>=1) operate(x); }
  ll query(ll a, ll b) {
    ll L = 0, R = 0;
    for (a+=size, b+=size; a<b; a>>=1, b>>=1) {
      if (a&1) { L = q(L,pos[a]); a++; }
      if (b&1) { b--; R = q(pos[b],R); }
    }
    return q(L,R);
  }
  void operate(ll i) { pos[i] = q(pos[i*2], pos[i*2+1]); }
  ll q(ll x, ll y) { return x + y; }
};

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------


int main() {
  ll N; cin >> N;
  num A[N]; rep(i,N) { ll x; cin >> x; A[i] = {x+i,i}; }
  num B[N]; rep(i,N) { ll x; cin >> x; B[i] = {x+i,i}; }
  sort(A,A+N); sort(B,B+N);
  rep(i,N) { 
    if (A[i].x!=B[i].x) {
      cout << -1 << endl;
      return 0;
    }
  }
  // rep(i,N) cout << A[i].i << " "; cout << endl;
  // rep(i,N) cout << B[i].i << " "; cout << endl;

  ll to[N] = {}; rep(i,N) to[A[i].i] = B[i].i;
  // debug(to,to+N);

  ll result = 0;
  SegTree st(N);
  rep(i,N) {
    result += st.query(to[i],N);
    st.update(to[i],1);
  }

  cout << result << endl;
  return 0;
}