#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <sstream>
#include <climits>
using namespace std;
//1447 dijkstra 二维矩阵
int n, m;
int map[1010][1010];
bool vi[1010];
int dis[1010];
const int INF = 2100000000;
void init()
{
for (int i = 1; i <= n; ++ i)
{
vi[i] = false;
dis[i] = INF;
for (int j = 1; j <= n; ++ j)
{
map[i][j] = INF;
}
}
for (int i = 0; i < m; ++ i)
{
int from, to, d;
scanf("%d%d%d", &from, &to, &d);
map[to][from] = map[from][to] = d;
}
}
void dijkstra()
{
dis[1] = 0;
for (int k = 0; k < n; ++ k)
{
int index = -1;
int minn = INF;
for (int i = 1; i <= n; ++ i)
{
if (vi[i]==false && dis[i]<minn)
{
index = i;
minn = dis[i];
}
}
vi[index] = true;
for (int j = 1; j <= n; ++ j)
dis[j]=min(dis[j],dis[index]+map[index][j]);
}
}
int main()
{
// freopen("input.txt","r",stdin);
while (cin >> n >> m, n || m)
{
init();
dijkstra();
cout << dis[n] << endl;
}
return 0;
}
/**************************************************************
Problem: 1447
User: cust123
Language: C++
Result: Accepted
Time:10 ms
Memory:5508 kb
****************************************************************/
#include "iostream"
#include "stdio.h"
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <string.h>
#include <stack>
#include <ctype.h>
#include <functional>
#include <math.h>
using namespace std;
//1447
const int INF=0x7f7f7f7f7f;
int n,m,a,b,c;
struct E{
int next;
int c;
};
vector<E> e[101];
bool mark[101];
int dis[101];
int main(){
//freopen("input.txt","r",stdin);
while(cin>>n>>m){
if(n==0&&m==0) break;
for(int i=1;i<=n;i++) e[i].clear();
while(m--){
cin>>a>>b>>c;
E t;
t.c=c;
t.next=b;
e[a].push_back(t);
t.next=a;
e[b].push_back(t);
}
for(int i=1;i<=n;i++){
dis[i]=INF;
mark[i]=false;
}
dis[1]=0;
mark[1]=true;
int np=1;
for(int i=1;i<n;i++){
for(int j=0;j<e[np].size();j++){
int t=e[np][j].next;
int c=e[np][j].c;
if(mark[t]==true) continue;
if(dis[t]==INF || dis[t]>dis[np]+c)
dis[t]=dis[np]+c;
}
int mi=INF;
for(int j=1;j<=n;j++){
if(mark[j]==true) continue;
if(dis[j]==INF) continue;
if(dis[j]<mi){
mi=dis[j];
np=j;
}
}
mark[np]=true;
}
cout<<dis[n]<<endl;
}
return 0;
}
/**************************************************************
Problem: 1447
User: cust123
Language: C++
Result: Accepted
Time:30 ms
Memory:1520 kb
****************************************************************/