/*
	?§£????????????:The Most Powerful Spell
	(http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1169&lang=jp)

	Status:

	?????£????????¨:
        

*/
var log = console.log;
if (typeof process != "undefined") {
    var input = "";
    process.stdin.resume();
    process.stdin.setEncoding('utf8');
    process.stdin.on('data', function(chunk) {
        input += chunk;
    });
    process.stdin.on('end', function() {
        var lines = input.split("\n");
        main(lines);
    });
}


function main(lines) {
    lines = lines.map(function(line) {
        return line.split(" ").filter(function(e) {
            return e != "";
        }).map(function(e) {
            var n = Number(e);
            if (n.toString() == "NaN") {
                return e.replace(/(\r)|(\n)/g, "");
            }
            return n;
        });
    });
    //read until last line
    while (lines[0].toString() != "0,0,0,0") {
        var tl = lines.shift();
        var n = tl[0];
        var a = tl[1];
        var s = tl[2];
        var g = tl[3];
        //????????£??????????????¨??????????????¨??????????????§??£??\???????????????????????????????????????????????????
        var set = lines.slice(0, a);
        lines = lines.slice(a);
        var graph = [];
        for (var i = 0; i < n; i++) {
            graph[i] = [];
            for (var j = 0; j < n; j++) {
                graph[i][j] = "|";
            }
        }
        set.forEach(function(edge) {
            var start = edge[0];
            var end = edge[1];
            var label = edge[2];
            var old_label = graph[start][end];
            if (label < old_label) {
                graph[start][end] = label;
            }
            if (start == g) {
                graph[start][end] = "|";
            }
        });
        var graph2 = [];
        for (var i = 0; i < n; i++) {
            graph2[i] = [];
            for (var j = 0; j < n; j++) {
                graph2[i][j] = graph[i][j];
            }
        }
        dataset(n, a, s, g, graph, graph2);
    }
}

function dataset(n, a, s, g, graph, graph2) {
    //log(graph);
    //log("----");
    //??????????????????????????????
    //??????????????????????????????????????¨??§????????????????????????????????¨???????????°???????????????
    var prev = graph2;
    var target = graph;
    do {
        var k = prev;
        prev = target;
        target = k;
        prev.forEach(function(e, i) {
            e.forEach(function(f, j) {
                target[i][j] = f;
            });
        });
        //prev??§????´¢?????????target?????????????????§??????
        var visited = [];
        for (var i = 0; i < n; i++) {
            visited[i] = 0;
        }
        var now = s;
        var stack = [s];
        while (stack.length > 0) {
            now = stack.pop();
            stack.push(now);
            visited[now] = 1;
            //log("now at " + now);
            if (now == g) {
                //??´??????????????????
                //?????????
                break;
            }
            var next_c = prev[now];
            var min_idx = -1;
            var min_label = "|";
            for (var i = 0; i < n; i++) {
                if (next_c[i] < min_label) {
                    min_idx = i;
                    min_label = next_c[i];
                }
            }
            //min_idx???????¬????????????????????????????????????????
            if (min_idx == -1) {
                //?????¨?????????
                //??????????????¬??????????????¢??????
                //???????????\??£???????????£????????¨?¶????
                for (var i = 0; i < n; i++) {
                    target[i][now] = "|";
                }
                stack.pop();
            } else {
                //min_idx????????¨??????
                if (visited[min_idx]) {
                    //?????§????????£????????¨?????????
                    //??????????????????
                    var idx = stack.indexOf(min_idx);
                    var dups = stack.slice(idx);
                    //log(min_idx);
                    //?????\?????????????????????????¶????
                    dups.forEach(function(loop) {
                        for (var i = 0; i < n; i++) {
                            target[i][loop] = "|";
                            target[loop][i] = "|";
                        }
                    });
                    break;
                } else {
                    //?????£????????¨?????????
                    stack.push(min_idx);
                }
            }
        }
        //log("target->");
        //log(target);
        //log("prev->");
        //log(prev);
        //log("breaked");
    } while (prev.toString() != target.toString());
    //?????±?????§????¶????
    //??????????????§?????£???????????±?????§?????????
    var graph = target;
    for (var i = 0; i < n; i++) {
        graph[i][i] = "|";
    }
    //????????¢????????¨??????????????????????????§???????????????????????????????????¨??§??????
    //log(graph);
    var decided = [];
    var d = [];
    for (var i = 0; i < n; i++) {
        decided[i] = false;
        d[i] = "|";
    }
    d[s] = "";
    while (decided.filter(function(e) { return e; }).length != n) {
        var min_idx = -1;
        var min_label = "|";
        d.forEach(function(label, index) {
            if (label <= min_label && !decided[index]) {
                min_idx = index;
                min_label = label;
            }
        });
        var u_idx = min_idx;
        decided[u_idx] = true;
        for (var i = 0; i < n; i++) {
            //u???????£??????§???????????£??????????°?????????´??°
            if (graph[u_idx][i] < "|") {
                //u_idx??????i?????§?????????
                if (d[i] > d[u_idx] + graph[u_idx][i]) {
                    d[i] = d[u_idx] + graph[u_idx][i];
                }
            }
        }
    }
    if (d[g] == "|") {
        d[g] = "NO";
    }
    log(d[g]);
}