Under construction

Still being built. Things may change

Signs on the Road

Time limit2sMemory limit1024 MB

Summary
Find the lexicographically smallest digit string written while walking a graph to visit all towns, and the minimum toll for that string.
Level

Hard9 of 10

Topics
Graph, Minimum spanning tree, Greedy, String
Solved
No attempts yet

Problem

Woohyeon became a traveling salesman and needs to visit every town numbered from 11 to NN. He is currently in town 11. There are MM roads between the towns, and any two towns are connected by at most one road. Each road has two properties. First, each road has a sign with a digit from 11 to 99. Second, each road has a toll, but the toll is paid only the first time the road is crossed. Crossing the same road again costs nothing.

Whenever Woohyeon crosses a road, he writes the digit on its sign on his phone. He can place the cursor anywhere before typing, so after writing "321" and crossing a road with a 4 sign, he can produce "3421" or "3214". However, he does not write the digit of a road he has already crossed.

Woohyeon never lacks the money for a toll, but he does not want to pay more than necessary. Still, he wants the number on his phone to be as small as possible, even if that costs a little more. Find the smallest number he can write on his phone while visiting all NN towns, and the minimum toll needed to write that number.

Input

The first line contains NN and MM, separated by a space. (1≤N≤200 0001 \le N \le 200\,000, 1≤M≤500 0001 \le M \le 500\,000)

Each of the next MM lines contains x y z wx\, y\, z\, w, separated by spaces. This means towns xx and yy are connected by a road with sign zz, and its toll is ww won. (1≤x,y≤N1 \le x, y \le N, x≠yx \ne y, 1≤z≤91 \le z \le 9, 0≤w≤1060 \le w \le 10^6)

Output

If it is impossible to visit all NN towns, print -1 on the first line.

If it is possible, print on the first line the smallest number that can be written on the phone while visiting all NN towns, and the minimum toll needed to write that number, separated by a space.

Examples2

  1. Example 1

    Input
    5 7
    2 3 7 10
    1 2 5 20
    3 1 4 30
    3 5 1 15
    4 2 9 50
    4 5 9 35
    2 5 2 40
    
    Expected output
    1249 120
    
  2. Example 2

    Input
    5 4
    1 2 1 1000
    2 3 2 1000
    3 1 3 1000
    4 5 4 1000
    
    Expected output
    -1