package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc *bufio.Scanner

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

type query struct {
	l int
	r int
	c int
}

type queries []query

func (q queries) Len() int {
	return len(q)
}

func (q queries) Swap(i, j int) {
	q[i], q[j] = q[j], q[i]
}

func (q queries) Less(i, j int) bool {
	switch {
	case q[i].l != q[j].l:
		return q[i].l < q[j].l
	case q[i].c != q[j].c:
		return q[i].c < q[j].c
	default:
		return q[i].l > q[j].l
	}
}

type endpoint struct {
	x int
	d int
}

func main() {
	sc = bufio.NewScanner(os.Stdin)
	sc.Buffer(make([]byte, 0), 1000000001*3)
	sc.Split(bufio.ScanWords)
	n, m := nextInt(), nextInt()
	var queries queries = make([]query, m)
	for qi := range queries {
		l, r, c := nextInt(), nextInt(), nextInt()
		queries[qi] = query{l, r, c}
	}
	sort.Sort(queries)

	endpoints := make([]endpoint, 0)
	endpoints = append(endpoints, endpoint{1, 0})
	for _, q := range queries {
		li := sort.Search(len(endpoints), func(i int) bool { return endpoints[i].x >= q.l })
		if li == len(endpoints) {
			fmt.Println(-1)
			return
		}
		dr := endpoints[li].d + q.c
		endpoints = endpoints[li:]
		ri := sort.Search(len(endpoints), func(i int) bool { return endpoints[i].x >= q.r })
		switch {
		case ri == len(endpoints):
			endpoints = append(endpoints, endpoint{q.r, dr})
		case endpoints[ri].x != q.r:
			endpoints = append(endpoints[:ri], append([]endpoint{endpoint{q.r, dr}}, endpoints[ri:]...)...)
		case endpoints[ri].d > dr:
			endpoints[ri].d = dr
		default:
		}
	}

	ni := sort.Search(len(endpoints), func(i int) bool { return endpoints[i].x >= n })
	if ni < len(endpoints) && endpoints[ni].x == n {
		fmt.Println(endpoints[ni].d)
	} else {
		fmt.Println(-1)
	}
}
