#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
struct edge
{
int to,val;
};
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q;
vector<edge>e[2505];
int dis[2505];
int vis[2505];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
edge tmp;
tmp.to=y;
tmp.val=z;
e[x].push_back(tmp);
tmp.to=x;
tmp.val=z;
e[y].push_back(tmp);
}
for(int i=1;i<=n;i++)
{
dis[i]=2147483647;
}
dis[1]=0;
q.push(make_pair(0,1));
while(!q.empty())
{
int x=q.top().second;
q.pop();
if(vis[x]==1)
continue;
vis[x]=1;
for(int i=0;i<e[x].size();i++)
{
int y=e[x][i].to;
if(dis[x]+e[x][i].val<dis[y])
{
dis[y]=dis[x]+e[x][i].val;
q.push(make_pair(dis[y],y));
}
}
}
printf("%d\n",dis[n]);
return 0;
}