#include <bits/stdc++.h>
using namespace std;

int main()
{
  int a, b;
  cin >> a >> b;
  vector<int> va(a), vb(b);
  iota(begin(va), end(va), 1);
  iota(begin(vb), end(vb), -b);
  int sa = a * (a + 1) / 2;
  int sb = b * (b + 1) / 2;
  if (sa < sb) {
    va[a - 1] += sb - sa;
  } else if (sa > sb) {
    vb[0] -= sa - sb;
  }
  for (auto&& e : va) {
    cout << " " << e;
  }
  for (auto&& e : vb) {
    cout << " " << e;
  }
  cout << endl;
}

#ifdef __LOCAL
  #define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
template<typename T> bool chmax(T &a,T b) {if(a<b) {a=b; return true;} return false;}
template<typename T> bool chmin(T &a,T b) {if(a>b) {a=b; return true;} return false;}
#define itn int
#define fi first
#define se second
#define intmax numeric_limits<int>::max()
#define llmax numeric_limits<ll>::max()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(int)(n);i>=1;i--)
#define all(vec) vec.begin(),vec.end()
#define sortt(vec) sort((vec).begin(),(vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef tuple<ll,ll,ll> tlll;
typedef tuple<int,int,int> tiii;
const ll mod=1e9+7;
const int inf=1<<30;
const ll lnf=1ll<<60;

int main(){
  ll n,l; cin >> n >> l;
  vector<ll> a(n);
  rep(i,n){
    cin >> a[i];
    a[i]--;
  }
  vector<ll> b(n);
  rep(i,n){
    cin >> b[i];
    b[i]--;
  }
  unordered_map<ll,set<ll>> mp;
  mp[0].insert(-1);
  mp[l-n].insert(n);
  rep(i,n){
    mp[a[i]-i].insert(i);
  }
  ll ans=0;
  rep(i,n){
    if(mp.count(b[i]-i)){
      if(a[i]!=b[i]){
        ll add=min(abs(*begin(mp[b[i]-i])-i),abs(*rbegin(mp[b[i]-i])-i));
        if(*begin(mp[b[i]-i])<=i&&i<=*rbegin(mp[b[i]-i])) add=0;
        ans+=add;
        mp[b[i]-i].insert(i);
      }
    }
    else{
      cout << -1 << endl;
      return 0;
    }
  }
  if(a==b) cout << 0 << endl;
  else cout << ans << endl;
  
}