#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <list>
#include <stack>
#include <queue>
#define MAX 10000000
using namespace std;
/*
T2 hdu 1874
*/
struct Edge
{
int u,v;
int cost;
};
Edge edge[1005*2];
int dis[205];//标记路径 点
int N,M;// 1起点 ,N 终点,M 边
int st,en;
int cnt;
void Bellman_Ford()
{
int i,j;
for(i=0; i<N ;i++)
dis[i] = MAX;
dis[st]=0;
for(i=0; i<N-1 ;i++)
for(j=0;j< cnt;j++)
if(dis[edge[j].u]!=MAX && dis[edge[j].v] > dis[edge[j].u] + edge[j].cost)
{
dis[edge[j].v] = dis[edge[j].u] + edge[j].cost;
}
}
int main()
{
int i;
while(cin>>N>>M)
{
int a,b,c;
cnt=0;
for(i=0;i<M;i++)
{
cin>>a>>b>>c;
edge[cnt].u=a;
edge[cnt].v=b;
edge[cnt].cost=c;
cnt++;
edge[cnt].u=b;
edge[cnt].v=a;
edge[cnt].cost=c;
cnt++;
}
cin>>st>>en;
Bellman_Ford();
if(dis[en] == MAX)
cout<<"-1"<<endl;
else cout<<dis[en]<<endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <list>
#include <stack>
#include <queue>
#define MAX 10000000
using namespace std;
/*
T1*/
struct Edge
{
int u,v;
int cost;
};
Edge edge[10005*2];
int dis[105];//标记路径 点
int N,M;// 1起点 ,N 终点,M 边
int st,en;
void Bellman_Ford()
{
int i,j;
for(i=1; i<=N ;i++)
dis[i] = ( i == 1 ? 0: MAX );
for(i=1; i<=N-1 ;i++)
for(j=1;j<= M*2;j++)
if(dis[edge[j].v] > dis[edge[j].u] + edge[j].cost) //松弛(顺序一定不能反~)
{
dis[edge[j].v] = dis[edge[j].u] + edge[j].cost;
}
}
int main()
{
int i,j;
while(cin>>N>>M ,N+M)
{
int a,b,c;j=0;
for(i=1;i<=M;i++)
{
cin>>a>>b>>c;
j++;
edge[j].u=a;
edge[j].v=b;
edge[j].cost=c;
j++;
edge[j].u=b;
edge[j].v=a;
edge[j].cost=c;
}
cin>>st>>en;
Bellman_Ford();
cout<<dis[N]<<endl;
}
return 0;
}