#include <stdio.h>
#include <stdlib.h>
#define INF 0x3f3f3f3f
int n, m, a[110][110];
int vis[110], dist[110];
void dijkstra(int v)
{
int i, j, k;
for(i = 1; i <= n; i++)
{
dist[i] = a[v][i]; //dist数组的初始化
vis[i] = 0;
}
dist[v] = 0;
vis[i] = 1;
for(i = 0; i <n-1; i++)
{
int min = INF, u = v;
for(j = 1; j <= n; j++) //寻找未标记结点的最小值
{
if(!vis[j] && dist[j] < min)
{
u = j;
min = dist[j];
}
}
vis[u] = 1;
for(k = 1; k <= n;k++) //更新最短路
{
if(!vis[k] && a[u][k]< INF &&dist[k] >dist[u]+a[u][k])
{
dist[k] = dist[u]+ a[u][k];
}
}
}
}
int main()
{
int i, j, x, y, w;
while(scanf("%d%d", &n,&m) != EOF)
{
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
if(i == j)
a[i][j] = 0;
else
a[i][j] = INF;
}
}
for(i = 0; i < m; i++)
{
scanf("%d%d%d", &x, &y, &w);
if(w < a[x][y]) //避免覆盖最短路
a[x][y] = a[y][x] = w;
}
if(m == 0) printf("0\n");
else
{
dijkstra(1);
printf("%d\n", dist[n]);
}
}
return 0;
}