package com.thinker.bishi.other;


import java.util.*;

/**
 * @author lzh
 */
public class Main {
    private static int last = 0;
    private static List<Integer> tmp = new ArrayList<Integer>();
    private static List<Integer> result ;
    private static Map<Integer,int[]> tmps = new HashMap<>();
    private static int check(int sum,int[] lines,int[] init){
        boolean flag = lines[1] <= init[1] && lines[0] == last;
        if(flag){
            sum += lines[2];
            last = lines[1];
            tmp.add(last);
        }
        return sum;
    }
    public static void main(String[] args) {
        Scanner in  =new Scanner(System.in);
        String[] first = in.nextLine().split(" ");
        int[] init = new int[2];
        int sum = 0;
        int index = 1;
        for(int i=0;i<2;i++){
            init[i] = Integer.parseInt(first[i]);
        }
        last = init[0];
        tmp.add(last);
        while (in.hasNextLine()){
            String[] lines = in.nextLine().split(" ");
            int[] linesInt = new int[3];
            for(int i=0;i<3;i++){
                linesInt[i] = Integer.parseInt(lines[i]);
            }
            tmps.put(index,linesInt);
            index += 1;
        }
        int min = 0;
        for(int i=0;i<index;i++){
            int[] ints = new int[index];
            for(int k=0;k<index;k++){ ints[k] = 0;}
            for(int j=0;j<index;j++){
                int random = (int)(Math.random()*(10-1));
                while (ints[random] == 1){random = (int)(Math.random()*(10-1));}
                sum = check(sum,tmps.get(random),init);
            }
            if(i == 0) min = sum;
            else if(sum < min){
                result = tmp;
            }
        }
        for(int i=0;i<result.size();i++){
            if(i == result.size()-1){
                System.out.print(result.get(i)+"("+min+")");
            }else {
                System.out.print(result.get(i)+"->");
            }
        }


    }
}
