/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll conbinationMemo[100][100];

void cmemoInit(){
    rep(i, 100){
        rep(j, 100){
            conbinationMemo[i][j] = -1;
        }
    }
}
 
ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
int main(void){
    ll r1, c1;
    cin >> r1 >> c1;
    ll r2, c2;
    cin >> r2 >> c2;

    if(r1 == r2 && c1 == c2){
        cout << 0 << endl;
        return 0;
    }

    if(abs(r1 - r2) + abs(c1 - c2) <= 3){
        cout << 1 << endl;
        return 0;
    }

    if(r1 + c1 == r2 + c2 || r1 - c1 == r2 - c2){
        cout << 1 << endl;
    } else {
        if(abs((r1 + c1) - (r2 + c2)) <= 3 || abs((r1 - c1) - (r2 - c2)) <= 3){
            cout << 2 << endl;
        } else {
            if((r1 + c1) % 2 == (r2 + c2) % 2){
                cout << 2 << endl;
            } else {
                cout << 3 << endl;
            }
        }
    }
}
//#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;


using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please


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


	int N, L;
	cin >> N >> L;

	int A[100100], B[100100];
	rep1(i, N) cin >> A[i];
	rep1(i, N) cin >> B[i];
	A[0] = 0;
	B[0] = 0;
	A[N + 1] = L + 1;
	B[N + 1] = L + 1;

	ll kotae = 0;
	int k = 0;
	int a = 1;
	rep1(b, N + 1) {
		chmax(a, b);
		if (A[b] < B[b]) {
			while (A[a] < B[b] + k) {
				a++;
				k++;
			}
			if (A[a] == B[b] + k) {
				kotae += k;
				k--;
				while (k && B[b] + 1 == B[b + 1]) {
					b++;
					k--;
				}
			}
		}
		else if (k) {
			co(-1);
			return 0;
		}
	}

	reverse(A, A + N + 2);
	reverse(B, B + N + 2);
	rep(i, N + 2) A[i] = L + 1 - A[i];
	rep(i, N + 2) B[i] = L + 1 - B[i];
	k = 0;
	a = 1;
	rep1(b, N + 1) {
		chmax(a, b);
		if (A[b] < B[b]) {
			while (A[a] < B[b] + k) {
				a++;
				k++;
			}
			if (A[a] == B[b] + k) {
				kotae += k;
				k--;
				while (k && B[b] + 1 == B[b + 1]) {
					b++;
					k--;
				}
			}
		}
		else if (k) {
			co(-1);
			return 0;
		}
	}

	co(kotae);

	Would you please return 0;
}