//dijkstra
#include<iostream>
#include<cstdio>#include<cstring>
#include<fstream>
#include<cmath>
#include<cstdlib>
using namespace std;
const int MAX=0xfffffff;
int n,m;
int map[220][220];
int dist[220];
int vis[220];
int dijkstra(int s,int t)
{
for (int i=0;i<n;i++)
{
vis[i]=0;
dist[i]=map[s][i];
}
vis[s]=1;
for(int i=0;i<n;i++)
{
int min=MAX;
int k=0;
for (int j=0;j<n;j++)
{
if(!vis[j]&&dist[j]<min)
{
min=dist[j];
k=j;
}
}
vis[k]=1;
for(int j=0;j<n;j++)
{
if(!vis[j]&&dist[j]>dist[k]+map[k][j]&&map[k][j]!=MAX)//map[k][j]!=MAX
dist[j]=dist[k]+map[k][j];
}
}
return dist[t]<MAX?dist[t]:-1;
}
int main ( )
{
freopen("1.txt","r",stdin);
while (cin>>n>>m)
{
//初始化数组
for(int i=0;i<n;i++)
for (int j=0;j<n;j++)
if (i==j) map[i][j]=0;
else map[i][j]=MAX;
//输入已知路径
for(int i=1;i<=m;i++)
{
int x,y,z;
cin>>x>>y>>z;
map[x][y]=map[y][x]=min(map[x][y],z);
}
//查找
int s,t;
cin>>s>>t;
cout<<dijkstra(s,t)<<endl;
}
return 0;
}
//floyd