#include<stdio.h>
#include<queue>
#include<iostream>
#include<vector>
using namespace std;
#define inf 1000000000
#define N 31000
#define NN 151000
struct node {
int u,v,w,next;
}bian[NN];
int n,m,yong,head[N];
void creat(int u,int v,int w) {
bian[yong].u=u;
bian[yong].v=v;
bian[yong].w=w;
bian[yong].next=head[u];
head[u]=yong++;
}
int dijkstra(int u,int v) {
int dis[N],visit[N],i,j;
for(i=1;i<n;i++)
dis[i]=inf;
dis[0]=0;
memset(visit,0,sizeof(visit));
typedef pair<int,int>p;
priority_queue<p,vector<p>,greater<p> >q;
q.push(make_pair(0,0));
while(!q.empty()) {
p cur=q.top();
q.pop();
j=cur.second;
if(visit[j])continue;
visit[j]=1;
for(i=head[j];i!=-1;i=bian[i].next)
if(dis[bian[i].v]>dis[j]+bian[i].w) {
dis[bian[i].v]=dis[j]+bian[i].w;
q.push(make_pair(dis[bian[i].v],bian[i].v));
}
}
return dis[v];
}
int main() {
int k,a,b;
while(scanf("%d%d",&n,&m)!=EOF) {
yong=0;
memset(head,-1,sizeof(head));
while(m--) {
scanf("%d%d%d",&a,&b,&k);
a--;b--;
creat(a,b,k);
}
printf("%d
",dijkstra(0,n-1));
}
return 0;
}