// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int ll
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define ll long long
#define pi pair<int,int> 
#define pii pair<pi,int>
#define vi vector<int>
#define pb push_back 
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n){
    return fastexp(n,mod-2);
}
template <typename T>
void add(T &a, T b){
	a += b;
	if(a >= mod)a -= mod;
}
template <typename T>
void sub(T &a, T b){
	a -= b;
	if(a < 0)a += mod;
}
template <typename T>
void mul(T &a, T b){
	a *= b;
	if(a >= mod)a %= mod;
}
const int maxn = 1e6 + 10;
int mu[maxn],lpf[maxn];
void lpf1(){
	repa(i,2,maxn){
		if(!lpf[i]){
			for(int j = i;j < maxn;j+=i){
				if(!lpf[j])lpf[j] = i;
			}
		}
	}
}
void sieve(){
	lpf1();
	mu[1] = 1;
	repa(i,2,maxn){
		if(lpf[i / lpf[i]] == lpf[i]){
			mu[i] = 0;
		}
		else{
			mu[i] = -1*(mu[i/lpf[i]]);
		}
	}
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int l,r;
	cin>>l>>r;
	int ans = 0;
	for(int i = max(l,2ll);i <= r;i++){
		for(int j = i+i;j <= r;j += i){
			ans -= 2;
		}
	}
	sieve();
	repa(i,2,r+1){
		// int x1 = l/i;
		// if(l%i != 0)x1++;
		// int x2 = r/i;
		int d = (r/i)-((l-1)/i);
		// cout<<i<<d<<endl;
		// if(d < 2)break;
		ans -= mu[i]*(d*(d-1));
	}
	cout<<ans;
	return 0;
}	
#include <bits/stdc++.h>
using namespace std;

//左に行きたい各それについて、自分の左でa[i]-iが一致しているやつを見つけて飛び込む
//a[i]-iが一致しているやつの中で最大のものを見つけたらそこまでギュッとする
#define int long long
template<typename T>
void fin(T a){
  cout<<a<<endl;
  exit(0);
}
const int INF=1e9+7;
int ans;
typedef pair<int,int> P;

signed main(){
  int n,l;cin>>n>>l;
  vector<int> a(n);
  vector<int> b(n);
  for(int i=0;i<n;i++)cin>>a[i];
  for(int i=0;i<n;i++)cin>>b[i];
  set<int> III,JJJ;
  {
    set<P> s;//(a[i]-i,i)
    s.insert(P(1,-1));
    for(int i=0;i<n;i++){
      if(a[i]<b[i])continue;
      s.insert(P(a[i]-i,i));III.insert(i);
    }//nowまでは左に行きたい
    int L=INF;//Lまでは左に押し付けられてる
    for(int i=n-1;i>=0;i--){
      if(!III.count(i))continue;
      int k=b[i]-i;//cout<<i<<" "<<k<<endl;
      s.erase(P(a[i]-i,i));
      if(i>L&&((L==-1&&b[i]==i+1)||(~L&&b[i]==a[L]+i-L)))continue;
      if(a[i]==b[i])continue;
      P p=*--s.lower_bound(P(k,INF));
      int va=p.first,idx=p.second;
      if(va!=k)fin(-1);
      ans+=i-idx;//cout<<i-idx<<endl;
      L=idx;
    }
  }
  //cout<<"A"<<ans<<endl;
  for(int i=0;i<n;i++)a[i]*=-1;
  for(int i=0;i<n;i++)b[i]*=-1;
  sort(a.begin(),a.end());
  sort(b.begin(),b.end());
  {
    set<P> s;//(a[i]-i,i)
    s.insert(P(-l,-1));
    for(int i=0;i<n;i++){
      if(a[i]<b[i])continue;
      s.insert(P(a[i]-i,i));JJJ.insert(i);
    }//nowまでは左に行きたい
    int L=INF;//Lまでは左に押し付けられてる
    for(int i=n-1;i>=0;i--){
      if(!JJJ.count(i))continue;
      int k=b[i]-i;//cout<<i<<" "<<k<<endl;
      s.erase(P(a[i]-i,i));
      if(i>L&&((L==-1&&b[i]==-l-1+i+1)||(~L&&b[i]==a[L]+i-L)))continue;//cout<<b[i]<<" "<<i<<" "<<L<<" "<<a[L]+i-L<<endl;
      if(a[i]==b[i])continue;
      P p=*--s.lower_bound(P(k,INF));
      int va=p.first,idx=p.second;
      if(va!=k)fin(-1);
      ans+=i-idx;
      L=idx;
      //cout<<i-idx<<endl;
    }
  }
  for(int p:JJJ)III.insert(n-1-p);
  if(III.size()<n)fin(-1);
  cout<<ans<<endl;
}
