#include <bits/stdc++.h>
using namespace std;
long long cost[10005][10005];
long long d[10005];
bool used[10005];
long long V;
void dijkstra(long long s) {
  fill(d, d + V, 10000000000);
  fill(used, used + V, false);
  d[s] = 0;
  while (true) {
    long long v = -1;
    for (long long u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
    }
    if (v == -1) break;
    used[v] = true;
    for (long long u = 0; u < V; u++) {
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}
int main(void) {
  long long n, m;
  cin >> n >> m;
  V = n;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      cost[i][j] = 10000000000;
    }
  }
  for (long long i = 0; i < m; i++) {
    long long t1, t2, c;
    cin >> t1 >> t2 >> c;
    t1--, t2--;
    for (long long j = t1; j < t2 + 1; j++) {
      for (long long k = t2; k >= t1; k--) {
        cost[j][k] = min(cost[j][k], c);
        cost[k][j] = min(cost[k][j], c);
      }
    }
  }
  dijkstra(0);
  if (d[n - 1] == 10000000000) {
    cout << -1 << endl;
  } else {
    cout << d[n - 1] << endl;
  }
  return 0;
}
