#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
const int N = 1510, M = 5e4 + 10;
int n, m;
struct Node
{
int id;
int time;
Node* next;
Node(int _id) :id(_id), next(NULL) {}
}*head[N];
int d[N], q[N];
long long int spend[N];
void add(int a, int b, int time)
{
auto p = new Node(b);
p->time = time;
p->next = head[a];
head[a] = p;
}
bool topsort()
{
int hh = 0, tt = -1;
q[++tt] = 1;
for (int i = 2; i <= n; i++)
{
if (!d[i])
{
for (auto p = head[i]; p; p->next) --d[p->id];
}
}
while (hh <= tt)
{
int t = q[hh++];
for (auto p = head[t]; p; p = p->next)
{
spend[p->id] = max(spend[p->id], spend[t] + p->time);
if (--d[p->id] == 0) q[++tt] = p->id;
}
}
return tt == n - 1;
}
int main()
{
cin >> n >> m;
//for (int i = 1; i <= n; i++) head[i]->time = 0;
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
d[b]++;
}
auto ans = topsort();
if (ans == false) cout << "-1" << endl;
else cout << spend[n] << endl;
return 0;
}