#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

//0-indexed
class BIT{
public:
  int N;
  vector<ll> data;
  BIT(){}
  BIT(int n):N(n),data(n){}

  ll sum(int l,int r){//[l,r)
    return sum(r)-sum(l);
  }

  void add(int p,ll x){
    p++;
    while(p<=N){
      data[p-1]+=x;
      p+=p&(-p);
    }
  }

private:
  ll sum(int r){
      ll s=0;
      while(r>0){
          s+=data[r-1];
          r-=r&(-r);
      }
      return s;
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin>>N;
  vector<ll> A(N),B(N);
  rep(i,N) cin>>A[i];
  rep(i,N) cin>>B[i];

  {
    ll asum=0,bsum=0;
    rep(i,N) asum+=A[i];
    rep(i,N) bsum+=B[i];
    if(asum!=bsum){
      cout<<-1<<endl;
      return 0;
    }
  }

  map<ll,vector<int>> vmp;
  for(int i=N-1;i>=0;i--) vmp[A[i]+i].push_back(i);

  vector<int> P(N,0);
  rep(i,N){
    ll tarv=B[i]+i;
    if(vmp.count(tarv)==false||vmp[tarv].empty()){
      cout<<-1<<endl;
      return 0;
    }
    int t=vmp[tarv].back();
    vmp[tarv].pop_back();
    P[t]=i;
  }
  
  BIT bit(N);
  ll ans=0;
  rep(i,N){
    ll res=bit.sum(P[i],N);
    ans+=res;
    bit.add(P[i],1);
  }
  cout<<ans<<endl;



  return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> P;
ll gcd(ll a, ll b){return b? gcd(b, a % b): a;}
ll quickpow(ll a, ll b){ll res = 1; while(b){if (b & 1) res = res * a; a = a * a; b >>= 1;} return res;}
// head

const int N = 2e5 + 5;
int a[N], b[N], n, num, ans, tmp[N];

map<pair<int, int>, int> mp;
map<int, int> cnt;

void merge(int l, int r) {
	int mid = (l + r) >> 1, i = l, j = mid + 1, now = l;
	while (i <= mid && j <= r) {
		if (a[i] < a[j]) tmp[now++] = a[i++];
		else {
			ans += mid - i + 1;
			tmp[now++] = a[j++];
		}
	}
	while (i <= mid) tmp[now++] = a[i++];
	while (j <= r) tmp[now++] = a[j++];
	for (int i = l; i <= r; i++) a[i] = tmp[i];
} 

void merge_sort(int l, int r) {
	if (l == r) return;
	int mid = (l + r) >> 1;
	merge_sort(l, mid);
	merge_sort(mid + 1, r);
	merge(l, r);
}

signed main(void){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		a[i] += i;
	}
	for (int i = 1; i <= n; i++) {
		cin >> b[i];
		b[i] += i;
	}
	for (int i = 1; i <= n; i++) {
		mp[make_pair(++cnt[b[i]], b[i])] = ++num;
	}
	cnt.clear();
	for (int i = 1; i <= n; i++) {
		pair<int, int> now = make_pair(++cnt[a[i]], a[i]);
		if (!mp.count(now)) {
			cout << -1 << endl;
			return 0;
		}
		a[i] = mp[now];
		//cout<<a[i]<<" ";
	}//cout<<endl;
	merge_sort(1, n);
	cout << ans << endl;
	return 0;
}
