#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define low(x) ((x)&-(x))
#define ll long long
//#define file
using namespace std;

struct type{
	int s,x;
} A[200001],B[200001];
int n,i,j,k,l;
int a[200001],b[200001];
int tr[200001];
int num[200001];
ll ans;

bool cmp(type a,type b) {return a.s<b.s || a.s==b.s && a.x<b.x;}
void change(int t)
{
	while (t<=n)
	++tr[t],t+=low(t);
}
int find(int t)
{
	int ans=0;
	while (t)
	ans+=tr[t],t-=low(t);
	return ans;
}

int main()
{
	#ifdef file
	freopen("c.in","r",stdin);
	#endif
	
	scanf("%d",&n);
	fo(i,1,n) scanf("%d",&a[i]),a[i]+=i,A[i]=type{a[i],i};
	fo(i,1,n) scanf("%d",&b[i]),b[i]+=i,B[i]=type{b[i],i};
	sort(A+1,A+n+1,cmp);
	sort(B+1,B+n+1,cmp);
	
	fo(i,1,n)
	{
		if (A[i].s!=B[i].s) {printf("-1\n");return 0;}
		a[A[i].x]=b[B[i].x]=i;
	}
	
	l=0;
	fo(i,1,n) num[a[i]]=++l;
	fo(i,1,n) a[i]=num[a[i]],b[i]=num[b[i]];
	fo(i,1,n)
	{
		ans+=i-1-find(b[i]);
		change(b[i]);
	}
	printf("%lld\n",ans);
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}
#include <bits/stdc++.h>   
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long 
#define pll pair<ll,ll>
#define pb push_back
#define M 1000000007
#define lc '\n'
using namespace std;
#define N 2000010
ll lpf[N], mobius[N];

void least_prime_factor()
{
    for (ll i = 2; i < N; i++)
 
        // If it is a prime number
        if (!lpf[i])
 
            for (ll j = i; j < N; j += i)
 
                // For all multiples which are not
                // visited yet.
                if (!lpf[j])
                    lpf[j] = i;
}
void Mobius()
{
    for (ll i = 1; i < N; i++) {
 
        // If number is one
        if (i == 1)
            mobius[i] = 1;
        else {
 
            // If number has a squared prime factor
            if (lpf[i / lpf[i]] == lpf[i])
                mobius[i] = 0;
 
            // Multiply -1 with the previous number
            else
                mobius[i] = -1 * mobius[i / lpf[i]];
        }
    }
}
ll gcd_pairs(ll a[],ll n)
{
    // To store maximum number
    ll maxi = 0;
 
    // To store frequency of each number
    ll fre[N] = { 0 };
 
    // Find frequency and maximum number
    for (ll i = 0; i < n; i++) {
        fre[a[i]]++;
        maxi = max(a[i], maxi);
    }
 
    least_prime_factor();
    Mobius();
 
    // To store number of pairs with gcd equals to 1
    ll ans = 0;
 
    // Traverse through the all possible elements
    for (ll i = 1; i <= maxi; i++) {
        if (!mobius[i])
            continue;
 
        ll temp = 0;
        for (ll j = i; j <= maxi; j += i)
            temp += fre[j];
 
        ans += temp * (temp - 1) / 2 * mobius[i];
    }
 
    // Return the number of pairs
    return ans;
}
int main()
{
    IO
    ll l,r;
    cin>>l>>r;
    ll n=r-l+1;
    ll final=(n*(n-1))/2;
    ll a[n];
    for(ll i=0;i<n;i++)
    {
    	a[i]=l+i;
    }
    for(ll i=l;i<=r;i++)
    {
    	final-=((r/i)-1);
    }
    if(l==1)
    {
        final+=n-1;
    }
    final-=gcd_pairs(a,n);
    cout<<final*2<<lc;
    return 0;
}
/*
	Well, why don't you just concentrate  
	on being the best you, you can be.
*/

