#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <map>

using namespace std;

int n, m;

struct Segm{
    int l, r;
    long long c;
};

Segm a[500000];

bool comp(Segm a, Segm b)
{
    return a.l < b.l;
}

vector<long long> d[500000];

int main()
{
    cin >> n >> m;
    for (int i = 0; i < m; i++) cin >> a[i].l >> a[i].r >> a[i].c;
    sort(a, a + m, comp);
    long long len = 0;
    map<long long, int> kek;
    int j = 0;
    for (int i = 1; i <= n; i++)
    {
        if (i > 1 && kek.size() == 0)
        {
            cout << -1;
            return 0;
        }
        if (i > 1)
        {
            auto it = kek.begin();
            len = (*it).first;
        }
        for (; j < m && a[j].l == i; j++)
        {
            kek[len + a[j].c]++;
            d[a[j].r].push_back(len + a[j].c);
        }
        for (int j = 0; j < d[i].size(); j++)
        {
            kek[d[i][j]]--;
            if (kek[d[i][j]] == 0) kek.erase(d[i][j]);
        }
    }
    cout << len;
}
