package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout

	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
		if len(os.Args) > 2 {
			wfp, _ = os.Create(os.Args[2])
		}
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	seg := Segment{}
	seg.init(n)

	for i := 0; i < n; i++ {
		seg.minimize(i, seg.inf)
	}

	seg.bucket[0][0] = 0

	ee := make(Edges, m)
	for i := 0; i < m; i++ {
		ee[i].l = getNextInt(scanner) - 1
		ee[i].r = getNextInt(scanner)
		ee[i].c = getNextInt64(scanner)
	}
	sort.Sort(ee)
	for i := 0; i < m; i++ {
		seg.rangeminimize(ee[i].l, ee[i].r, ee[i].c)
	}

	ans := seg.get(n - 1)
	if ans == seg.inf {
		ans = -1
	}

	fmt.Fprintln(writer, ans)

	writer.Flush()
}

type Edge struct {
	l, r int
	c    int64
}

type Edges []Edge

func (a Edges) Len() int           { return len(a) }
func (a Edges) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Edges) Less(i, j int) bool { return a[i].l < a[j].l }

// Segment ...
type Segment struct {
	n, h, i, chunk int
	unit           []int
	bucket         [][]int64
	inf            int64
}

func (seg *Segment) init(n int) {
	seg.n = n
	seg.unit = make([]int, 1)
	seg.unit[0] = 1
	seg.bucket = make([][]int64, 1)
	seg.bucket[0] = make([]int64, n)

	chunk := 330
	for i := 0; n > 1; i++ {
		n = (n-1)/chunk + 1
		seg.bucket = append(seg.bucket, make([]int64, n))
		seg.unit = append(seg.unit, seg.unit[i]*chunk)
	}
	seg.h = len(seg.unit)
	seg.chunk = chunk
	seg.inf = math.MaxInt64 - int64(math.MaxInt32)
}

func (seg *Segment) minimize(index int, value int64) {
	seg.bucket[0][index] = value
	for seg.i = 0; seg.i < seg.h-1; seg.i++ {
		s := index - index%seg.chunk
		t := s + seg.chunk
		if t > len(seg.bucket[seg.i]) {
			t = len(seg.bucket[seg.i])
		}
		parent := index / seg.chunk
		min := seg.inf
		for i := s; i < t; i++ {
			if min > seg.bucket[seg.i][i] {
				min = seg.bucket[seg.i][i]
			}
		}
		seg.bucket[seg.i+1][parent] = min
		index /= seg.chunk
	}
}
func (seg *Segment) get(i int) int64 {
	ans := seg.bucket[0][i]
	for seg.i = 1; seg.i < seg.h; seg.i++ {
		if ans > seg.bucket[seg.i][i/seg.unit[seg.i]] {
			ans = seg.bucket[seg.i][i/seg.unit[seg.i]]
		}
		i /= seg.unit[seg.i]
	}
	return ans
}
func (seg *Segment) rangeminimize(l, r int, value int64) {
	value += seg.get(l)
	for index := l; index < r; index += seg.unit[seg.i] {
		for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ {
			if seg.bucket[seg.i][index/seg.unit[seg.i]] > value {
				seg.bucket[seg.i][index/seg.unit[seg.i]] = value
			}
			if index+seg.unit[seg.i+1] >= r {
				break
			}
		}
		if seg.bucket[seg.i][index/seg.unit[seg.i]] > value {
			seg.bucket[seg.i][index/seg.unit[seg.i]] = value
		}
	}
}


Measured memory_bytes: 8192