Under construction

Still being built. Things may change

Money Copying

Time limit2sMemory limit1024 MB

Summary
Given barter trades between items and won, find the minimum starting money that lets a holder grow money without bound, or INF if none exists.
Level

Medium7 of 10

Topics
Graph, Shortest path, Binary search
Solved
No attempts yet

Problem

The GIST House secondhand market runs events that exchange money for a specific item, or one specific item for another specific item. When items are swapped, extra money may also change hands. The market has unlimited money and items, so any trade can be repeated indefinitely as long as goods remain. However, a customer can hold at most one item at a time.

For example, suppose the following trades exist:

  • t1t_1: A user buys item 11 for 1010 won.
  • t2t_2: A user adds 1010 won and trades it for item 22.
  • t3t_3: A user sells item 22 and receives 2121 won.
  • t4t_4: A user trades item 22 for item 11 and receives an additional 1212 won.
  • t5t_5: A user buys item 22 for 77 won.

Suppose the development team allows only the trades in the list L1={t1,t2,t3}L_1 = \{t_1, t_2, t_3\}. Then choosing t1,t2,t3t_1, t_2, t_3 in this order turns 2020 won into 2121 won. Repeating this indefinitely lets the money grow without bound. Copied money can harm the national economy, so the development team wants to prevent this. Given a list of trades, the team wants to determine whether the list is safe.

The safety level of a list is defined as follows.

  • If a person starts with CC won and can grow their money without bound by repeating trades, the safety level of the list is the minimum value of CC. If no such CC exists, the safety level is ∞\infty. To grow the money, it is not necessary to sell every item.

For example, the safety level of L1={t1,t2,t3}L_1 = \{t_1, t_2, t_3\} is 2020, because starting with 2020 won and repeating t1,t2,t3t_1, t_2, t_3 in order works. However, a person with 1919 won cannot grow their money without bound no matter what they do. The safety level of L2={t3,t4,t5}L_2 = \{t_3, t_4, t_5\} is 77.

Meanwhile, the safety level of L3={t2,t3,t4}L_3 = \{t_2, t_3, t_4\} is ∞\infty. It seems that repeating t2t_2 and t4t_4 would grow the money without bound, but there is no way to obtain item 11 or item 22, so those trades cannot be attempted.

Write a program that, given a list of barter trades, measures the safety level of that list.

Input

The first line contains the number of items NN and the length of the barter trade list MM. (1≤N≤1 000,1≤M≤min⁡(N2+N,3 000)1 \leq N \leq 1\,000, 1 \leq M \leq \min\left(N^2+N, 3\,000\right))

From the second line, the next MM lines each give one piece of information about the trade list LL in one of the following five formats.

  • 1 c i1\ c\ i: You can buy item ii for cc won.
  • 2 i c2\ i\ c: You can sell item ii and earn cc won.
  • 3 i1 i23\ i_1\ i_2: You can trade item i1i_1 for item i2i_2.
  • 4 i1 c i24\ i_1\ c\ i_2: You can exchange item i1i_1 and cc won for item i2i_2.
  • 5 i1 i2 c5\ i_1\ i_2\ c: You can exchange item i1i_1 for item i2i_2 and cc won.

ii, i1i_1, and i2i_2 are integers from 11 to NN. cc is an integer from 11 to 1 0001\,000. The values on each line are separated by spaces.

In the trade list LL, formats 33, 44, and 55 each satisfy i1≠i2i_1 \neq i_2.

No two lines of format 11 share the same ii.

No two lines of format 22 share the same ii.

No two lines of formats 33, 44, or 55 share both the same i1i_1 and i2i_2. For example, 3 1 2 and 4 1 1 2 are never given together. However, 3 1 2 and 3 1 3 can be given together, because they share i1i_1 but not i2i_2.

Trades are not reversible. For example, the input 3 1 2 does not mean you can trade item 22 for item 11.

Output

Print the safety level of the list. If the safety level is ∞\infty, print the string INF.

Examples1

  1. Example 1

    Input
    5 8
    1 10 5
    2 1 10
    4 1 15 4
    4 2 10 3
    4 3 10 4
    4 5 10 2
    5 2 1 10
    5 4 5 20
    
    Expected output
    25