1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #define inf 1000000000
12 #define maxn 5000+100
13 #define maxm 100000+100
14 #define ll long long
15 using namespace std;
16 inline ll read()
17 {
18 ll x=0,f=1;char ch=getchar();
19 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
20 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
21 return x*f;
22 }
23 struct edge{int from,go,next,w;}e[2*maxm];
24 int n,m,s,tot,q[maxn],d[2][maxn],head[maxn];
25 bool v[maxn];
26 void ins(int x,int y,int z)
27 {
28 e[++tot].go=y;e[tot].from=x;e[tot].w=z;e[tot].next=head[x];head[x]=tot;
29 }
30 void insert(int x,int y,int z)
31 {
32 ins(x,y,z);ins(y,x,z);
33 }
34 void spfa(int t)
35 {
36 for(int i=1;i<=n;++i) d[t][i]=inf;
37 memset(v,0,sizeof(v));
38 int l=0,r=1,x,y;
39 s=(t==0)?1:n;q[1]=s;d[t][s]=0;
40 while(l!=r)
41 {
42 x=q[++l];if(l==maxn)l=0;v[x]=0;
43 for(int i=head[x];i;i=e[i].next)
44 if(d[t][x]+e[i].w<d[t][y=e[i].go])
45 {
46 d[t][y]=d[t][x]+e[i].w;
47 if(!v[y]){v[y]=1;q[++r]=y;if(r==maxn)r=0;}
48 }
49 }
50 }
51 int main()
52 {
53 freopen("input.txt","r",stdin);
54 freopen("output.txt","w",stdout);
55 n=read();m=read();
56 while(m--)
57 {
58 int x=read(),y=read(),z=read();insert(x,y,z);
59 }
60 spfa(0);spfa(1);
61 int ans=inf;
62 for(int i=1;i<=n;i++)
63 {
64 for(int j=head[i];j;j=e[j].next)
65 {
66 int tmp=d[0][i]+d[1][e[j].go]+e[j].w;
67 if(tmp!=d[0][n])ans=min(ans,tmp);
68 }
69 }
70 printf("%d\n",ans);
71 return 0;
72 }