Proving Propositions

Choose directed edges so all N propositions become mutually reachable, minimizing the difference between the hardest and easiest chosen proof difficulties.

Medium7GraphTwo pointersUnion-findMinimum spanning treeNo attempts yetTime limit2sMemory limit512 MB

Problem

Yeongseon is studying for a calculus exam and wants to prove that NN propositions are all equivalent. The propositions are numbered from 0 to N1N-1.

For any two different propositions xx and yy, Yeongseon can prove "if xx then yy", and every such proof has its own difficulty. The difficulty of "if xx then yy" and the difficulty of "if yy then xx" may differ.

Today's task is to choose a set of proofs so that any two propositions imply each other, either directly or through other propositions. For example, when N=3N = 3, one way is to prove 0 => 1, 1 => 0, 0 => 2, 2 => 0. Another way is to prove 0 => 1, 1 => 2, 2 => 0.

Among the chosen proofs, Yeongseon wants the gap between the hardest difficulty and the easiest difficulty to be as small as possible. Write a program that finds that minimum gap.

Input

The first line contains the number of propositions NN (1N501 \le N \le 50).

Each of the next NN lines contains NN integers, the difficulties of the proofs. The jj-th integer on the ii-th line is the difficulty of proving ii => jj. Numbering starts at 0.

Every difficulty is an integer between 0 and 150,000, and the ii-th integer on the ii-th line is always 0.

Output

Print on the first line the smallest possible difference between the hardest and the easiest difficulty among the chosen proofs, over all choices that make every proposition imply every other one.

When N=1N = 1, no proof is needed, so print 0.