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