#include <bits/stdc++.h>
using namespace std;
int head[100000 + 5], arrive[100000 << 1 | 5], nxt[100000 << 1 | 5],
    val[100000 << 1 | 5], tot;
void add_edge(int from, int to, int w) {
  arrive[++tot] = to;
  nxt[tot] = head[from];
  val[tot] = w;
  head[from] = tot;
}
long long dis[100000 + 5];
queue<int> q;
bool in[100000 + 5];
void spfa() {
  memset(dis, 0x3f, sizeof dis);
  dis[1] = 0;
  q.push(1);
  in[1] = 1;
  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    in[u] = 0;
    for (int i = head[u]; i; i = nxt[i]) {
      int v = arrive[i];
      if (dis[u] + val[i] < dis[v]) {
        dis[v] = dis[u] + val[i];
        if (!in[v]) {
          q.push(v);
          in[v] = 1;
        }
      }
    }
  }
}
int main() {
  int n, m;
  int l, r, c;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &l, &r, &c);
    add_edge(l, r, c);
  }
  for (int i = 1; i < n; i++) {
    add_edge(i + 1, i, 0);
  }
  spfa();
  cout << (dis[n] == 0x3f3f3f3f3f3f3f3fll ? -1 : dis[n]) << endl;
  return 0;
}
