#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  int n, m;
  cin >> n >> m;
  const int MAX_N = 100005;
  const long long INF = 1001001001;
  vector<vector<long long>> g(n, vector<long long>(n, INF));
  for (int i = 0; i < (m); i++) {
    long long l, r, c;
    cin >> l >> r >> c;
    for (int s = l; s <= r - 1; s++) {
      for (int t = l + 1; t <= r; t++) {
        g[s - 1][t - 1] = min(g[s - 1][t - 1], c);
        g[t - 1][s - 1] = min(g[t - 1][s - 1], c);
      }
    }
  }
  for (int i = 0; i < (n); i++) {
    g[i][i] = 0;
  }
  for (int k = 0; k < (n); k++)
    for (int i = 0; i < (n); i++)
      for (int j = 0; j < (n); j++) {
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
      }
  long long ans = g[0][n - 1];
  if (ans == INF) ans = -1;
  cout << ans << endl;
}
