#include <bits/stdc++.h>
using namespace std;
int n, m, u, v, l, ans, f[50005], ind[50005];
struct edge {
int to, cost;
};
vector<edge> p[50005];
queue<int> q;
int main () {
cin >> n >> m;
f[n] = -1;
for (int i = 1; i <= m; i++) {
int bol = 0;
cin >> u >> v >> l;
for (int i = 0; i < p[u].size(); i++) { //处理重边,只取权最大的
if(p[u][i].to == v) {
p[u][i].cost = max(p[u][i].cost, l);
bol = 1;
}
}
if(bol == 0) p[u].push_back((edge) {v, l});
}
q.push(1);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = 0; i < p[x].size(); i++) {
int y = p[x][i].to;
f[y] = max(f[y], f[x] + p[x][i].cost);
// ind[y]--;
// if (ind[y] == 0)
q.push(y);
}
}
cout << f[n];
return 0;
}
WA了5, 6两个点