#include <iostream> #include <cstring> #include <cstdio> #define INF 0x3f3f3f using namespace std;
int map[1005][1005];
int n,m,s,t;
void Floyd () {int i,j,k;
cin>>s>>t;
for (k=0;k<=n-1;k++)//floyd core step for (i=0;i<=n-1;i++) {if (Map[i][k]<inf) {for (j=0;j<=n-1;j++) {
if (Map[k][j]<inf&&map[i][j]>map[i][k]+map[k][j]) map[i][j]=map[i][k]+map[k][j];
}}} if (Map[s][t]<inf) cout<<map[s][t]<<endl;
Else cout<< "-1" <<endl;
return;
} int main () {int i,j,u,v,w;
while (Cin>>n>>m) {for (i=0;i<=n-1;i++)//Initialize map[][] array for (j=0;j<=n-1;j++) Map[i][j]=inf;
for (i=0;i<=n-1;i++)//Note Don't take this step map[i][i]=0; /* Explanation: such as k=1,i=2,j=2, Map[2][1]+map[1][2]<map[2][2] will be updated map[2][2], the result is obviously not 0, and under normal circumstances, map[2][2] should be 0 so there is no less for (i
=0;i<=n-1;i++) map[i][i]=0; This step */for (i=1;i<=m;i++)//input data {Cin>>u>>v>>w;
if (map[u][v]>w)//Prevent the occurrence of multiple paths between two points in case of the occurrence of {map[u][v]=w;
Map[v][u]=w;
}} Floyd ();
/*cin>>s>>t;
for (k=0;k<=n-1;k++)//floyd core step for (i=0;i<=n-1;i++) {if (Map[i][k]<inf) {for (j=0;j<=n-1;j++) {
if (Map[k][j]<inf&&map[i][j]>map[i][k]+map[k][j]) map[i][j]=map[i][k]+map[k][j];
}}}*//*for (i=0;i<=n-1;i++) {for (j=0;j<=n-1;j++) cout<<map[i][j]<< "";
cout<<endl;
}*//*if (Map[s][t]<inf) cout<<map[s][t]<<endl;
Else cout<< "-1" <<endl;*/} return 0; }