using System;
using System.Collections.Generic;
using System.Linq;

class D
{
    static void Main()
    {
#if DEBUG
        string exampleInput =
        @"10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3
";
        Console.SetIn(new System.IO.StringReader(exampleInput));
#endif

        var NM = Console.ReadLine().Split(' ').Select(int.Parse).ToList();

        var result = new long[NM[0]];
        var L = Enumerable.Range(0, NM[0]).Select(_ => new List<List<int>>()).ToList();
        for (int m = 0; m < NM[1]; m++)
        {
            var LRC = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
            LRC[0]--;
            LRC[1]--;
            L[LRC[0]].Add(LRC);
        }
        var list = new SortedDictionary<int, int>();
        long shortestR = 0;
        for (int i = 0; i < NM[0]; i++)
        {
            foreach (var lrc in L[i])
            {
                int v;
                int r = -lrc[1];
                if (list.TryGetValue(r, out v))
                {
                    if (v > lrc[2]) list[r] = lrc[2];
                }
                else
                {
                    list.Add(r, lrc[2]);
                }
            }
            if (i > 0 && result[i] == 0) continue;
            if (i != shortestR) continue;
            long minC = (long)1e18;
            foreach (var rc in list)
            {
                var r = -rc.Key;
                var c = result[i] + rc.Value;
                if (result[r] == 0) result[r] = c;
                else result[r] = Math.Min(result[r], c);
                if (c < minC)
                {
                    minC = c;
                    shortestR = r;
                }
            }
            list.Clear();
        }
        Console.WriteLine(result.Last() == 0 ? -1 : result.Last());
        Console.Out.Flush();
    }
}
