Problem: The Kingdom of JOIOI is a rectangular grid of $H \times W$ cells. In the Kingdom of JOIOI, in order to improve efficiency of administrative institutions, the country will be divided into two regions called "JOI" and "IOI."

Since we do not want to divide it in a complicated way, the division must satisfy the following conditions:

* Each region must contain at least one cell.
* Each cell must belong to exactly one of two regions.
* For every pair of cells in the JOI region, we can travel from one to the other by passing through cells belonging to the JOI region only. When move from a cell to another cell, the two cells must share an edge. The same is true for the IOI region.
* For each row or column, if we take all the cells in that row or column, the cells belonging to each region must be connected. All the cells in a row or column may belong to the same region.



Each cell has an integer called the altitude. After we divide the country into two regions, it is expected that traveling in each region will be active. But, if the difference of the altitudes of cells are large, traveling between them becomes hard. Therefore, we would like to minimize the maximum of the difference of the altitudes between cells belonging to the same region. In other words, we would like to minimize the larger value of

* the difference between the maximum and the minimum altitudes in the JOI region, and
* the difference between the maximum and the minimum altitudes in the IOI region.



Task

Given the altitudes of cells in the Kingdom of JOIOI, write a program which calculates the minimum of the larger value of the difference between the maximum and the minimum altitudes in the JOI region and the difference between the maximum and the minimum altitudes in the IOI region when we divide the country into two regions.

Input

Read the following data from the standard input.

* The first line of input contains two space separated integers $H$, $W$. This means the Kingdom of JOIOI is a rectangular grid of $H \times W$ cells.
* The $i$-th line ($1 \leq i \leq H$) of the following $H$ lines contains $W$ space separated integers $A_{i,1}, A_{i,2}, ..., A_{i,W}$. This means the cell in the $i$-th row from above and $j$-th column from left ($1 \leq j \leq W$) has altitude $A_{i,j}$.



Output

Write one line to the standard output. The output contains the minimum of the larger value of the difference between the maximum and the minimum altitudes in the JOI region and the difference between the maximum and the minimum altitudes in the IOI region when we divide the country into two regions.

Constraints

All input data satisfy the following conditions.

* $2 \leq H \leq 2 000．$
* $2 \leq W \leq 2 000．$
* $1 \leq A_{i, j} \leq 1 000 000 000 (1 \leq i \leq H, 1 \leq j \leq W)．$



Sample Input and Output

Sample Input 1


4 4
1 12 6 11
11 10 2 14
10 1 9 20
4 17 19 10


Sample Output 1


11


For example, in this sample input, we divide the country into two regions as follows. Here, ‘J’ denotes the JOI region, and ‘I’ denotes the IOI region.

<image>

The following division does not satisfy the condition because, in the third column from left, cells with ‘I’ are not connected.

<image>

Sample Input 2


8 6
23 23 10 11 16 21
15 26 19 28 19 20
25 26 28 16 15 11
11 8 19 11 15 24
14 19 15 14 24 11
10 8 11 7 6 14
23 5 19 23 17 17
18 11 21 14 20 16


Sample Output 2


18





Creative Commonse License

The 16th Japanese Olympiad in Informatics (JOI 2016/2017) Final Round





Example

Input

4 4
1 12 6 11
11 10 2 14
10 1 9 20
4 17 19 10


Output

11

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T,class U> using P = pair<T,U>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;

constexpr int inf = 1e9+1;
int ma[4][2010][2010] = {};
int mi[4][2010][2010] = {};
int exist[4][2010] = {};
int A[4][2010][2010] = {};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H,W;
    int M = 0;
    cin >> H >> W;
    for(int i=0;i<H;i++) for(int j=0;j<W;j++){
        cin >> A[0][i][j];
        A[1][W-j-1][i] = A[0][i][j];
        M = max(M,A[0][i][j]);
    }
    for(int i=0;i<H;i++) for(int j=0;j<W;j++) A[2][i][j] = A[0][i][W-j-1];
    for(int i=0;i<W;i++) for(int j=0;j<H;j++) A[3][i][j] = A[1][i][H-j-1];

    for(int t=0;t<4;t++) for(int i=0;i<=max(H,W);i++){
        exist[t][i] = -1;
        for(int j=0;j<=max(H,W);j++){
            ma[t][i][j] = 0;
            mi[t][i][j] = inf;
        }
    }

    auto calc = [&](int H,int W,int t){
        for(int i=0;i<H;i++) for(int j=W-1;j>=0;j--){
            ma[t][i][j] = max(ma[t][i][j+1],A[t][i][j]);
            mi[t][i][j] = min(mi[t][i][j+1],A[t][i][j]);
            if(A[t][i][j]==M) exist[t][i] = max(exist[t][i],j);
        }
    };

    calc(H,W,0);
    calc(W,H,1);
    calc(H,W,2);
    calc(W,H,3);
    
    auto check = [&](int H,int W,int d,int t){
        bool res = false;
        int ma2 = 0,mi2 = inf,pl = W-1;
        bool judge = true;
        for(int i=0;i<H;i++){
            int l = -1;
            for(int j=0;j<=pl;j++){
                if(A[t][i][j]<M-d) break;
                l = j;
            }
            if(l<exist[t][i]){
                judge = false;
                break;
            }
            mi2 = min(mi2,mi[t][i][l+1]);
            ma2 = max(ma2,ma[t][i][l+1]);
            pl = l;
        }
        res |= judge && ma2-mi2<=d;
        ma2 = 0,mi2 = inf,pl = W-1,judge = true;
        for(int i=H-1;i>=0;i--){
            int l = -1;
            for(int j=0;j<=pl;j++){
                if(A[t][i][j]<M-d) break;
                l = j;
            }
            if(l<exist[t][i]){
                judge = false;
                break;
            }
            mi2 = min(mi2,mi[t][i][l+1]);
            ma2 = max(ma2,ma[t][i][l+1]);
            pl = l;
        }
        res |= judge && ma2-mi2<=d;
        return res;
    };

    auto ok = [&](int d){
        bool res = false;
        res |= check(H,W,d,0);
        res |= check(W,H,d,1);
        res |= check(H,W,d,2);
        res |= check(W,H,d,3);
        return res;
    };

    int l = -1,r = M;
    while(l+1<r){
        int m = (l+r)>>1;
        (ok(m)? r:l) = m;
    }
    cout << r << endl;
}