Minimum weight spread for strong connectivity

Pick a strongly connected subgraph of a complete directed graph so the chosen edges' weight range is as small as possible.

Medium6GraphSortingTwo pointersUnion-findNo attempts yetTime limit2sMemory limit512 MB

Problem

There is a directed weighted graph with NN vertices numbered 0 to N1N-1. For every ordered pair of distinct vertices xx and yy there is one edge from xx to yy, so the graph has N×(N1)N \times (N-1) edges in total.

Choose some of these edges to build a new graph. Using only the chosen edges, a path from any vertex uu to any vertex vv must always exist. For example, when N=3N=3 you may choose 010 \Rightarrow 1, 101 \Rightarrow 0, 020 \Rightarrow 2, 202 \Rightarrow 0, and you may also choose 010 \Rightarrow 1, 121 \Rightarrow 2, 202 \Rightarrow 0.

Among the chosen edges, make the difference between the largest weight and the smallest weight as small as possible. Find the minimum of that difference over all valid choices. When N=1N=1 there is no edge to choose, so the answer is 0.

Input

The first line contains NN, a natural number at most 50.

Each of the next NN lines holds one row of the weight matrix. The y+1y+1-th value from the left on the x+1x+1-th of these lines is the weight of the edge from vertex xx to vertex yy. Every weight is an integer between 0 and 150,000. The self loop from vertex xx to vertex xx is always given as 0, and self loops cannot be chosen.

Output

Print one integer, the minimum possible difference between the largest and the smallest chosen weight.