#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const long long INF = 1000000009000000009;

int n, m;
int l[N], r[N], dd[N];
bool c[N];

vector<int> t1[N * 4];
void ubd1(int tl, int tr, int l, int r, int x, int pos)
{
    if (l > r)
        return;
    if (tl == l && tr == r)
    {
        t1[pos].push_back(x);
        return;
    }
    int m = (tl + tr) / 2;
    ubd1(tl, m, l, min(m, r), x, pos * 2);
    ubd1(m + 1, tr, max(m + 1, l), r, x, pos * 2 + 1);
}

vector<int> v;
void qry1(int tl, int tr, int x, int pos)
{
    while (!t1[pos].empty())
    {
        v.push_back(t1[pos].back());
        t1[pos].pop_back();
    }
    if (tl == tr)
        return;
    int m = (tl + tr) / 2;
    if (x <= m)
        qry1(tl, m, x, pos * 2);
    else
        qry1(m + 1, tr, x, pos * 2 + 1);
}

struct ban
{
    long long minu;
    int mini;
    long long laz;
    ban()
    {
        minu = INF;
        mini = -1;
        laz = INF;
    }
    ban(int i)
    {
        minu = INF;
        mini = i;
        laz = INF;
    }
};

ban t[N * 4];

void shi(int pos)
{
    t[pos * 2].minu = min(t[pos * 2].minu, t[pos].laz);
    t[pos * 2].laz = min(t[pos * 2].laz, t[pos].laz);
    t[pos * 2 + 1].minu = min(t[pos * 2 + 1].minu, t[pos].laz);
    t[pos * 2 + 1].laz = min(t[pos * 2 + 1].laz, t[pos].laz);
    t[pos].laz = INF;
}

ban mer(const ban& l, const ban& r)
{
    ban res;
    if (l.mini == -1 && r.mini == -1)
        return res;
    if (l.mini == -1)
    {
        res.mini = r.mini;
        res.minu = r.minu;
    }
    else if (r.mini == -1)
    {
        res.mini = l.mini;
        res.minu = l.minu;
    }
    else if (l.minu < r.minu)
    {
        res.mini = l.mini;
        res.minu = l.minu;
    }
    else
    {
        res.mini = r.mini;
        res.minu = r.minu;
    }
    return res;
}

void bil(int tl, int tr, int pos)
{
    if (tl == tr)
    {
        t[pos] = ban(tl);
        return;
    }
    int m = (tl + tr) / 2;
    bil(tl, m, pos * 2);
    bil(m + 1, tr, pos * 2 + 1);
    t[pos] = mer(t[pos * 2], t[pos * 2 + 1]);
}

void ubd(int tl, int tr, int l, int r, long long y, int pos)
{
    if (l > r)
        return;
    if (tl == l && tr == r)
    {
        t[pos].minu = min(t[pos].minu, y);
        t[pos].laz = min(t[pos].laz, y);
        return;
    }
    shi(pos);
    int m = (tl + tr) / 2;
    ubd(tl, m, l, min(m, r), y, pos * 2);
    ubd(m + 1, tr, max(m + 1, l), r, y, pos * 2 + 1);
    t[pos] = mer(t[pos * 2], t[pos * 2 + 1]);
}

void han(int tl, int tr, int x, int pos)
{
    if (tl == tr)
    {
        t[pos] = ban();
        return;
    }
    shi(pos);
    int m = (tl + tr) / 2;
    if (x <= m)
        han(tl, m, x, pos * 2);
    else
        han(m + 1, tr, x, pos * 2 + 1);
    t[pos] = mer(t[pos * 2], t[pos * 2 + 1]);
}

ban qry(int tl, int tr, int l, int r, int pos)
{
    if (l > r)
        return ban();
    if (tl == l && tr == r)
        return t[pos];
    shi(pos);
    int m = (tl + tr) / 2;
    return mer(qry(tl, m, l, min(m, r), pos * 2),
               qry(m + 1, tr, max(m + 1, l), r, pos * 2 + 1));
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; ++i)
    {
        scanf("%d%d%d", &l[i], &r[i], &dd[i]);
        ubd1(1, n, l[i], r[i], i, 1);
    }
    bil(1, n, 1);
    ubd(1, n, 1, 1, 0, 1);
    while (1)
    {
        if (t[1].minu == INF)
        {
            cout << -1 << endl;
            return 0;
        }
        int x = t[1].mini;
        long long d = t[1].minu;
        if (x == n)
        {
            cout << d << endl;
            return 0;
        }
        han(1, n, x, 1);
        v.clear();
        qry1(1, n, x, 1);
        for (int j = 0; j < v.size(); ++j)
        {
            int i = v[j];
            if (c[i])
                continue;
            c[i] = true;
            ubd(1, n, l[i], r[i], d + dd[i], 1);
        }
    }
    return 0;
}


Measured memory_bytes: 30848