#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = N * 2;
long long h[N], st[N], dist[N], cnt, n, m;
const long long inf = 0x3f3f3f3f3f3f3f3f;
struct node {
  long long to, nxt, w;
} eg[M];
void add(int u, int v, int w) {
  eg[cnt].w = w;
  eg[cnt].to = v;
  eg[cnt].nxt = h[u];
  h[u] = cnt++;
}
priority_queue<long long, vector<long long>, greater<long long> > q;
long long dijkstra(long long u) {
  memset(st, 0, sizeof(st));
  for (int i = 1; i <= n; i++) dist[i] = inf;
  dist[u] = 0;
  q.push(u);
  while (!q.empty()) {
    long long u = q.top();
    q.pop();
    if (st[u]) continue;
    st[u] = 1;
    for (long long i = h[u]; i != -1; i = eg[i].nxt) {
      long long v = eg[i].to;
      if (st[v]) continue;
      if (dist[v] > dist[u] + eg[i].w) {
        dist[v] = dist[u] + eg[i].w;
        q.push(v);
      }
    }
  }
  if (dist[n] == inf) return -1;
  return dist[n];
}
int main() {
  memset(h, -1, sizeof(h));
  cnt = 0;
  cin >> n >> m;
  for (int i = 2; i <= n; i++) add(i, i - 1, 0);
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    add(u, v, w);
  }
  cout << dijkstra(1) << endl;
  return 0;
}
