#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
    x += k;
    x %= mod;
    if(x < 0) x += mod;
}
void del(int &x, int k)
{
    x -= k;
    x %= mod;
    if(x < 0) x += mod;
}
int n,m,d[2][maxn];
array<int,3> edge[maxn];
vii ke[maxn],adj[maxn];
int p[maxn];
void dij(int inp,int d[],int n)
{
    fo(i,1,n) d[i] = inf;
    d[inp] = 0;
    priority_queue<pii,vii,greater<pii>> q;
    q.push({0,inp});
    while(q.size())
    {
        auto [du,u] = q.top();
        q.pop();
        if(d[u] != du) continue;
        for(auto [v,w] : ke[u])
        {
            if(d[v] > d[u] + w)
            {
                d[v] = d[u] + w;
                q.push({d[v],v});
            }
        }
    }
}
int low[maxn],num[maxn],cnt;
//vi bridge;
int scc,tp[maxn];
stack<int> st;
vii adjj[maxn];
void dfs(int u,int par = 0)
{
    st.push(u);
    low[u] = num[u] = ++cnt;
    for(auto [v,idx] : adj[u]) if(v != par)
        {
            if(!num[v])
            {
                dfs(v,u);
                minimize(low[u],low[v]);
//                if(low[v] == num[v]) bridge.pb(idx);
            }
            else minimize(low[u],num[v]);
        }
    if(low[u] == num[u])
    {
        int v = 0;
        ++scc;
        while(v != u)
        {
            v = st.top();
            st.pop();
            low[v] = num[v] = n + 1;
            tp[v] = scc;
        }
    }
}
int pp[maxn];
void dfs1(int u,int par = 0)
{
    for(auto [v,w] : adjj[u]) if(v != par)
        {
            pp[v] = w;
            dfs1(v,u);
        }
}
bool check(int val)
{
    cnt = 0;
//    bridge.clear();
    fo(i,1,n)
    {
        adjj[i].clear();
        adj[i].clear(),low[i] = num[i] = 0;
    }
    scc = cnt = 0;
    fo(i,1,m)
    {
        auto [u,v,w] = edge[i];
        if(min(d[0][u] + w + d[1][v],d[1][u] + w + d[0][v]) < val)
        {
            adj[u].pb(v,i);
            adj[v].pb(u,i);
//            cout << u << ' ' << v << ' ' << w;
//            en;
        }
    }
    dfs(1);
    fo(u,1,n)
    {
        for(auto [v,idx] : adj[u]) if(tp[u] != tp[v])
            {
                adjj[tp[u]].pb(tp[v],idx);
            }
    }
//    fo(i,1,n) cout << low[i] << ' ' << num[i] << "\n";
//    fo(i,1,n) cout << tp[i] << ' ';en;
    dfs1(tp[1]);
    int u = tp[n];
//    cout << tp[1] << ' ' << tp[n];en;
    while(u != tp[1])
    {
        int idx = pp[u];
        if(idx == 0) break;
        auto [x,y,w] = edge[idx];
        if(min(d[1][x] + d[0][y] + w,d[0][x] + d[1][y] + w) + p[idx] >= val) return 1;
        u = tp[x] ^ tp[y] ^ u;
    }
    return 0;
}
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m;
    fo(i,1,m)
    {
        int u,v,w;
        cin >> u >> v >> w;
        ke[u].pb(v,w);
        ke[v].pb(u,w);
        edge[i] = {u,v,w};
    }
    fod(i,m,1) p[i] = max(p[i + 1],edge[i + 1][2]);
    dij(1,d[0],n);
    dij(n,d[1],n);
    int l = d[0][n],r = d[0][n] + p[1] + 5,ans = l;
//    cout << check(7);
//    en;
    while(l <= r)
    {
        int mid = l + r >> 1;
        if(check(mid)) ans = mid,l = mid + 1;
        else r = mid - 1;
    }
    cout << ans;
}
Compilation message (stderr)
Aesthetic.cpp:168:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  168 | main()
      | ^~~~
Aesthetic.cpp: In function 'int main()':
Aesthetic.cpp:173:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  173 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Aesthetic.cpp:174:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  174 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |