#include <bits/stdc++.h>
using namespace std;
const int N = 4e6 + 7;
struct node {
  int to, cost;
} r;
struct que {
  int x, y;
  bool friend operator<(que a, que b) { return a.x > b.x; }
} w;
int vis[N];
int dis[N];
vector<node> v[N];
int n, m, x, y, z;
void push(int x, int y, int z) {
  r.to = y;
  r.cost = z;
  v[x].push_back(r);
}
int inf = 2147483647;
priority_queue<que> q;
int slove(int x) {
  fill(dis, dis + N, inf);
  w.x = 0;
  w.y = 1;
  dis[1] = 0;
  q.push(w);
  while (!q.empty()) {
    w = q.top();
    q.pop();
    int pos = w.y;
    if (dis[pos] < w.x) continue;
    for (int i = 0; i < v[pos].size(); i++) {
      node e = v[pos][i];
      if (dis[e.to] > dis[pos] + e.cost) {
        dis[e.to] = dis[pos] + e.cost;
        w.x = dis[e.to];
        w.y = e.to;
        q.push(w);
      }
    }
  }
  return dis[n];
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    push(i, i - 1, 0);
  }
  while (m--) {
    cin >> x >> y >> z;
    push(x, y, z);
  }
  if (slove(1) == inf)
    cout << "-1\n";
  else
    cout << dis[n] << '\n';
  return 0;
}
