#include using namespace std; #define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i) #define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i) #define REP(i, n) rep(i, 0, n) #define REP2(i, n) rep2(i, 0, n) #define all(hoge) (hoge).begin(), (hoge).end() #define en '\n' using ll = long long; using ull = unsigned long long; template using vec = vector; template using vvec = vector>; typedef pair P; using tp = tuple; constexpr long long INF = 1LL << 60; constexpr int INF_INT = 1 << 25; //constexpr long long MOD = (ll)1e9 + 7; constexpr long long MOD = 998244353LL; using ld = long double; static const ld pi = 3.141592653589793L; typedef vector Array; typedef vector Matrix; template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } //グラフ関連 struct Edge { ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector Edges; typedef vector Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { G[from].push_back(Edge(to, cap, (ll)G[to].size())); if(revFlag) G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); } void solve() { ll n, l; cin >> n >> l; vec x(n); vec y(n); set stx, sty; REP(i, n) { cin >> x[i]; stx.insert(x[i]); } REP(i, n) { cin >> y[i]; sty.insert(y[i]); } auto it = stx.begin(); ll ans = 0; ll t = 0; while(stx.size()) { //次のお茶 auto sy = sty.upper_bound(*it); if(sy == sty.end()) { sy = sty.begin(); t++; } //次のお茶より後にある寿司 auto tx = stx.upper_bound(*sy); if(tx == stx.end()) { tx = stx.begin(); t++; } //効率を落とさずに最後に飲めるお茶 auto ty = sty.upper_bound(*tx); if(ty == sty.begin()) ty = sty.end(); ty--; if(stx.size() == 1) { cout << l * (t - 1) + *ty << en; return; } sty.erase(ty); //最後のお茶をのむ if(*it == *tx) { it = stx.erase(it); } else { stx.erase(it); it = tx; //次の寿司 } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); /* ll t; cin >> t; while(t--)*/ solve(); return 0; }