#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000009;
long long d[10000][10000];
int n, m;
void warshall_floyd() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    }
  }
  if (d[0][n - 1] == INF)
    cout << "-1" << endl;
  else
    cout << d[0][n - 1] << endl;
  return;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = INF;
    }
  }
  for (int i = 0; i < m; i++) {
    int l, r;
    long long c;
    cin >> l >> r >> c;
    l--;
    r--;
    for (int j = l; j < r; j++) {
      for (int k = l + 1; k <= r; k++) {
        d[j][k] = min(c, d[j][k]);
        d[k][j] = min(c, d[k][j]);
      }
    }
  }
  warshall_floyd();
}
