Under construction

Still being built. Things may change

School Tour

Interview

Time limit1sMemory limit256 MB

Summary
Choose a spanning tree of the graph rooted at the entrance that contains the fixed edge to building 1, find the min and max possible number of uphill edges among its edges, and print (max)^2 - (min)^2.
Level

Medium6 of 10

Topics
Minimum spanning tree, Union-find, Greedy, Graph
Solved
No attempts yet

Problem

Gukhee guides high school students around the campus during the summer vacation. The campus is on a hillside, so a road between two buildings is either an uphill road or a downhill road. Gukhee surveyed every road once, taking the entrance as the starting point.

The buildings are numbered 11 through NN, and the entrance is numbered 00. Exactly one road leaves the entrance, and it leads to building 11.

To introduce every building, Gukhee has to choose a set of roads that connects the entrance and all NN buildings, and she chooses as few roads as possible. There are N+1N+1 places in total, so she chooses exactly NN roads, and those NN roads alone must let her reach every building from the entrance.

The fatigue of a choice is k2k^2, where kk is the number of uphill roads among the chosen roads. Uphill and downhill come from the original survey only, so walking down a downhill road and climbing back up it later is not counted. The road between the entrance and building 11 belongs to every choice, so it counts as well.

Among all valid choices, one has the largest fatigue and one has the smallest. Find the difference between the two.

Input

The first line has the number of buildings NN (1≤N≤10001 \le N \le 1000) and the number of roads between buildings MM (1≤M≤N(N−1)/21 \le M \le N(N-1)/2).

The second line describes the road between the entrance and building 11 in the form AA BB CC. Here AA is always 00 and BB is always 11.

Each of the next MM lines has AA BB CC, meaning that a road joins building AA and building BB (1≤A,B≤N1 \le A, B \le N, A≠BA \ne B). CC is 00 for an uphill road and 11 for a downhill road.

No pair of buildings is joined by more than one road. Every building is reachable from the entrance.

Output

Print the difference between the largest fatigue and the smallest fatigue on one line.

Examples3

  1. Example 1

    Input
    4 5
    0 1 1
    1 2 0
    1 4 0
    4 2 1
    3 4 1
    2 3 0
    
    Expected output
    8
    
  2. Example 2

    Input
    2 1
    0 1 0
    1 2 0
    
    Expected output
    0
    
  3. Example 3

    Input
    3 3
    0 1 0
    1 2 0
    2 3 1
    1 3 1
    
    Expected output
    3