#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;
}
template <typename T>
vector<vector<T>> pre_warshall_floyd(int n, T inf) {
  vector<vector<T>> g(n, vector<T>(n, inf));
  for (int i = (0); i < ((n)); i++) g[i][i] = 0;
  return g;
}
template <typename T>
void warshall_floyd(vector<vector<T>>& g) {
  int n = (int)g.size();
  for (int k = (0); k < ((n)); k++)
    for (int i = (0); i < ((n)); i++)
      for (int j = (0); j < ((n)); j++) chmin(g[i][j], g[i][k] + g[k][j]);
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g = pre_warshall_floyd(n, INF);
  int l, r, c;
  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;
        g[j][k] = min(c, g[j][k]);
      }
    }
  }
  warshall_floyd(g);
  cout << (g[0][n - 1] == INF ? -1 : g[0][n - 1]) << EOL;
}
