#include <bits/stdc++.h>
using namespace std;
struct Segtree {
  int data[2 * 131072 - 1];
  Segtree(vector<int> v) {
    int n = 131072;
    for (int i = 0; i < 2 * n - 1; i++) data[i] = 1000000000;
    data[n - 1] = 0;
    for (int i = n - 2; i >= 0; i--)
      data[i] = min(data[2 * i + 1], data[2 * i + 2]);
  }
  void change(int id, int val) {
    int i = id + 131072 - 1;
    data[i] = val;
    i = i / 2;
    while (1) {
      data[i] = min(data[2 * i + 1], data[2 * i + 2]);
      if (i == 0) break;
      i = i / 2;
    }
  }
  int find(int a, int b, int k = 0, int l = 0, int r = 131072) {
    if (a <= l && r <= b) return data[k];
    if (b <= l || r <= a)
      return 1000000000;
    else {
      return min(find(a, b, 2 * k + 1, l, (l + r) / 2),
                 find(a, b, 2 * k + 2, (l + r) / 2, r));
    }
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(n);
  for (auto x : v) x = 1000000000;
  v[0] = 0;
  Segtree segt(v);
  vector<pair<pair<int, int>, int>> g(m);
  for (int i = 0; i < m; i++) {
    cin >> g[i].first.first >> g[i].first.second >> g[i].second;
  }
  sort(g.begin(), g.end());
  for (int i = 0; i < m; i++) {
    int l = g[i].first.first - 1, r = g[i].first.second, c = g[i].second;
    int mc = segt.find(l, r - 1);
    int minc = segt.find(r - 1, r);
    segt.change(r - 1, min(minc, mc + c));
  }
  if (segt.find(n - 1, n) == 1000000000)
    cout << -1 << endl;
  else
    cout << segt.find(n - 1, n) << endl;
  return 0;
}
