Sensor network#1825

Sensor network

60 seconds
256 megabytes
Hard

In response to a request by the SWERC 2010 problem-setting team, a sensor network has just been installed in the headquarters. The organizing committee has instilled in the problem-setters a disproportionate fear of leaking classified information about the problems.

However, in their haste, they forgot to consider the electrical network needed to make the sensors work. As a result, the security system is currently not working, but the large amount of resources spent on it needs to be justified, so the installation must be ready before the contest ends. Therefore, you are asked to write a program to assist the electrician in his work.

Since the organizing committee spared no expense, they ordered the sensors from a prestigious company. Every sensor is handcrafted, and each one has a number written on it, indicating the recommended voltage that should be applied for correct operation. Sensors can function at higher voltages, though this increases the risk of failure, but they should never be used with a voltage below the recommended value. The electrician was surprised when he received the sensors: almost all of them had different recommended voltages! The sensors were installed in the building so that each one controls exactly two doors, and every door is controlled by at least one sensor. Now it is time for the electrician to supply power to the sensors. He faces the following constraints:

For simplicity, a subset of sensors satisfying the first two constraints above is called an admissible subset. The network is designed so that the set of all sensors is always an admissible subset, but it is preferable to select a possibly smaller subset that minimizes the margin, defined as the maximum difference, in absolute value, between the numbers written on any pair of sensors in the subset. (This is to keep the risk of failure as low as possible).

The electrician could not solve the problem of finding the admissible subset of sensors with the minimum margin. Therefore, the electrical installation is still not complete. Today, you are asked to write a program to find the answer, given the sensor network and the recommended voltage for each sensor.

Input

The input consists of several test cases, each separated by a single blank line. Each test case starts with a line containing the integer (2 \leq n \leq 350), the number of doors in the building. The next line contains another integer (n-1 \leq m \leq \frac{n(n-1)}{2}), the number of sensors in the network. The test case ends with lines, each describing one of the sensors. The -th line contains three integers, (0 \leq a \leq n-1), (0 \leq b \leq n-1), and (1 \leq w \leq 2^{15}), in that order. The integers and represent the pair of doors controlled by the -th sensor, and is its recommended voltage. You may safely assume that no two sensors control the same two doors.

The input ends with a line containing .

Output

For each case, your program should output a line containing the minimum margin of an admissible subset of the sensors.

Examples

Example 1

Loading
Loading