import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import dijkstra

N,M = map(int,readline().split())
LRC = np.array(read().split(),np.int64)

fr = []
to = []
wt = []

# i+1 -> i に長さ 0 の辺を貼る
fr.append(np.arange(2,N+1))
to.append(np.arange(1,N))
wt.append(np.zeros(N-1,np.int64))
# L,R に長さ C の辺を貼る
fr.append(LRC[::3])
to.append(LRC[1::3])
wt.append(LRC[2::3])

fr = np.concatenate(fr)
to = np.concatenate(to)
wt = np.concatenate(wt)

graph = csr_matrix((wt,(fr,to)),(N+1,N+1))

x = dijkstra(graph,indices=1)[-1]

if x == np.inf:
    answer = -1
else:
    answer = int(x+.5)

print(answer)