#include<iostream>
#include<vector>
#include<queue>
using namespace std;
const int N = 2e3 + 5;
vector<pair<int, int> >edge[N];
queue<int>q;
int dp[N], in[N];
int main(){
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i++){
int u, v, w;
cin >> u >> v >> w;
edge[u].push_back({v, w});
in[v]++;
}
for(int i = 2; i <= n; i++){
if(in[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int h = q.front();
q.pop();
for(int i = 0; i < edge[h].size(); i++){
if(!--(in[edge[h][i].first]))q.push(edge[h][i].first);
}
}
q.push(1);
while(!q.empty()){
int h = q.front();
q.pop();
for(int i = 0; i < edge[h].size(); i++){
int v = edge[h][i].first;
int w = edge[h][i].second;
dp[v] = max(dp[v], dp[h] + w);
in[v]--;
if(in[v] == 0){
q.push(v);
}
}
}
if(dp[n])cout << dp[n];
else cout << -1;
return 0;
}