Please subscribe to the official Codeforces channel in Telegram via the link: https://t.me/codeforces_official.
×

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ACM-ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

No tag edit access

A. Dorm Water Supply

time limit per test

1 secondmemory limit per test

256 megabytesinput

standard inputoutput

standard outputThe German University in Cairo (GUC) dorm houses are numbered from 1 to *n*. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).

For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.

In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap.

Input

The first line contains two space-separated integers *n* and *p* (1 ≤ *n* ≤ 1000, 0 ≤ *p* ≤ *n*) — the number of houses and the number of pipes correspondingly.

Then *p* lines follow — the description of *p* pipes. The *i*-th line contains three integers *a*_{i} *b*_{i} *d*_{i}, indicating a pipe of diameter *d*_{i} going from house *a*_{i} to house *b*_{i} (1 ≤ *a*_{i}, *b*_{i} ≤ *n*, *a*_{i} ≠ *b*_{i}, 1 ≤ *d*_{i} ≤ 10^{6}).

It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it.

Output

Print integer *t* in the first line — the number of tank-tap pairs of houses.

For the next *t* lines, print 3 integers per line, separated by spaces: *tank*_{i}, *tap*_{i}, and *diameter*_{i}, where *tank*_{i} ≠ *tap*_{i} (1 ≤ *i* ≤ *t*). Here *tank*_{i} and *tap*_{i} are indexes of tank and tap houses respectively, and *diameter*_{i} is the maximum amount of water that can be conveyed. All the *t* lines should be ordered (increasingly) by *tank*_{i}.

Examples

Input

3 2

1 2 10

2 3 20

Output

1

1 3 10

Input

3 3

1 2 20

2 3 10

3 1 5

Output

0

Input

4 2

1 2 60

3 4 50

Output

2

1 2 60

3 4 50

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Dec/14/2018 00:37:36 (d1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|