// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



// Optimization



//#pragma GCC optimize("O3")

#define endl '\n'



// Shortcut



#define int long long

#define eb emplace_back

#define pb push_back

#define pob pop_back

#define mp make_pair

#define upb upper_bound

#define lwb lower_bound

#define fi first

#define se second

#define For(i, l, r) for (int i = l; i < r; i++)

#define ForE(i, l, r) for (int i = l; i <= r; i++)

#define Ford(i, r, l) for (int i = r; i > l; i--)

#define FordE(i, r, l) for (int i = r; i >= l; i--)

#define Fora(i, a) for (auto i : a)



// I/O & Debug



#define PrintV(a) Fora(iiii, a) cout << iiii << ' '; cout << endl;

#define PrintVl(a) Fora(iiii, a) cout << iiii << endl;

#define PrintA(a, l, r) for (int iiii = l; iiii <= r; iiii++) cout << a[iiii] << ' '; cout << endl;

#define PrintAl(a, l, r) for (int iiii = l; iiii <= r; iiii++) cout << a[iiii] << endl;

#define Ptest(x) return cout << x, 0;

#define gl(s) getline(cin, s);

#define setpre(x) fixed << setprecision(x)

/*

#define debug(args...){ string _sDEB = #args; replace(_sDEB.begin(), _sDEB.end(), ',', ' '); stringstream _ssDEB(_sDEB); istream_iterator<string> _itDEB(_ssDEB); DEB(_itDEB, args); }

void DEB(istream_iterator<string> it) {}

template<typename T, typename... Args>

void DEB(istream_iterator<string> it, T a, Args... args){

    cout << *it << " = " << a << endl;

    DEB(++it, args...);

}

*/



// Functions



//#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')

#define bend(a) a.begin(), a.end()

#define rbend(a) a.rbegin(), a.rend()

#define mset(a) memset(a, 0, sizeof(a))

#define mset1(a) memset(a, 1, sizeof(a))

#define msetn1(a) memset(a, -1, sizeof(a))

#define msetinf(a) memset(a, 0x3f, sizeof(a))

#define gcd __gcd

#define __builtin_popcount __builtin_popcountll

//mt19937 rando(chrono::steady_clock::now().time_since_epoch().count());



// Data Structure



#define pque priority_queue

#define mts multiset

#define y0 _y0_

#define y1 _y1_

#define div divi

typedef long long ll;

typedef long double ld;

typedef vector <int> vi;

typedef vector <ll> vll;

typedef vector <ld> vld;

typedef vector <string> vs;

typedef pair <int, int> pii;

typedef pair <ll, ll> pll;

typedef vector <vi > vvi;

typedef vector <vll > vvll;

typedef vector <pii > vpii;

typedef vector <pll > vpll;



const int N = 1e5 + 5;

int mod = 1e9 + 7, mod1 = 998244353, mod2 = 1e9 + 9, inf = 1e9 + 7;

ll infll = 1e18 + 7;



int n, m;

int dist[N];

vpii adj[N];



void dijkstra(int u){

    pque <pii, vpii, greater <pii > > pq;

    msetinf(dist);

    pq.push({0, u});

    dist[u] = 0;

    while (!pq.empty()){

        int t = pq.top().se, dt = pq.top().fi;

        pq.pop();

        if (dt != dist[t]) continue;

        Fora(v, adj[t]){

            if (dist[v.fi] > dist[t] + v.se){

                dist[v.fi] = dist[t] + v.se;

                pq.push({dist[v.fi], v.fi});

            }

        }

    }

}



signed main(){

    ios_base::sync_with_stdio(0);

    cin.tie(0); cout.tie(0);

    cin >> n >> m;

    ForE(i, 2, n){

        adj[i].pb({i - 1, 0});

    }

    ForE(i, 1, m){

        int u, v, w; cin >> u >> v >> w;

        adj[u].pb({v, w});

    }

    dijkstra(1);

    if (dist[n] >= infll) cout << -1;

    else cout << dist[n];

}



/*

==================================+

INPUT:                            |

------------------------------    |



------------------------------    |

==================================+

OUTPUT:                           |

------------------------------    |



------------------------------    |

==================================+

*/

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

using LL = long long;

using ULL = unsigned long long;

using VI = vector<int>;

using VL = vector<LL>;

using PII = pair<int, int>;

using PLL = pair<LL, LL>;



#define SZ(a) (int)a.size()

#define ALL(a) a.begin(), a.end()

#define MP make_pair

#define PB push_back

#define EB emplace_back

#define F first

#define S second

#define FOR(i, a, b) for (int i = (a); i<(b); ++i)

#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)

#define FILL(a, b) memset(a, b, sizeof(a))



void dout() { cerr << endl; }



template <typename Head, typename... Tail>

void dout(Head H, Tail... T) {

    cerr << H << ' ';

    dout(T...);

}



int main()

{

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    int n, m;

    cin >> n >> m;

    vector<tuple<int, int, LL>> a;

    a.reserve(m);

    FOR(i, 0, m)

    {

        int l, r;

        LL c;

        cin >> l >> r >> c;

        a.EB(l, r, c);

    }

    sort(ALL(a));

    set<pair<int, LL>> q;

    q.emplace(1, 0LL);

    for (auto& op : a)

    {

        int l, r, c;

        tie(l, r, c) = op;

        auto cur_it = q.lower_bound(MP(l, -1LL));

        if (cur_it == q.end())

            break;

        LL x = cur_it->S + c;

        auto bigger = q.upper_bound(MP(r, -1LL));

        if (bigger != q.end() && bigger->S <= x)

            continue;

        else

        {

            auto it = q.emplace(r, x).F;

            while (it != q.begin() && it->S <= prev(it)->S)

                q.erase(prev(it));

        }

    }

    auto it = q.lower_bound(MP(n, -1LL));

    if (it == q.end())

        cout << -1 << "\n";

    else

        cout << it->S << "\n";

    return 0;

}

// Fastest known solution for this problem: