#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define RF(x) freopen(x,"r",stdin)
#define WF(x) freopen(x,"w",stdout)
typedef long long LL;
using namespace std;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
const LL MOD = (LL)998244353;
const int SIZE = 2e5+5;
const LL INF = 1LL<<60;
const double eps = 1e-4;
const double PI=3.1415926535897932;

int a[100009];
int b[100009];
int c[100009];
map<int,vector<int> > m;

int main(){
    DRI(n);
    RI(a[n+1]);a[n+1]++;
    REPP(i,1,n+1)RI(a[i]);
    REPP(i,1,n+1)RI(b[i]);
    b[n+1]=a[n+1];
    REP(i,n+2){
        m[a[i]-i].PB(i);
    }
    LL ans = 0;
    REPP(i,1,n+1){
        if(!m.count(b[i]-i)){
            puts("-1");return 0;
        }
        const vector<int> &v = m[b[i]-i];
        auto it = lower_bound(ALL(v),i);
        if(it==v.end()){
            //ans += abs(i-*prev(it));
            c[i] = *prev(it);
        }
        else if(it==v.begin()){
            //ans += abs(*it - i);
            c[i] = *it;
        }
        else{
            //ans += min(abs(*it - i),abs(i-*prev(it)));
            if(abs(*it - i)<abs(i-*prev(it)))c[i] = *it;
            else c[i] = *prev(it);
        }
    }
    c[n+1]=n+1;
    REPP(i,1,n+1){
        //printf("%d ",c[i]);
        if(a[i]==b[i])continue;
        bool same = (c[i]>i && c[i+1] == c[i])||(c[i]<i && c[i-1] == c[i]);
        if (same) ans++;
        else {
            ans += abs(i-c[i]);
        }
    }
    printf("%lld",ans);
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1.1e5;
int n,m,ta,tb;
struct qj{
	int l,r;
}a[N],b[N];
int main()
{
	cin>>n>>m;int la=0;
	for(int i=1,x;i<=n;i++){
	scanf("%d",&x);if(la+1<=x-1)a[++ta]=(qj){la+1,x-1};la=x;}
	int x=m;if(la+1<=x)a[++ta]=(qj){la+1,x};la=0;
	for(int i=1,x;i<=n;i++){
	scanf("%d",&x);if(la+1<=x-1)b[++tb]=(qj){la+1,x-1};la=x;}
	x=m;if(la+1<=x)b[++tb]=(qj){la+1,x};la=0;
	ll ans=0;int j=1;
	for(int i=1;i<=tb;i++){//cout<<i<<" "<<ans<<endl;
		if(a[j].l<b[i].l)ans+=b[i].l-a[j].l;
		while(b[i].l<=b[i].r){
			if(a[j].r-a[j].l>b[i].r-b[i].l)return puts("-1"),0;
			b[i].l+=(a[j].r-a[j].l+1);if(b[i].l>b[i].r){
				if(a[j].r>b[i].r)ans+=a[j].r-b[i].r;j++;break;
			}j++;
		}
	}cout<<ans;
}