program preprava;
type Tradek=array[1..1] of  record
                                cislo:longint;
                                delka:byte;
                              end;
    PRadek=^TRadek;

var {cesty:array[1..10000,1..1000] of record
                                        cislo:longint;
                                        delka:byte;
                                     end;
    }
    cesty:array[1..10000] of PRadek;
    pomradek:PRadek;
    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;

  pp:Pointer;
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; 
       cesty[i]:=PRadek(GetMem(10*100));
    end;
  for i:=1 to m do
  begin
    readln(x,y,v);
    Inc(pocetcest[x]); Inc(pocetcest[y]);

    if (pocetcest[x] mod 100)=0 then
    begin
      pomradek:=PRadek(GetMem(10*(pocetcest[x]+100)));
      for j:=1 to pocetcest[x]-1 do
        pomradek^[j]:=cesty[x]^[j];
      release(cesty[x]);
      cesty[x]:=pomradek;  
    end;


 if (pocetcest[y] mod 100)=0 then
    begin
      pomradek:=PRadek(GetMem(10*(pocetcest[y]+100)));
      for j:=1 to pocetcest[y]-1 do
        pomradek^[j]:=cesty[y]^[j];
      release(cesty[y]);
      cesty[y]:=pomradek;  
    end;




    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.
