#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
int main(){
ios::sync_with_stdio(0);
int n,m,s,e;
bool can[40][40];
string M[40][40],str;
string loop[40];
while(true){
cin >> n >> m >> s >> e;
if(n == 0) break;
for(int i = 0;i < n;++i)
loop[i] = "zzzzzzz";
memset(can,0,sizeof can);
for(int i = 0,u,v;i < m;++i){
cin >> u >> v >> str;
if(u == v) loop[u] = min(loop[u],str);
else{
if(!can[u][v]) M[u][v] = str;
else M[u][v] = min(M[u][v],str);
can[u][v] = true;
}
}
for(int k = 0;k < n;++k)
for(int i = 0;i < n;++i)
for(int j = 0;j < n;++j)
if(can[i][k] && can[k][j]){
if(!can[i][j]) M[i][j] = M[i][k] + M[k][j];
else M[i][j] = min(M[i][j],M[i][k] + M[k][j]);
can[i][j] = true;
}
for(int i = 0;i < n;++i)
for(int j = 0;j < n;++j)
if(j != i && can[i][j] && can[j][i])
loop[i] = min(loop[i],M[i][j] + M[j][i]);
if(!can[s][e]) cout << "NO\n";
else{
bool ok = true;
if(loop[s] + M[s][e] < M[s][e]) ok = false;
for(int i = 0;i < n;++i){
if(i != s && i != e && can[s][i] && can[i][e] && M[s][i] + loop[i] + M[i][e] < M[s][e])
ok = false;
}
if(!ok) cout << "NO\n";
else cout << M[s][e] << '\n';
}
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHN0cmluZz4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICAKICAgIGludCBuLG0scyxlOwogICAgYm9vbCBjYW5bNDBdWzQwXTsKICAgIHN0cmluZyBNWzQwXVs0MF0sc3RyOwogICAgc3RyaW5nIGxvb3BbNDBdOwogICAgCiAgICB3aGlsZSh0cnVlKXsKICAgICAgICBjaW4gPj4gbiA+PiBtID4+IHMgPj4gZTsKICAgICAgICAKICAgICAgICBpZihuID09IDApIGJyZWFrOwogICAgICAgIAogICAgICAgIGZvcihpbnQgaSA9IDA7aSA8IG47KytpKQogICAgICAgICAgICBsb29wW2ldID0gInp6enp6enoiOwogICAgICAgIAogICAgICAgIG1lbXNldChjYW4sMCxzaXplb2YgY2FuKTsKICAgICAgICAKICAgICAgICBmb3IoaW50IGkgPSAwLHUsdjtpIDwgbTsrK2kpewogICAgICAgICAgICBjaW4gPj4gdSA+PiB2ID4+IHN0cjsKICAgICAgICAgICAgCiAgICAgICAgICAgIGlmKHUgPT0gdikgbG9vcFt1XSA9IG1pbihsb29wW3VdLHN0cik7CiAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICBpZighY2FuW3VdW3ZdKSBNW3VdW3ZdID0gc3RyOwogICAgICAgICAgICAgICAgZWxzZSBNW3VdW3ZdID0gbWluKE1bdV1bdl0sc3RyKTsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgY2FuW3VdW3ZdID0gdHJ1ZTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAKICAgICAgICBmb3IoaW50IGsgPSAwO2sgPCBuOysraykKICAgICAgICAgICAgZm9yKGludCBpID0gMDtpIDwgbjsrK2kpCiAgICAgICAgICAgICAgICBmb3IoaW50IGogPSAwO2ogPCBuOysraikKICAgICAgICAgICAgICAgICAgICBpZihjYW5baV1ba10gJiYgY2FuW2tdW2pdKXsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoIWNhbltpXVtqXSkgTVtpXVtqXSA9IE1baV1ba10gKyBNW2tdW2pdOwogICAgICAgICAgICAgICAgICAgICAgICBlbHNlIE1baV1bal0gPSBtaW4oTVtpXVtqXSxNW2ldW2tdICsgTVtrXVtqXSk7CiAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICBjYW5baV1bal0gPSB0cnVlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAKICAgICAgICBmb3IoaW50IGkgPSAwO2kgPCBuOysraSkKICAgICAgICAgICAgZm9yKGludCBqID0gMDtqIDwgbjsrK2opCiAgICAgICAgICAgICAgICBpZihqICE9IGkgJiYgY2FuW2ldW2pdICYmIGNhbltqXVtpXSkKICAgICAgICAgICAgICAgICAgICBsb29wW2ldID0gbWluKGxvb3BbaV0sTVtpXVtqXSArIE1bal1baV0pOwogICAgICAgIAogICAgICAgIGlmKCFjYW5bc11bZV0pIGNvdXQgPDwgIk5PXG4iOwogICAgICAgIGVsc2V7CiAgICAgICAgICAgIGJvb2wgb2sgPSB0cnVlOwogICAgICAgICAgICAKICAgICAgICAgICAgaWYobG9vcFtzXSArIE1bc11bZV0gPCBNW3NdW2VdKSBvayA9IGZhbHNlOwogICAgICAgICAgICAKICAgICAgICAgICAgZm9yKGludCBpID0gMDtpIDwgbjsrK2kpewogICAgICAgICAgICAgICAgaWYoaSAhPSBzICYmIGkgIT0gZSAmJiBjYW5bc11baV0gJiYgY2FuW2ldW2VdICYmIE1bc11baV0gKyBsb29wW2ldICsgTVtpXVtlXSA8IE1bc11bZV0pCiAgICAgICAgICAgICAgICAgICAgb2sgPSBmYWxzZTsKICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgaWYoIW9rKSBjb3V0IDw8ICJOT1xuIjsKICAgICAgICAgICAgZWxzZSBjb3V0IDw8IE1bc11bZV0gPDwgJ1xuJzsKICAgICAgICB9CiAgICB9CiAgICAKICAgIHJldHVybiAwOwp9Cg==
NCA3IDAgMgowIDEgYWJyYQowIDEgb2lsCjIgMCBrZXQKMSAzIGNhZGEKMyAzIGRhCjMgMiBicmEKMiAzIGtldAoyIDIgMCAxCjAgMCBhCjAgMSBiCjUgNiAzIDAKMyAxIG9wCjMgMiBvcAozIDQgb3BxCjEgMCBzdAoyIDAgcXIKNCAwIHIKMiAxIDAgMQoxIDEgbG9vb29wCjMgMyAwIDIKMCAxIGEKMCAxIGFiCjEgMiBjCjAgMCAwIDA=
4 7 0 2
0 1 abra
0 1 oil
2 0 ket
1 3 cada
3 3 da
3 2 bra
2 3 ket
2 2 0 1
0 0 a
0 1 b
5 6 3 0
3 1 op
3 2 op
3 4 opq
1 0 st
2 0 qr
4 0 r
2 1 0 1
1 1 loooop
3 3 0 2
0 1 a
0 1 ab
1 2 c
0 0 0 0