#include<iostream>
#include<vector>
#include<deque>
#include<map>
using namespace std;
const int N=1505;
struct Node{
int x;
int y;
bool operator<(const Node& other)const{
if(x!=other.x)return x<other.x;
return y<other.y;
}
};
vector<int>side[N];
map<Node,int>length;
bool inQueue[N];
bool allVisit[N];
int maxSize[N];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
side[u].push_back(v);
if(length[{u,v}])length[{u,v}]=max(length[{u,v}],w);
else length[{u,v}]=w;
}
deque<int>bfs;
bfs.push_back(1);
inQueue[1]=true;
while(!bfs.empty()){
int head=bfs.front();
bfs.pop_front();
inQueue[head]=false;
for(int i=0;i<side[head].size();i++){
if(maxSize[side[head][i]]<maxSize[head]+length[{head,side[head][i]}]){
maxSize[side[head][i]]=maxSize[head]+length[{head,side[head][i]}];
if(!inQueue[side[head][i]]){
bfs.push_back(side[head][i]);
inQueue[side[head][i]]=true;
allVisit[side[head][i]]=true;
}
}
}
}
if(allVisit[n])printf("%d\n",maxSize[n]);
else printf("-1\n");
return 0;
}