/*
* File: pku3159.cpp
* Author: chenjiang
*终于明白了图的这种表示法,head[i]后面连接的是以i为开始点的边
* 当图的顶点很多时用这种方法比较好
* Created on 2010年4月22日, 下午8:37
*/
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stack>
using namespace std;
#define maxn 30005
#define maxe 150005
int n, m, ind;
int head[maxn];
struct Edge {
int v;
int w;
int next;
} edge[maxe];
int dist[maxn];
bool s[maxn];
void spfa() {
memset(s, 0, sizeof (s));
stack<int> S;
S.push(1);
memset(dist, 127, sizeof (dist));
dist[1] = 0;
s[1] = 1;
while (!S.empty()) {
int cnt = S.top();
S.pop();
s[cnt] = 0;
int ptr = head[cnt]; //ptr是cnt的下一条边
while (1) {
int tmp = edge[ptr].v;
if (dist[tmp] > dist[cnt] + edge[ptr].w) {
dist[tmp] = dist[cnt] + edge[ptr].w;
if (!s[tmp]) {
S.push(tmp);
s[tmp] = 1;
}
}
if (edge[ptr].next == -1)break;
ptr = edge[ptr].next;
}
}
printf("%d/n", dist[n]);
}
/*
*
*/
int main(int argc, char** argv) {
int i, j, k;
//freopen("a.a", "r", stdin);
while (scanf("%d%d", &n, &m) != EOF) {
memset(head, -1, sizeof (head));
ind = 0;
for (i = 1; i <= m; i++) {
int a, b, w;
scanf("%d%d%d", &a, &b, &w);
ind++;
edge[ind].v = b;
edge[ind].w = w;
edge[ind].next = head[a];
head[a] = ind;
}
spfa();
}
return (EXIT_SUCCESS);
}