#include<bits/stdc++.h>
#define ii pair<int,int>
#define st first
#define nd second
using namespace std;

const int X=2e5+5;
long long n, m, x, y, d[X], c;
vector<ii>b[X];
priority_queue<ii,vector<ii>,greater<ii> >pq;

int main(){
    ios_base::sync_with_stdio(false);   cin.tie(0);
    cin>>n>>m;
    while(m--){
        cin>>x>>y>>c;
        b[x].push_back({c,y});
        b[y].push_back({c,x});
    }

    for(int i=1; i<=n; i++) d[i]=1e18;
    d[1]=0;
    pq.push({0,1});
    while(!(pq.empty())){
        ii u=pq.top();
        pq.pop();
        if(d[u.nd]<u.st)    continue;
        for(ii v:b[u.nd]){
            if(d[v.nd]>u.st+v.st){
                d[v.nd]=u.st+v.st;
                pq.push({d[v.nd],v.nd});
            }
        }
    }
    cout<<d[n];
}
