#include<bits/stdc++.h>
using namespace std;
#define int long long
int read()
{
char ch = getchar();
int x = 0,f = 1;
while(ch < '0' || ch > '9')
{
if(ch == '-')f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int a[2001][2001];
queue <int> q;
int rd[2001];
int dp[2001];
signed main()
{
int n,m;
n = read(),m = read();
for(int i = 1;i <= m;i ++)
{
int x,y,z;
x = read(),y = read(),z = read();
a[x][y] = z;
rd[y] ++;
}
memset(dp,128,sizeof(dp));
for(int i = 1;i <= n;i ++)
{
if(rd[i] == 0)
{
q.push(i);
}
}
while(!q.empty())
{
int x = q.front();
q.pop();
for(int i = 1;i <= n;i ++)
{
if(a[x][i])
{
rd[i] --;
if(rd[i] == 0)
{
q.push(i);
}
}
}
}
dp[1] = 0;
q.push(1);
while(!q.empty())
{
int x = q.front();
q.pop();
for(int i = 1;i <= n;i ++)
{
if(a[x][i] == 0)continue;
dp[i] = max(dp[i],dp[x] + a[x][i]);
rd[i] --;
if(rd[i] == 0)
{
q.push(i);
}
}
}
if(dp[n] == INT_MIN)
{
cout<<-1;
return 0;
}
cout<<dp[n];
return 0;
}