#include <bits/stdc++.h>
using namespace std;

#define N 100005

int L[N], R[N], C[N];

int n, m;

using ll = long long;
ll a[N];

vector <int> v[N];

const ll inf = 0x3f3f3f3f3f3f3f3f;

void give(int k, ll v) {
    k = n + 1 -  k;
    a[k] = v;
}
ll calc(int k) {
	k = n + 1 - k;
    ll res = inf;
    for ( ; k; k -= k & -k) res = min(res, a[k]);
    return res;
}
void update(int k, ll val) {
	k = n + 1 - k;
	for ( ; k <= n; k += k & -k) a[k] = min(a[k], val);
}

int main() {
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= m; i ++) scanf("%d %d %d", &L[i], &R[i], &C[i]), v[L[i]].push_back(i);
    for (int i = 1; i <= n; i ++) give(i, inf);
    give(1, 0);
    for (int i = 1; i <= n; i ++) {
        ll val = calc(i);
        for (auto x : v[i]) {
            int r = R[x], c = C[x];
            update(r, val + c);
        }
    }
    ll ans = calc(n);
    if (ans == inf) ans = -1;
    cout << ans << endl;
	return 0;
}
