Under construction

Still being built. Things may change

Bicycle Race

Time limit8sMemory limit256 MB

Summary
Find two triangles that share one host city and have five distinct cities in total, maximizing the sum of their six road values, or report -1 if none exists.
Level

Medium7 of 10

Topics
Graph, Brute force, Greedy
Solved
No attempts yet

Problem

The Great Bicycle Race is approaching, and the organizers of the annual sports competition need to choose a track for the racers. The country has nn cities and mm bidirectional roads. The organizers pick one city as the host. The race lasts two days. Each day, riders start at the host city, pass through two intermediate cities, and return to the host city. The intermediate cities must not repeat across the two days, so the race uses five distinct cities and six distinct roads in total. Each road ii has an integer attractiveness aia_i, and the attractiveness of a route is the sum of the six road values. Determine whether a valid race exists, and if so, the maximum possible attractiveness of a route.

Input

The first line contains two integers nn and mm (5≤n≤1000005 \le n \le 100000, 6≤m≤1000006 \le m \le 100000), the number of cities and roads. Each of the next mm lines contains three integers uiu_i, viv_i, and aia_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i, 1≤ai≤1000001 \le a_i \le 100000): the two cities connected by road ii and its attractiveness. No two roads connect the same pair of cities.

Output

If no valid route exists, print −1-1. Otherwise, print the maximum total attractiveness of a valid route.

Hint

In the first sample, one of the best choices is city 5 as the host, with the route 5 → 1 → 2 → 5 → 3 → 4 → 5.

Examples2

  1. Example 1

    Input
    6 9
    1 2 3
    2 3 1
    3 4 2
    4 5 1
    3 5 7
    2 5 2
    1 5 3
    4 6 2
    5 6 1
    
    Expected output
    18
    
  2. Example 2

    Input
    6 6
    1 3 2
    1 2 2
    2 3 4
    3 4 1
    3 6 6
    1 4 8
    
    Expected output
    -1