#import sys
#import re
import heapq

input = raw_input
range = xrange
def initialize_graph():
    graph = {}
    n, edge_list = read_file()
    for edge_inf in edge_list:
        node1 = edge_inf[0]
        node2 = edge_inf[1]
        cost = edge_inf[2]
        graph = set_new_node(graph, node1, node2)
        graph[node1][node2] = cost
        graph[node2][node1] = cost
    return n, graph

def set_new_node(graph, node1, node2):
    if not node1 in graph.keys():
        graph[node1] = {}
    if not node2 in graph.keys():
        graph[node2] = {}
    return graph

def solve_by_dijkstra(graph, n):
    min_dist_dict = {}
    prev_node_dict = {}
    q = []
    heapq.heappush(q, (0, 1))
    
    prev_node = ''

    while True:
        if q == []:
            return None, None
        dist, node = heapq.heappop(q)
        min_dist_dict[node] = dist
        prev_node_dict[node] = prev_node
        if node == n: 
            return min_dist_dict, prev_node_dict
        prev_node = node
        culc_min_dist_and_put(graph, q, node, min_dist_dict, prev_node_dict)
    return None, None

def culc_min_dist_and_put(graph, q, departure_node, min_dist_dict, prev_node_dict):
    for arrival_node in graph[departure_node].keys():
        tmp_d = min_dist_dict[departure_node] + graph[departure_node][arrival_node]
        if arrival_node in min_dist_dict.keys():
            if tmp_d < min_dist_dict[arrival_node]:
                min_dist_dict[arrival_node] = tmp_d
                heapq.heappush(q, (min_dist_dict[arrival_node], arrival_node))
        else:
            min_dist_dict[arrival_node] = tmp_d
            heapq.heappush(q, (min_dist_dict[arrival_node], arrival_node))

def main():
    n, graph = initialize_graph()
    min_dist_dict, prev_node_dict = solve_by_dijkstra(graph, n)
    if min_dist_dict is not None:
        out = min_dist_dict[n]
    else:
        out = -1
    print(out)

def read_file():
    M = 10 ** 10
    n, m = map(int, input().split(" "))
    ws = [[M for _ in range(n+1)] for _ in range(n+1)]

    pairs = []
    for _ in range(m):
        l, r, w = map(int, input().split(" "))
        #print("i", _, l, r, w)
        for s in range(l, r+1):
            for t in range(s+1, r+1):
                #print((s, t))
                ws[s][t] = min(ws[s][t], w)
                pairs.append((s, t))

    set_pairs = set(pairs)
    #print(pairs, set_pairs)
    edge_list = []
    for s, t in set_pairs:
        w = ws[s][t]
        edge_list.append([s, t, w])

    #print("edge_list", edge_list)
    return n, edge_list


main()