#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e9 + 7;
const long long INF = 1e18;
const double PI = 2 * asin(1);
int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<pair<int, long long>, int> > LCR(M);
  for (int i = 0; i < M; i++) {
    int L, R;
    long long C;
    cin >> L >> R >> C;
    L--;
    R--;
    LCR[i].first = make_pair(L, C);
    LCR[i].second = R;
  }
  sort(LCR.begin(), LCR.end());
  vector<long long> ans(N);
  fill(ans.begin(), ans.end(), -1);
  vector<bool> visited(N);
  ans[0] = 0;
  visited[0] = true;
  int edge = 0;
  for (int i = 0; i < M; i++) {
    int L = LCR[i].first.first, R = LCR[i].second;
    long long C = LCR[i].first.second;
    if (visited[L] == false) {
      break;
    } else {
      long long compare = ans[L] + C;
      if (ans[min(R, edge)] > compare) {
        int l = L + 1, r = min(R, edge);
        bool flag = true;
        int now;
        while (flag == true) {
          now = (l + r) / 2;
          if (ans[now] > compare)
            l = now;
          else
            r = now;
          if ((l + r) / 2 == now) break;
        }
        for (int j = now; j <= min(R, edge); j++) {
          ans[j] = min(ans[j], compare);
        }
      }
      for (int j = edge + 1; j <= R; j++) {
        ans[j] = compare;
        visited[j] = true;
      }
      edge = max(edge, R);
    }
  }
  cout << ans[N - 1] << endl;
  return 0;
}
