#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct Edge {
  long long to, cap, rev;
  Edge(long long _to, long long _cap, long long _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
void add_edge(vector<vector<Edge>>& G, long long from, long long to,
              long long cap, bool revFlag, long long revCap) {
  G[from].push_back(Edge(to, cap, (long long)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (long long)G[from].size() - 1));
}
void Dijkstra(vector<vector<Edge>>& G, long long s, vector<long long>& d) {
  d.resize(G.size());
  for (long long(i) = 0; (i) < (d.size()); (i)++) d[i] = INF;
  d[s] = 0;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  q.push(make_pair(0, s));
  while (!q.empty()) {
    pair<long long, long long> a = q.top();
    q.pop();
    if (d[a.second] < a.first) continue;
    for (long long(i) = 0; (i) < (G[a.second].size()); (i)++) {
      Edge e = G[a.second][i];
      if (d[e.to] > d[a.second] + e.cap) {
        d[e.to] = d[a.second] + e.cap;
        q.push(make_pair(d[e.to], e.to));
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long n, m;
  cin >> n >> m;
  vector<vector<Edge>> g(m + 2);
  vector<pair<pair<long long, long long>, long long>> lrc;
  for (long long(i) = 0; (i) < (m); (i)++) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    lrc.push_back(make_pair(make_pair(l, r), c));
  }
  sort((lrc).begin(), (lrc).end(),
       [&](pair<pair<long long, long long>, long long> x,
           pair<pair<long long, long long>, long long> y) {
         if (x.first == y.first) return x.second < y.second;
         if (x.first.first == y.first.first)
           return x.first.second > y.first.second;
         return x.first.first < y.first.first;
       });
  if (lrc[0].first.first != 0) {
    cout << 0 << endl;
  }
  for (int i = 0; i < m; i++) {
    long long l = lrc[i].first.first;
    long long r = lrc[i].first.second;
    long long c = lrc[i].second;
    if (l == 0) {
      add_edge(g, m, i, 0, false, 0);
    } else if (r == n - 1) {
      add_edge(g, i, m + 1, c, false, 0);
      continue;
    }
    for (int j = i + 1; j < m; j++) {
      long long lj = lrc[j].first.first;
      long long rj = lrc[j].first.second;
      long long cj = lrc[j].second;
      if (lj == l) continue;
      if (lj > r) break;
      add_edge(g, i, j, c, false, 0);
    }
  }
  vector<long long> d;
  Dijkstra(g, m, d);
  if (d[m + 1] == INF) {
    cout << -1 << endl;
  } else {
    cout << d[m + 1] << endl;
  }
  return 0;
}
