#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct node {ll u,v;};
bool operator < (const node& a,const node& b){
if(a.v==b.v) return a.u>b.u;
return a.v<b.v;
}
#define maxn 1000+10
priority_queue <node> q;
vector <node> g[maxn];
bool vis[maxn];
ll f[maxn];
void d(ll x){
memset(f,-0x3f3f3f3f,sizeof(f));
q.push( (node){x,0} );
f[x]=0;
while(!q.empty()){
node t=q.top(); q.pop();
ll u=t.u;
if(vis[u]) continue;
else vis[u]=1;
for(int i=0;i<(int)g[u].size();i++){
node to=g[u][i];
if(f[to.u]<f[u]+g[u][i].v){
f[to.u]=f[u]+to.v;
q.push( (node){to.u,f[to.u]} );
}
}
}
}
int main(){
ll n,m;
scanf("%lld%lld",&n,&m);
for(ll i=0;i<m;i++){
ll x,y,z;
scanf("%lld%lld%lld",&x,&y,&z);
g[x].push_back( (node){y,z} );
}
d(1);
if(f[n]==-0x3f3f3f3f) printf("-1");
else printf("%lld",f[n]);
return 0;
}