from copy import deepcopy
x=[int(i) for i in input().split(' ')]
n=x[0]
m=x[1]
go={}
fro={}
fath={}
ne={}
lu={}
for i in range(n):
go[i+1]=[]
fro[i+1]=[]
fath[i+1]=0
ne[i+1]={}
lu[i+1]=0
for i in range(m):
b=[int(o) for o in input().split(' ')]
u=b[0]
v=b[1]
w=b[2]
go[u].append(v)
fro[v].append(u)
fath[v]+=1
if v in ne[u]:
ff=ne[u][v]
else:
ne[u][v]=0
ff=0
if ff<w:
ne[u][v]=w
queue=[]
for i in fro:
if fath[i]==0:
fath[i]='n'
queue.append(i)
visited=[]
while queue:
now=queue.pop(0)
for i in go[now]:
fath[i]-=1
for i in fath:
if fath[i]==0:
if i not in visited:
visited.append(i)
queue.append(i)
zhi=lu[now]
for i in ne[now]:
le=lu[i]
xle=zhi
g=ne[now][i]
xle+=g
if xle>le:
lu[i]=xle
if n not in visited:
print(-1)
else:
print(lu[n])