#include <bits/stdc++.h>
using namespace std;
using ll = long long;
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;
}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
const char EOL = '\n';
void solve();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(numeric_limits<double>::max_digits10);
  solve();
  return 0;
}
struct edge {
  int to;
  int cost;
};
using P = pair<int, int>;
const int MAX_V = 100100;
vector<edge> G[MAX_V];
vector<int> d(MAX_V, INF);
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) continue;
    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  int l, r, c;
  vector<vector<int>> z(n + 1, vector<int>(n + 1, INF));
  for (int i = (0); i < ((m)); i++) {
    cin >> l >> r >> c;
    l--;
    r--;
    for (int j = (l); j < (r + 1); j++) {
      for (int k = (l); k < (r + 1); k++) {
        if (j == k) continue;
        if (z[j][k] <= c) continue;
        z[j][k] = c;
      }
    }
  }
  for (int i = (0); i < ((n)); i++)
    for (int j = (0); j < ((n)); j++) {
      if (z[i][j] == INF) continue;
      edge e = {j, z[i][j]};
      G[i].push_back(e);
    }
  dijkstra(0);
  cout << (d[n - 1] == INF ? -1 : d[n - 1]) << EOL;
}
