from scipy.sparse import*
n,*t=map(int,open(0).read().split())
*u,=range(n+1)
a=csgraph.dijkstra(csr_matrix(([0]*n+t[3::3],(u[1:]+t[1::3],u[:-1]+t[2::3])),[n+1]*2),1,1)[n]
print(int(a)if a<1e18else-1)