#include <bits/stdc++.h>

const int N = 100010;
const int MAX = 1 << 17;
using ll = long long;
using pii = std::pair <ll, int>;

pii seg[MAX << 1];
ll lazy[MAX << 1];
bool vis[MAX << 1];
int l[N], r[N], c[N];
std::vector <int> divide[MAX << 1];

void append(int sit, int l, int r, int ql, int qr, int id){
    if (ql > r || qr < l){
        return;
    }
    if (ql <= l && qr >= r){
        divide[sit].push_back(id);
        return;
    }
    int mid = (l + r) / 2;
    append(sit << 1, l, mid, ql, qr, id);
    append(sit << 1 | 1, mid + 1, r, ql, qr, id);
}

void push(int sit){
    if (lazy[sit] == LLONG_MAX){
        return;
    }
    seg[sit << 1].first = std::min(seg[sit << 1].first, lazy[sit]);
    seg[sit << 1 | 1].first = std::min(seg[sit << 1 | 1].first, lazy[sit]);
    lazy[sit << 1] = std::min(lazy[sit << 1], lazy[sit]);
    lazy[sit << 1 | 1] = std::min(lazy[sit << 1 | 1], lazy[sit]);
    lazy[sit] = LLONG_MAX;
}

void pull(int sit){
    seg[sit] = std::min(seg[sit << 1], seg[sit << 1 | 1]);
}

void add(int sit, int l, int r, int ql, int qr, ll value){
    if (ql > r || qr < l){
        return;
    }
    if (ql <= l && qr >= r){
        lazy[sit] = std::min(lazy[sit], value);
        seg[sit].first = std::min(seg[sit].first, value);
        return;
    }
    push(sit);
    int mid = (l + r) / 2;
    add(sit << 1, l, mid, ql, qr, value);
    add(sit << 1 | 1, mid + 1, r, ql, qr, value);
    pull(sit);
}

void set(int sit, int l, int r, int q, ll value){
    if (q > r || q < l){
        return;
    }
    if (q <= l && q >= r){
        seg[sit].first = value;
        return;
    }
    push(sit);
    int mid = (l + r) / 2;
    set(sit << 1, l, mid, q, value);
    set(sit << 1 | 1, mid + 1, r, q, value);
    pull(sit);
}

pii query(int sit, int l, int r, int ql, int qr){
    if (ql > r || qr < l){
        return {LLONG_MAX, -1};
    }
    if (ql <= l && qr >= r){
        return seg[sit];
    }
    push(sit);
    int mid = (l + r) / 2;
    return std::min(query(sit << 1, l, mid, ql, qr), query(sit << 1 | 1, mid + 1, r, ql, qr));
}

void add(int ql, int qr, ll value){
    add(1, 0, MAX - 1, ql, qr, value);
}

pii query(int ql, int qr){
    return query(1, 0, MAX - 1, ql, qr);
}

int main(){
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < m; ++ i){
        scanf("%d%d%d", &l[i], &r[i], &c[i]);
        -- l[i], -- r[i];
        append(1, 0, MAX - 1, l[i], r[i], i);
    }
    for (int i = 0; i < MAX << 1; ++ i){
        lazy[i] = LLONG_MAX;
    }
    for (int i = 0; i < n; ++ i){
        seg[i + MAX] = {i == 0 ? 0 : LLONG_MAX, i};
    }
    for (int i = MAX - 1; i; -- i){
        seg[i] = std::min(seg[i << 1], seg[i << 1 | 1]);
    }
    while (true){
        pii dis = query(0, n - 1);
        if (dis.second == n - 1){
            printf("%lld\n", dis.first);
            return 0;
        }
        if (dis.first == LLONG_MAX){
            break;
        }
        int pos = dis.second + MAX;
        for ( ; pos; pos >>= 1){
            for (auto u : divide[pos]){
                if (vis[u]){
                    continue;
                }
                add(l[u], r[u], dis.first + c[u]);
                vis[u] = true;
            }
            divide[pos].clear();
        }
        set(1, 0, MAX - 1, dis.second, LLONG_MAX);
    }
    puts("-1");
    return 0;
}


Measured memory_bytes: 23296