using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;

public static class Program
{
    private static readonly long INF = 1001001000100;
        struct Edge : IComparable<Edge>
        {
            public int To;
            public long Cost;
            
            public int CompareTo(Edge other)
            {
                if (Cost > other.Cost) return 1;
//                if (Cost == other.Cost) return 0;
                return -1;
            }
        }
        
        struct Pair<T, U> : IComparable<Pair<T, U>>
            where T : IComparable<T>
            where U : IComparable<U>
        {
            public T first;
            public U second;
            private bool CompWithFirst;
 
            public Pair(T f, U s, int comp)
            {
                first = f;
                second = s;
                CompWithFirst = comp == 1;
            }
 
            public int CompareTo(Pair<T, U> other)
            {
                return CompWithFirst ? first.CompareTo(other.first) : second.CompareTo(other.second);
            }
        }
        
        class PriorityQueue<T>
        {
            private readonly List<T> heap;
            private readonly Comparison<T> compare;
            private int size;
            public PriorityQueue() : this(Comparer<T>.Default) {} 
            public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) { }
            public PriorityQueue(Comparison<T> comparison) : this(16, comparison) { }
            public PriorityQueue(int capacity, Comparison<T> comparison)
            {
                this.heap = new List<T>(capacity);
                this.compare = comparison;
            }
            public void Enqueue(T item)
            {
                this.heap.Add(item);
                var i = size++;
                while (i > 0)
                {
                    var p = (i - 1) >> 1;
                    if (compare(this.heap[p], item) <= 0)
                        break;
                    this.heap[i] = heap[p];
                    i = p;
                }
                this.heap[i] = item;

            }
            public T Dequeue()
            {
                var ret = this.heap[0];
                var x = this.heap[--size];
                var i = 0;
                while ((i << 1) + 1 < size)
                {
                    var a = (i << 1) + 1;
                    var b = (i << 1) + 2;
                    if (b < size && compare(heap[b], heap[a]) < 0) a = b;
                    if (compare(heap[a], x) >= 0)
                        break;
                    heap[i] = heap[a];
                    i = a;
                }
                heap[i] = x;
                heap.RemoveAt(size);
                return ret;
            }
            public T Peek() { return heap[0]; }
            public int Count { get { return size; } }
            public bool Any() { return size > 0; }
        }
        
        public static void Main(string[] args)
        {
            try
            {
                var intInput = ReadLine().Split(' ').Select(int.Parse).ToArray();
                int n = intInput[0];
                int m = intInput[1];

                //グラフの生成
                var G = new List<Edge>[n];
                for (int i = 0; i < n-1; i++)
                {
                    var e = new Edge {To = i, Cost = 0};
                    if (G[i+1] == null) G[i+1] = new List<Edge>();
                    G[i+1].Add(e);
                }

                for (int i = 0; i < m; i++)
                {
                    var longInput = ReadLine().Split(' ').Select(long.Parse).ToArray();
                    int l = (int)longInput[0] - 1;
                    int r = (int)longInput[1] - 1;
                    long cost = longInput[2];
                
                    var e = new Edge {To = r, Cost = cost};
                    if (G[l] == null) G[l] = new List<Edge>();
                    G[l].Add(e);
                }

//                for (int i = 0; i < n; i++)
//                {
//                    foreach (var e in G[i])
//                    {
//                        WriteLine($"From:{i}To:{e.To}Cost:{e.Cost}");
//                    }
//                }

                var pq = new PriorityQueue<Pair<int, long>>();
                var p = new Pair<int, long>();
                p.first = 0;
                p.second = 0;
                pq.Enqueue(p);
                var d = new long[n];
                for (int i=0; i < n; i++) d[i] = INF;
                d[0] = 0;
                while (pq.Count > 0)
                {
                    var v = pq.Dequeue();
//                    WriteLine($"To:{v.To}Cost:{v.Cost}");
                    if (d[v.first] < v.second) continue;
                    foreach (var e in G[v.first])
                    {
                        if (d[e.To] > d[v.first] + e.Cost)
                        {
                            d[e.To] = d[v.first] + e.Cost;
                            var pp = new Pair<int, long>();
                            pp.first = e.To;
                            pp.second = d[e.To];
                            pq.Enqueue(pp);
                        }
                    }
                
                }

                long ans = d[n - 1];
                if (ans == INF) ans = -1;
                WriteLine(ans);
            }
            catch (Exception e)
            {
                WriteLine(e.StackTrace);
            }
            
        }
    }
