#include "bits/stdc++.h" using namespace std; #define all(x) begin(x),end(x) template ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } template::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #define debug(a) cerr << "(" << #a << ": " << a << ")\n"; typedef long long ll; typedef vector vi; typedef vector vvi; typedef pair pi; const int mxN = 1e5+1, oo = 1e9; /* Take adjacent differences Then want to do some greedy? Can do +1 or -1 on one of them. need to make everything 0 can balance out +1's and -1's. can do +M or -M free of charge. either needs +1's or -1's can try all those options of switchings? */ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n>> m; vi a(n),b(n); for(auto& i : a) cin >> i; for(auto& i : b) cin >> i; vi c(n); c[0]=(a[0]-b[0]+m)%m; for(int i=1;i=0;--i) { auto cur = c[i]; if(cur) { minus-=c[i]; plus+=m-c[i]; } ans=min(ans,max(minus,plus)); } cout << ans << '\n'; }