#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int inf = 1e9;

int a[maxn], b[maxn];

int main(){
	ios_base::sync_with_stdio(false);
	int n, L;
	cin >> n >> L;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	a[0] = 0, a[n+1] = L+1;
	for (int i = 1; i <= n; i++)
		cin >> b[i];
	b[0] = 0, b[n+1] = L+1;
	int last = 1;
	ll answer = 0;
	set<pair<int,int>> S, T;
	for (int i = 0; i <= n+1; i++){
		S.insert({a[i]-i,-i});
		T.insert({a[i]-i,+i});
	}
	for (int i = 1; i <= n+1; i++){
		if (a[i] > b[i]){
			if (b[i-1]+1 == b[i]){
				answer ++;
				continue;
			}
			auto it = S.lower_bound({b[i]-i,-inf});
			if ((*it).first != b[i]-i)
				return cout << "-1\n", 0;
			answer += i+(*it).second;
		}
		if (a[i] < b[i]){
			if (b[i]+1 == b[i+1]){
				answer ++;
				continue;
			}
			auto it = T.lower_bound({b[i]-i,-inf});
			if ((*it).first != b[i]-i)
				return cout << "-1\n", 0;
			answer += (*it).second-i;
		}
	}
	cout << answer << '\n';
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a, b; cin >> a >> b;
  int ans = 0;
  for(int i=1; i<=b-a+1; i++) {
    int _a = (a+i-1)/i, _b = b/i;
    if(_b-_a+1 >= 2) {
      chmax(ans, i);
    }
  }
  cout << ans << endk;
  return 0;
}
