#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 7;
struct node {
  int x, y;
} r, w;
int vis[N];
int dis[N];
int inf = 100000000;
vector<node> v[N];
int n, m, x, y, z;
void push(int x, int y, int z) {
  r.x = y;
  r.y = z;
  v[x].push_back(r);
}
int slove() {
  fill(dis, dis + N, inf);
  vis[1] = 1;
  dis[1] = 0;
  int cnt = n;
  int pos = 1;
  for (int i = 0; i < v[1].size(); i++) {
    dis[v[1][i].x] = v[1][i].y;
  }
  while (cnt--) {
    w.y = inf;
    for (int i = 1; i <= n; i++) {
      if (vis[i]) continue;
      if (dis[i] < w.y) {
        pos = i;
        w.y = dis[i];
      }
    }
    vis[pos] = 1;
    for (int i = 0; i < v[pos].size(); i++) {
      r = v[pos][i];
      if (vis[r.x]) continue;
      dis[r.x] = min(dis[pos] + r.y, dis[r.x]);
    }
  }
  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() == inf)
    cout << "-1\n";
  else
    cout << dis[n] << '\n';
  return 0;
}
