#define I inline #define fi first #define se second #define pb push_back #define MP make_pair #define PII pair<int , int> #define PSL pair<string , long long> #define PLL pair<long long , long long> #define all(x) (x).begin() , (x).end() #define copy(a , b) memcpy(a , b , sizeof(b)) #define clean(a , b) memset(a , b , sizeof(a)) #define rep(i , l , r) for (int i = (l); i <= (r); i ++) #define per(i , r , l) for (int i = (r); i >= (l); i --) #define PE(i , x) for(int i = head[x]; i; i = edge[i].last) #define DEBUG(x) std :: cerr << #x << '=' << x << std :: endl
typedeflonglong ll; typedefunsignedlonglong ull;
template <classT> inlinevoidread(T &x){ char c = getchar(), f = 0; x = 0; while (!isdigit(c)) f = (c == '-'), c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x = f ? -x : x; }
usingnamespacestd;
constint N = 10000 + 5; constint M = 100000 + 5; constint HA = 998244353;
int n , m; ll s[2005][2005]; ll maxn = -0x3f3f3f3f , minn = 0x3f3f3f3f;
boolcheck(int x){ int poi = m + 1; rep(i , 1 , n) { int t = 0; rep(j , 1 , min(poi , m)) { if(maxn - s[i][j] <= x) t = max(t , j); elsebreak; } poi = t; rep(j , t + 1 , m) { if(s[i][j] - minn > x) return0; } } return1; }
ll solve(){ ll l = 0 , r = maxn - minn; while(l + 1 < r) { int mid = (l + r) >> 1; if(check(mid)) r = mid; else l = mid; } if(check(l)) return l; elsereturn r; }