#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000009;
long long d[10000];
long long cost[10000][10000];
bool used[10000];
int n, m;
int dijkstra() {
  fill(d, d + n, INF);
  fill(used, used + n, false);
  d[0] = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < n; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) {
        v = u;
      }
    }
    if (v == -1) break;
    used[v] = true;
    for (int u = 0; u < n; u++) {
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
  return d[n - 1];
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cost[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++) {
        cost[j][k] = min(c, cost[j][k]);
        cost[k][j] = min(c, cost[k][j]);
      }
    }
  }
  int res = dijkstra();
  if (res == INF) {
    cout << "-1" << endl;
    return 0;
  }
  cout << res << endl;
}
