#include <bits/stdc++.h>
using namespace std;
long long int lsone(long long int p) { return (p & -p); }
struct path {
  int l;
  int r;
  int c;
};
bool compareTwoPath(path a, path b) {
  if (a.l != b.l) {
    return a.l < b.l;
  } else if (a.r != b.r) {
    return a.r < b.r;
  } else
    return a.c < b.c;
}
int main() {
  std::ios::sync_with_stdio(false);
  int n, m, l, r, c;
  vector<int> line;
  vector<path> pathes;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    line.push_back(-1);
  }
  for (int i = 0; i < m; i++) {
    cin >> l >> r >> c;
    path p;
    p.l = l - 1;
    p.r = r - 1;
    p.c = c;
    pathes.push_back(p);
  }
  sort(pathes.begin(), pathes.end(), compareTwoPath);
  line[0] = 0;
  for (int i = 0; i < pathes.size(); i++) {
    path p = pathes[i];
    if (line[p.l] == -1) {
      cout << -1;
      return 0;
    } else {
      if (line[p.r] == -1 || line[p.l] + p.c < line[p.r]) {
        line[p.r] = line[p.l] + p.c;
        int j = p.r - 1;
        while (j > p.l && (line[j] == -1 || line[j] > line[p.r])) {
          line[j] = line[p.r];
          j -= 1;
        }
      }
    }
  }
  cout << line[n - 1];
}
