N, m = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(m)]

s = sorted(s, key=lambda x:x[0])
N += 1
# N: 処理する区間の長さ
INF = 2**31-1
LV = (N-1).bit_length()
N0 = 2**LV
data = [INF]*(2*N0)
lazy = [None]*(2*N0)

# 伝搬対象の区間を求める
def gindex(l, r):
    L = (l + N0) >> 1; R = (r + N0) >> 1
    lc = 0 if l & 1 else (L & -L).bit_length()
    rc = 0 if r & 1 else (R & -R).bit_length()
    for i in range(LV):
        if rc <= i:
            yield R
        if L < R and lc <= i:
            yield L
        L >>= 1; R >>= 1

# 遅延伝搬処理
def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if v is None:
            continue
        lazy[2*i-1] = data[2*i-1] = lazy[2*i] = data[2*i] = v
        lazy[i-1] = None

# 区間[l, r)をxで更新
def update(l, r, x):
    min_value = query(l, r)
    # print(min_value, x)
    *ids, = gindex(l, r)
    propagates(*ids)

    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            if lazy[R-1] == None:
                lazy[R-1] = min_value + x
                # data[R-1] = min_value + x
            else:
                lazy[R-1] = min(lazy[R-1], min_value + x)
            data[R-1] = min(data[R-1], min_value + x)
        if L & 1:
            if lazy[L-1] == None:
                lazy[L-1] = min_value + x
                # data[L-1] = min_value + x
            else:
                lazy[L-1] = min(lazy[L-1], min_value + x)
            data[L-1] = min(data[L-1], min_value + x)
            L += 1
        L >>= 1; R >>= 1
    for i in ids:
        data[i-1] = min(data[2*i-1], data[2*i])
    # print(data, lazy)

# 区間[l, r)内の最小値を求める
def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r

    s = INF
    while L < R:
        if R & 1:
            R -= 1
            s = min(s, data[R-1])
        if L & 1:
            s = min(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

update(1, 2, -INF)
for i in range(m):
    l = s[i][0]
    r = s[i][1]
    c = s[i][2]

    update(l, r+1, c)
    # print(query(4, 5))

tmp = query(N-1, N)
if tmp == INF:
    print(-1)
else:
    print(tmp)
