program preprava;
var cesty:array[1..10000,1..1000] of record
                                        cislo:longint;
                                        delka:byte;
                                     end;
    pocetcest:array[1..10000] of longint;
    delka:array[0..10000] of longint;
    oznaceno:array[1..10000] of boolean;
    kam,max,maxi,m,n,a,b,i,j,x,y,v,pivot:longint;
begin
  readln(n,m,a,b);
while n<>0 do
begin

  max:=256*256*127;
  for i:=1 to 10000 do
    begin pocetcest[i]:=0; delka[i]:=max; oznaceno[i]:=false; end;
  for i:=1 to m do
  begin
    readln(x,y,v);
    Inc(pocetcest[x]); Inc(pocetcest[y]);
    cesty[x,pocetcest[x]].delka:=v;
    cesty[x,pocetcest[x]].cislo:=y;

    cesty[y,pocetcest[y]].delka:=v;
    cesty[y,pocetcest[y]].cislo:=x; 
  end;
 
  pivot:=a; delka[a]:=0;
  


  while pivot<>b do
  begin
   oznaceno[pivot]:=true;
   for i:=1 to pocetcest[pivot] do
   begin
    kam:=cesty[pivot,i].cislo;
    if delka[kam] > delka[pivot]+cesty[pivot,i].delka then
      delka[kam]:= delka[pivot]+cesty[pivot,i].delka;
   end;
   maxi:=0; delka[maxi]:=max;
   for i:=1 to n do
    if (delka[i]<delka[maxi])and (not oznaceno[i]) then maxi:=i;
   pivot:=maxi;
  end;
  
  writeln(delka[pivot]);

 readln(n,m,a,b);
end; 


end.
