#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define INF 1e9
using namespace std;
const int maxn=1000+10;
const int maxm=50000+10;
struct Edge
{
int from,to,dist;
Edge(){}
Edge(int f,int t,int d):from(f),to(t),dist(d){}
};
struct BellmanFord
{
int n,m;
int head[maxn],next[maxm];
Edge edges[maxm];
bool inq[maxn];
int cnt[maxn];
int d[maxn];
void init(int n)
{
this->n=n;
m=0;
memset(head,-1,sizeof(head));
}
void AddEdge(int from,int to,int dist)
{
edges[m]=Edge(from,to,dist);
next[m]=head[from];
head[from]=m++;
}
int bellmanford(int s)
{
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
queue<int> Q;
for(int i=1;i<=n;i++) d[i]= i==s?0:INF;
Q.push(s);
while(!Q.empty())
{
int u=Q.front(); Q.pop();
inq[u]=false;
for(int i=head[u];i!=-1;i=next[i])
{
Edge &e=edges[i];
if(d[e.to] > d[u]+e.dist)
{
d[e.to] = d[u]+e.dist;
if(!inq[e.to])
{
inq[e.to]=true;
Q.push(e.to);
if(++cnt[e.to]>n) return -1;
}
}
}
}
return d[n]==INF?-2:d[n];
}
}BF;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
BF.init(n);
while(m--)
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);//转化为a-b<=val a向b连权值为val的边
BF.AddEdge(u,v,d);
}
for(int i=1;i<=n;i++)
BF.AddEdge(0,i,0); //保证图的连通性
//注意特殊关系
if(BF.bellmanford(0)!=-1) printf("%d
",BF.bellmanford(1));
else puts("-1");
}
return 0;
}