#include <bits/stdc++.h>
using namespace std;
#define MAX_N 100000
int INF = 1e9+1;
int N;
int adjmat[MAX_N][MAX_N];

int dijkstra() {
    int d[N];
    bool used[N] = {};
    for (int i=0; i<N; i++) {
        d[i] = INF;
    }
    d[0] = 0;
    while (true) {
        int v = -1;
        for (int u=0; u<N; u++) {
            if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
        }
        if (v == -1) break;
        used[v] = true;
        for (int u=0; u<N; u++) {
            d[u] = min(d[u], d[v] + adjmat[u][v]);
        }
    }
    return d[N-1];
}


int main() {
    int M; cin >> N >> M;
    for (int i=0; i<N; i++) {
        for (int j=0; j<N; j++) {
            adjmat[i][j] = INF;
        }
    }
    for (int i=0; i<M; i++) {
        int l, r, c; cin >> l >> r >> c;
        l--; r--;
        for (int s=l; s<r; s++) {
            for (int t=s+1; t<=r; t++) {
                if (adjmat[s][t] > c) {
                    adjmat[s][t] = c;
                    adjmat[t][s] = c;
                }
            }
        }
    }
    int ans = dijkstra();
    if (ans == INF) ans = -1;
    cout << ans << endl;
}