//thanh huyenn ne//
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m, a1, a2, a3, ans = -1;
vector<pair<int,int>> p[100001];
vector<pair<int,int>> ytb[100001];
map<pair<int,int>,int> mp;
void nhap() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
if (c > ans) {
ans = c;
a1 = a;
a2 = b;
a3 = c;
}
p[a].push_back({b, c});
ytb[a].push_back({b, c});
mp[{a, b}] = c;
}
ytb[a1].push_back({a2, a3/2});
}
int bfs(int s) {
vector<int> d(n + 1, LLONG_MAX);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
q.push({0, s});
d[s] = 0;
while (!q.empty()) {
int x = q.top().second;
int dist_x = q.top().first;
q.pop();
if (dist_x > d[x]) continue;
for (pair<int,int> v : ytb[x]) {
int cc = v.first;
int ll = v.second;
if (d[cc] > d[x] + ll) {
d[cc] = d[x] + ll;
q.push({d[cc], cc});
}
}
}
return d[n];
}
int dijkstra(int s) {
vector<int> d(n + 1, LLONG_MAX);
vector<int> trace(n + 1, -1);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
q.push({0, s});
d[s] = 0;
while (!q.empty()) {
int x = q.top().second;
int dist_x = q.top().first;
q.pop();
if (dist_x > d[x]) continue;
for (pair<int,int> v : p[x]) {
int cc = v.first;
int ll = v.second;
if (d[cc] > d[x] + ll) {
d[cc] = d[x] + ll;
trace[cc] = x;
q.push({d[cc], cc});
}
}
}
vector<int> path;
int curr = n;
while (curr != -1) {
path.push_back(curr);
curr = trace[curr];
}
int res = -1;
reverse(path.begin(), path.end());
for (int i = 0; i < path.size() - 1; i++) {
int weight = mp[{path[i], path[i+1]}];
res = max(res, weight);
}
return d[n] - res + res / 2;
}
signed main() {
nhap();
cout << min(dijkstra(1), bfs(1));
return 0;
}
Ly90aGFuaCBodXllbm4gbmUvLwojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgojZGVmaW5l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nRpbnVlOwogICAgICAgIGZvciAocGFpcjxpbnQsaW50PiB2IDogeXRiW3hdKSB7CiAgICAgICAgICAgIGludCBjYyA9IHYuZmlyc3Q7CiAgICAgICAgICAgIGludCBsbCA9IHYuc2Vjb25kOwogICAgICAgICAgICBpZiAoZFtjY10gPiBkW3hdICsgbGwpIHsKICAgICAgICAgICAgICAgIGRbY2NdID0gZFt4XSArIGxsOwogICAgICAgICAgICAgICAgcS5wdXNoKHtkW2NjXSwgY2N9KTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBkW25dOwp9CgppbnQgZGlqa3N0cmEoaW50IHMpIHsKICAgIHZlY3RvcjxpbnQ+IGQobiArIDEsIExMT05HX01BWCk7CiAgICB2ZWN0b3I8aW50PiB0cmFjZShuICsgMSwgLTEpOwogICAgcHJpb3JpdHlfcXVldWU8cGFpcjxpbnQsaW50Pix2ZWN0b3I8cGFpcjxpbnQsaW50Pj4sZ3JlYXRlcjxwYWlyPGludCxpbnQ+Pj4gcTsKICAgIHEucHVzaCh7MCwgc30pOwogICAgZFtzXSA9IDA7CiAgICB3aGlsZSAoIXEuZW1wdHkoKSkgewogICAgICAgIGludCB4ID0gcS50b3AoKS5zZWNvbmQ7CiAgICAgICAgaW50IGRpc3RfeCA9IHEudG9wKCkuZmlyc3Q7CiAgICAgICAgcS5wb3AoKTsKICAgICAgICBpZiAoZGlzdF94ID4gZFt4XSkgY29udGludWU7CiAgICAgICAgZm9yIChwYWlyPGludCxpbnQ+IHYgOiBwW3hdKSB7CiAgICAgICAgICAgIGludCBjYyA9IHYuZmlyc3Q7CiAgICAgICAgICAgIGludCBsbCA9IHYuc2Vjb25kOwogICAgICAgICAgICBpZiAoZFtjY10gPiBkW3hdICsgbGwpIHsKICAgICAgICAgICAgICAgIGRbY2NdID0gZFt4XSArIGxsOwogICAgICAgICAgICAgICAgdHJhY2VbY2NdID0geDsKICAgICAgICAgICAgICAgIHEucHVzaCh7ZFtjY10sIGNjfSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICB2ZWN0b3I8aW50PiBwYXRoOwogICAgaW50IGN1cnIgPSBuOwogICAgd2hpbGUgKGN1cnIgIT0gLTEpIHsKICAgICAgICBwYXRoLnB1c2hfYmFjayhjdXJyKTsKICAgICAgICBjdXJyID0gdHJhY2VbY3Vycl07CiAgICB9CiAgICBpbnQgcmVzID0gLTE7CiAgICByZXZlcnNlKHBhdGguYmVnaW4oKSwgcGF0aC5lbmQoKSk7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IHBhdGguc2l6ZSgpIC0gMTsgaSsrKSB7CiAgICAgICAgaW50IHdlaWdodCA9IG1wW3twYXRoW2ldLCBwYXRoW2krMV19XTsKICAgICAgICByZXMgPSBtYXgocmVzLCB3ZWlnaHQpOwogICAgfQogICAgcmV0dXJuIGRbbl0gLSByZXMgKyByZXMgLyAyOwp9CgpzaWduZWQgbWFpbigpIHsKICAgIG5oYXAoKTsKICAgIGNvdXQgPDwgbWluKGRpamtzdHJhKDEpLCBiZnMoMSkpOwogICAgcmV0dXJuIDA7Cn0K