import sys

input = sys.stdin.readline



n,m = list(map(int, input().split()))

t = sorted([tuple(map(int, input().split())) for i in range(m)], key=lambda x:(x[0], -x[1]))

d=[0,0]+[1e18]*n

for l,r,c in t:

  for i in range(r,l,-1):

    if d[l]+c<d[i]:

      d[i]=d[l]+c

    else:

      break

print((d[-2]%10**18or-1))

import sys

input = sys.stdin.readline



n,m = list(map(int, input().split()))

t = sorted([tuple(map(int, input().split())) for i in range(m)])

INF = 10 ** 18

dp = [0,0] + [INF] * (n-1)

for l,r,c in t:

  for i in range(r,l,-1):

    if dp[l] + c < dp[i]:

      dp[i] = dp[l] + c

    else:

      break

ans = dp[n]

if ans == INF:

  print((-1))

else:

  print(ans)
import sys

input = sys.stdin.readline
n, m = list(map(int, input().split()))
t = sorted(
    [tuple(map(int, input().split())) for i in range(m)], key=lambda x: (x[0], -x[1])
)
d = [0, 0] + [1e18] * n
for l, r, c in t:
    for i in range(r, l, -1):
        if d[l] + c < d[i]:
            d[i] = d[l] + c
        else:
            break
print((d[-2] % 10**18 or -1))

import sys

input = sys.stdin.readline
n, m = list(map(int, input().split()))
t = sorted([tuple(map(int, input().split())) for i in range(m)])
INF = 10**18
dp = [0, 0] + [INF] * (n - 1)
for l, r, c in t:
    for i in range(r, l, -1):
        if dp[l] + c < dp[i]:
            dp[i] = dp[l] + c
        else:
            break
ans = dp[n]
if ans == INF:
    print((-1))
else:
    print(ans)

-t = sorted( -    [tuple(map(int, input().split())) for i in range(m)], key=lambda x: (x[0], -x[1]) -) -d = [0, 0] + [1e18] * n +t = sorted([tuple(map(int, input().split())) for i in range(m)]) +INF = 10**18 +dp = [0, 0] + [INF] * (n - 1) -        if d[l] + c < d[i]: -            d[i] = d[l] + c +        if dp[l] + c < dp[i]: +            dp[i] = dp[l] + c -print((d[-2] % 10**18 or -1)) +ans = dp[n] +if ans == INF: +    print((-1)) +else: +    print(ans)