#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

int main() {
    int n, L;
    cin >> n >> L;
    int a[n+2] = {}, b[n+2] = {};
    srep(i,1,n+1) cin >> a[i];
    srep(i,1,n+1) cin >> b[i];
    a[n+1] = L+1; b[n+1] = L+1;

    ll ans = 0;


    int l = 0;
    int ll[n+2];
    rep(i,n+2){
        if(a[i] == b[i]){
            l = i;
        }else if(a[i] < b[i]){
            l = i;
        }else{
            while(l<i){
                if(a[l] + i - l == b[i]){
                    while(l+1<i){
                        if(a[l+1]==a[l]+1){
                            l++;
                        }else{
                            break;
                        }
                    }
                    break;
                }else{
                    l++;
                }
            }
            if(l == i) {
                cout << -1 << endl;
                return 0;
            }
            // ans += i - l;
        }
        ll[i] = l;
        // cout << i << ' ' << l << endl;
    }

    int rr[n+2];
    int r = n+1;
    drep(i,n+2){
        if(a[i] == b[i]){
            r = i;
        }else if(a[i] > b[i]){
            r = i;
        }else{
            while(i<r){
                if(a[r] - (r - i) == b[i]){
                    while(i<r+1){
                        if(a[r-1]==a[r]-1){
                        r--;
                        }else{
                            break;
                        }
                    }
                    break;
                }else{
                    r--;
                }
            }
            if(r == i) {
                cout << -1 << endl;
                return 0;
            }
            // ans += r - i;
        }
        rr[i] = r;
        // cout << i << ' ' << r << endl;
    }

    int use[n+2] = {};
    drep(i,n+2){
        if(use[ll[i]] == 0){
            use[ll[i]] = 1;
            ans += i - ll[i];
        }
    }
    rep(i,n+2)use[i] = 0;
    rep(i,n+2){
        if(use[rr[i]] == 0){
            use[rr[i]] = 1;
            ans += rr[i] - i;
        }
    }

    cout << ans << endl;
    return 0;
}



#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

LL n,t;

LL calc(LL sum)
{
  LL lb=sum-n,ub=sum-1;
  lb=max(lb,2LL);ub=min(ub,n*2);
  if(ub<n+1) return (lb+ub-2)*(ub-lb+1)/2;
  if(lb>n+1) return (n*4-lb-ub+2)*(ub-lb+1)/2;
  return (lb+n-1)*(n-lb+2)/2+(n*3-ub+1)*(ub-n)/2-n;
}

LL calc2(LL sum,LL i)
{
  if(sum-i<2||sum-i>2*n) return 0;
  if(sum-i<=n+1) return sum-i-1;
  return n*2-(sum-i)+1;
}

int main()
{
  cin>>n>>t;
  LL sum;
  for(LL i=3;;++i)
  {
    LL val=calc(i);
    if(t-val<=0)
    {
      sum=i;
      break;
    }
    t-=val;
  }
  LL bea;
  for(LL i=1;;++i)
  {
    if(t-calc2(sum,i)<=0)
    {
      bea=i;
      break;
    }
    t-=calc2(sum,i);
  }
  LL tas=1;if(sum-bea-1>n) tas+=sum-bea-1-n;
  tas+=t-1;
  cout<<bea<<' '<<tas<<' '<<sum-bea-tas<<endl;
  return 0;
}