#include <iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int inf=0x3f3f3f3f;
const int Max=1e4+10;
typedef struct P
{
int from,cost;//from为父亲节点
friend operator <(P a,P b){return a.cost<b.cost;}//重载一个判断,使优先队列先取出价值小的//
}P;
typedef struct edge
{
int to,cost;//to为子节点
}edge;
vector<edge> G[Max];//记录节点之间的关系
int d[105];
int n,m;
void dijk()
{
fill(d+1,d+n+1,inf);
priority_queue<P> que;
d[1]=0;
P p;
p.from=1;
p.cost=0;
que.push(p);
while(!que.empty()){
p=que.top();
que.pop();
int v=p.from;
for(int i=0;i<G[v].size();i++){
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
p.from=e.to;
p.cost=d[e.to];
que.push(p);
}
}
}
}
int main()
{
int u,v,w;
edge e;
while(~scanf("%d%d",&n,&m)&&(n||m)){
for(int i=0;i<1010;++i) G[i].clear();
memset(d,0,sizeof d);
for(int i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
e.to=v,e.cost=w;
G[u].push_back(e);
e.to=u;
G[v].push_back(e);
}
dijk();
if(d[n]==inf)
printf("-10086\n");
else
printf("%d\n",d[n]);
}
return 0;
}