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

static class Program
{
    static long Solve(int N, int M, int[][] values)
    {
        var edges = new Dictionary<int, int>[N];
        for (int i = 0; i < N; ++i)
        {
            edges[i] = new Dictionary<int, int>();
            if (i > 0) { edges[i][i - 1] = 0; }
        }
        foreach (var v in values)
        {
            int L = v[0] - 1;
            int R = v[1] - 1;
            int C = v[2];
            edges[L][R] = C;
        }
        const long INF = long.MaxValue / 2;
        var d = Enumerable.Repeat(INF, N).ToArray();
        var q = new Queue<int>();
        d[0] = 0;
        q.Enqueue(0);
        while (q.Count > 0)
        {
            int a = q.Dequeue();
            foreach (var edge in edges[a])
            {
                int b = edge.Key;
                int c = edge.Value;
                if (d[a] + c < d[b])
                {
                    d[b] = d[a] + c;
                    q.Enqueue(b);
                }
            }
        }
        return (d[N - 1] < INF) ? d[N - 1] : -1;
    }

    static void Main(string[] args)
    {
        int[] V1 = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        int N = V1[0], M = V1[1];
        var values = new int[M][];
        for (int i = 0; i < M; ++i)
        {
            values[i] = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        }
        Console.WriteLine(Solve(N, M, values));
    }
}
