#include<bits/stdc++.h>
using namespace std;
const int N = 5e4+5;
queue<int> q;
int n,m,head[N],en,dist[N],ind[N];
struct Edge{
int w,to,next;
}edge[N];
void add(int x,int y,int w){
edge[++en].to = y;
edge[en].next = head[x];
head[x] = en;
edge[en].w = w;
}
void init(){
cin >> n >> m;
for(int i=1,x,y,w;i<=m;i++){
cin >> x >> y >> w;
add(x,y,w);
++ind[y];
}
}
void solve(){
for(int i=1;i<=n;i++)
if(!ind[i] && i!=1)
q.push(i);
int u,v,w;
while(!q.empty()){
u=q.front();
q.pop();
for(int i=head[u];i;i=edge[i].next){
v=edge[i].to ;
--ind[v];
if(!ind[i] && v!=1) q.push(v);
}
}
q.push(1);
while(!q.empty()){
u=q.front();
q.pop();
for(int i=head[u];i;i=edge[i].next ){
v=edge[i].to;
w=edge[i].w;
dist[v]=max(dist[v],dist[u]+w);
--ind[v];
if(!ind[v])q.push(v);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
fill(dist,dist+N,INT_MIN);
cin.tie(0);cout.tie(0);
dist[1] = 0;
init();
solve();
if(dist[n]==INT_MIN)
cout << -1;
else
cout << dist[n];
return 0;
}