#include <bits/stdc++.h>
using namespace std;
int ind[1505],maxn[1505];
struct k
{
int a,num;
k(int a1,int num1)
{
a=a1,num=num1;
}
};
vector<k> p[1505];
int main()
{
int n,m;
cin>>n>>m;
for(int i=1; i<=m; i++)
{
int u,v,w;
cin>>u>>v>>w;
p[u].push_back ({v,w});
ind[v]++;
}
queue <int> q;
for(int i=2; i<=n; i++)
maxn[i]=-1e9;
q.push(1);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0; i<p[x].size(); i++)
{
int t=p[x][i].a,len=p[x][i].num;
maxn[t]=max(maxn[t],maxn[x]+len);
ind[t]--;
if(!ind[t])
q.push(t);
}
}
if(maxn[n]!=-1e9)
cout<<maxn[n]<<endl;
else
cout<<-1<<endl;
return 0;
}
3,5,6测试点WA了