Mobile Network Bandwidth

Given a graph whose edge capacities are polynomials in x, output the max-flow polynomial from node 1 to node N for large x.

Hard9GraphGreedyMathImplementationNo attempts yetTime limit8sMemory limit512 MB

Problem

Internet traffic keeps growing because of smartphones, so wireless carriers have to expand their networks.

The network of a carrier consists of base stations and lines. Each line connects two base stations in both directions. The bandwidth of a line grows every year and is given as a polynomial f(x)f(x) of the year xx.

Given the structure of the network, compute the maximum bandwidth between base station 1 and base station NN as a polynomial of xx.

The traffic between the two stations can be split over several routes. The traffic on a line cannot exceed the bandwidth of that line, so the maximum bandwidth equals the maximum flow from station 1 to station NN.

Which line is the bottleneck depends on xx. Exactly one polynomial equals the maximum bandwidth for every sufficiently large xx. Print that polynomial.

Input

The input consists of several datasets. Each dataset has the following format.

N M
u1 v1 p1
...
uM vM pM

The first line contains the number of base stations NN (2N502 \le N \le 50) and the number of lines MM (0M5000 \le M \le 500). The next MM lines describe the network. The ii-th of them contains the station indices uiu_i and viv_i (1ui,viN1 \le u_i, v_i \le N) and the polynomial pip_i that gives the bandwidth of the line. Several lines may connect the same pair of stations, and a line may have both of its ends at the same station.

A polynomial has the form

aLxL+aL1xL1++a1x+a0a_L x^L + a_{L-1} x^{L-1} + \cdots + a_1 x + a_0

where LL (0L500 \le L \le 50) is the degree and aia_i (0iL0 \le i \le L, 0ai1000 \le a_i \le 100) are the coefficients. The input writes a polynomial by these rules.

  • A term aixia_i x^i with i2i \ge 2 is written as <a_i>x^<i>.
  • The linear term a1xa_1 x is written as <a_1>x.
  • The constant term a0a_0 is written as digits only.
  • The terms are written in strictly decreasing order of degree and joined by +.
  • For a term other than the constant one, the coefficient is omitted when ai=1a_i = 1.
  • A term with ai=0a_i = 0 is omitted entirely.
  • A polynomial contains no space and no character other than digits, x, ^, and +.

So 2x2+3x+52x^2 + 3x + 5 is written as 2x^2+3x+5, and 2x3+x2x^3 + x is written as 2x^3+x, never as 2x^3+0x^2+1x+0. No polynomial in the input has all coefficients equal to 00.

The end of the input is a line with two zeros. That line is not a dataset.

Output

For each dataset, print the maximum bandwidth as a polynomial of xx on one line. Use the same notation as the input, except that the answer may be the constant 00, which is printed as 0.