#include <bits/stdc++.h>
using namespace std;
int n,m,in[2005],maxa[2005];bool vis[2005];
struct node{
int to,w;
};
vector <node> G[2005];
queue <int> q;
void topo(){
memset(maxa,0x3f,sizeof(maxa));
maxa[1] = 0,maxa[n] = -1;
for (int i = 1;i <= n;i++) if (!in[i]) q.push(i);
while (!q.empty()){
int u = q.front();
for (int i = 0;i < G[u].size();i++){
int v = G[u][i].to;
maxa[v] = max(maxa[v],maxa[u]+G[u][i].w);
in[v]--;
if (!in[v]) q.push(v);
}
q.pop();
}
}
int main(){
cin >> n >> m;
int u,v,w;
while (m--){
cin >> u >> v >> w;
G[u].push_back(node{v,w});
in[v]++;
}
topo();
cout << maxa[n];
return 0;
}