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 <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
using vvi = std::vector<std::vector<int>>;
int shrink_to_right(const int left_max_value, const std::vector<std::vector<int>>& left_max, const std::vector<std::vector<int>>& right_min) {
	int min = INT_MAX;
	std::vector<int> right_pos(left_max.size());
	for (auto i = 0; i < left_max.size(); ++i) {
		right_pos[i] = std::distance(left_max[i].begin(), std::upper_bound(left_max[i].begin(), left_max[i].end(), left_max_value));
	}
	int min_pos = right_min.front().size();
	for (auto i = 0; i < right_min.size(); ++i) {
		min_pos = std::min(min_pos, right_pos[i]);
		if (min_pos < right_min[i].size())
			min = std::min(min, right_min[i][min_pos]);
	}
	int result = min;
	min = INT_MAX;
	min_pos = right_min.front().size();
	for (auto i = 1; i <= right_min.size(); ++i) {
		min_pos = std::min(min_pos, right_pos[right_pos.size() - i]);
		if (min_pos < right_min[right_min.size() - i].size())
			min = std::min(min, right_min[right_min.size() - i][min_pos]);
	}
	return std::max(result, min);
}
int first_less_or_equals(const std::vector<int>& array, const int value) {
	int left = 0;
	int right = array.size();
	while (left < right) {
		auto mid = (left + right) / 2;
		if (array[mid] <= value) right = mid;
		else left = mid + 1;
	}
	return right;
}
int shrink_to_left(const int right_max_value, const std::vector<std::vector<int>>& right_max, const std::vector<std::vector<int>>& left_min) {
	int min = INT_MAX;
	std::vector<int> right_pos(right_max.size());
	for (auto i = 0; i < right_max.size(); ++i) {
		right_pos[i] = first_less_or_equals(right_max[i], right_max_value) - 1;
	}
	int max_pos = -1;
	for (auto i = 0; i < left_min.size(); ++i) {
		max_pos = std::max(max_pos, right_pos[i]);
		if (max_pos >= 0)
			min = std::min(min, left_min[i][max_pos]);
	}
	int result = min;
	min = INT_MAX;
	max_pos = -1;
	for (int i = left_min.size() - 1; i >= 0; --i) {
		max_pos = std::max(max_pos, right_pos[i]);
		if (max_pos >= 0)
			min = std::min(min, left_min[i][max_pos]);
	}
	return std::max(result, min);
}
int shrink_to_bottom(const int top_max_value, const std::vector<std::vector<int>>& top_max, const std::vector<std::vector<int>>& bottom_min) {
	int min = INT_MAX;
	std::vector<int> bottom_pos(top_max.size());
	for (auto i = 0; i < top_max.size(); ++i) {
		bottom_pos[i] = std::distance(top_max[i].begin(), std::upper_bound(top_max[i].begin(), top_max[i].end(), top_max_value));
	}
	int min_pos = bottom_min.front().size();
	for (auto i = 0; i < bottom_min.size(); ++i) {
		min_pos = std::min(min_pos, bottom_pos[i]);
		if (min_pos < bottom_min[i].size())
			min = std::min(min, bottom_min[i][min_pos]);
	}
	int result = min;
	min = INT_MAX;
	min_pos = bottom_min.front().size();
	for (auto i = 1; i <= bottom_min.size(); ++i) {
		min_pos = std::min(min_pos, bottom_pos[bottom_pos.size() - i]);
		if (min_pos < bottom_min[bottom_min.size() - i].size())
			min = std::min(min, bottom_min[bottom_min.size() - i][min_pos]);
	}
	return std::max(result, min);
}
int shrink_to_top(const int bottom_max_value, const std::vector<std::vector<int>>& bottom_max, const std::vector<std::vector<int>>& top_min) {
	int min = INT_MAX;
	std::vector<int> bottom_pos(bottom_max.size());
	for (auto i = 0; i < bottom_max.size(); ++i) {
		bottom_pos[i] = first_less_or_equals(bottom_max[i], bottom_max_value) - 1;
	}
	int max_pos = -1;
	for (auto i = 0; i < top_min.size(); ++i) {
		max_pos = std::max(max_pos, bottom_pos[i]);
		if (max_pos >= 0)
			min = std::min(min, top_min[i][max_pos]);
	}
	int result = min;
	min = INT_MAX;
	max_pos = -1;
	for (int i = top_min.size() - 1; i >= 0; --i) {
		max_pos = std::max(max_pos, bottom_pos[i]);
		if (max_pos >= 0)
			min = std::min(min, top_min[i][max_pos]);
	}
	return std::max(result, min);
}
int cal_max_min(const int max_max_value, const vvi& top_max, const vvi& top_min, const vvi& left_max, const vvi& left_min, const vvi& bottom_max, const vvi& bottom_min, const vvi& right_max, const vvi& right_min) {
	return std::max({
		shrink_to_right(max_max_value, left_max, right_min),
		shrink_to_left(max_max_value, right_max, left_min),
		shrink_to_bottom(max_max_value, top_max, bottom_min),
		shrink_to_top(max_max_value, bottom_max, top_min)
		});
}
int main() {
	int h, w; std::cin >> h >> w;
	std::vector<std::vector<int>> state(h, std::vector<int>(w)); for (auto& line : state) for (auto& area : line) std::cin >> area;
	std::vector<std::vector<int>> flipped(w, std::vector<int>(h));
	for (auto i = 0; i < h; ++i) for (auto j = 0; j < w; ++j) flipped[j][i] = state[i][j];
	auto top_max = flipped;
	auto top_min = flipped;
	auto left_max = state;
	auto left_min = state;
	auto bottom_max = flipped;
	auto bottom_min = flipped;
	auto right_max = state;
	auto right_min = state;
	for (auto i = 0; i < h; ++i) for (auto j = 1; j < w; ++j) {
		left_max[i][j] = std::max(left_max[i][j], left_max[i][j - 1]);
		left_min[i][j] = std::min(left_min[i][j], left_min[i][j - 1]);
		right_max[i][w - j - 1] = std::max(right_max[i][w - j - 1], right_max[i][w - j]);
		right_min[i][w - j - 1] = std::min(right_min[i][w - j - 1], right_min[i][w - j]);
	}
	for (auto i = 1; i < h; ++i) for (auto j = 0; j < w; ++j) {
		top_max[j][i] = std::max(top_max[j][i], top_max[j][i - 1]);
		top_min[j][i] = std::min(top_min[j][i], top_min[j][i - 1]);
		bottom_max[j][h - i - 1] = std::max(bottom_max[j][h - i - 1], bottom_max[j][h - i]);
		bottom_min[j][h - i - 1] = std::min(bottom_min[j][h - i - 1], bottom_min[j][h - i]);
	}
	std::vector<std::pair<int, int>> coordinates; coordinates.reserve(h * w);
	for (auto i = 0; i < h; ++i) for (auto j = 0; j < w; ++j) coordinates.emplace_back(i, j);
	std::sort(coordinates.begin(), coordinates.end(), [&state](const std::pair<int, int>& a, const std::pair<int, int>& b) {return state[a.first][a.second] < state[b.first][b.second]; });
	const int max_value = state[coordinates.back().first][coordinates.back().second];
	const int min_value = state[coordinates.front().first][coordinates.front().second];
	auto min = 0;
	auto max = coordinates.size();
	while (min < max) {
		auto mid = (min + max) / 2;
		auto min_max = state[coordinates[mid].first][coordinates[mid].second];
		auto max_min = cal_max_min(min_max, top_max, top_min, left_max, left_min, bottom_max, bottom_min, right_max, right_min);
		auto min_diff = min_max - min_value;
		auto max_diff = max_value - max_min;
		if (min_diff < max_diff) {
			min = mid + 1;
		}
		else {
			max = mid;
		}
	}
	int result = INT_MAX;
	if (min > 0) {
		int min_max = state[coordinates[min - 1].first][coordinates[min - 1].second];
		result = max_value - cal_max_min(min_max, top_max, top_min, left_max, left_min, bottom_max, bottom_min, right_max, right_min);
	}
	if (min < coordinates.size()) {
		int min_max = state[coordinates[min].first][coordinates[min].second];
		result = std::min(result, min_max - min_value);
	}
	std::cout << result << '\n';
}