#include<bits/stdc++.h>
using namespace std;
const int M = 5e4 + 5;
const int W = 1e5 + 5;
int n , m , cnt;
int times[M] , ans[M] , h[M] , ne[M] , e[M] , ind[M] , oud[M] , w[W];
void add(int a , int b , int c){
e[cnt] = b , w[cnt] = c , ne[cnt] = h[a] , h[a] = cnt++;
}
int main(){
cin >> n >> m;
memset(h , -1 , sizeof(h));
memset(ans , -1e9 , sizeof(ans));
for(int i = 1 ; i <= m ; i++){
int a , b , c;
cin >> a >> b >> c;
add(a , b , c);
ind[b]++;
oud[a]++;
}
queue<int > q;
for(int i = 2 ; i <= n ; i++){
if(!ind[i]){
q.push(i);
}
}
while(!q.empty()){
int t = q.front();
q.pop();
for(int i = h[t] ; i != -1 ; i = ne[i]){
int k = e[i];
ind[k]--;
if(!ind[k]){
q.push(k);
}
}
}
q.push(1);
ans[1] = 0;
while(!q.empty()){
int t = q.front();
q.pop();
for(int i = h[t] ; i != -1 ; i = ne[i]){
int k = e[i];
ind[k]--;
ans[k] = max(ans[k] , ans[t] + w[i]);
if(!ind[k]){
q.push(k);
}
}
}
if(ans[n] == -1e9) cout << -1;
else cout << ans[n];
return 0;
}