#include <bits/stdc++.h>
using namespace std;
const long long INF = 1ll << 60;
struct edge {
  int to;
  long long cost;
} temp;
int V;
vector<edge> G[30010];
long long d[30010];
int used[30010];
void dij() {
  while (true) {
    int p = 0;
    for (int i = 1; i <= V; i++) {
      if (d[i] < d[p] && used[i] == 0) p = i;
    }
    if (p == 0) break;
    used[p] = 1;
    for (int i = 0; i < G[p].size(); i++) {
      d[G[p][i].to] = min(d[G[p][i].to], d[p] + G[p][i].cost);
    }
  }
}
int main() {
  int n;
  scanf("%d%d", &V, &n);
  for (int i = 0; i < n; i++) {
    edge temp;
    int from;
    scanf("%d%d%lld", &from, &temp.to, &temp.cost);
    for (int j = temp.to - 1; j >= from; j--) {
      edge tempp;
      tempp.to = j;
      tempp.cost = 0;
      G[temp.to].push_back(tempp);
    }
    G[from].push_back(temp);
  }
  for (int i = 0; i <= V; i++) d[i] = INF;
  memset(used, 0, sizeof(used));
  for (int i = 0; i < G[1].size(); i++) {
    d[G[1][i].to] = G[1][i].cost;
  }
  d[1] = 0;
  dij();
  if (d[V] != INF)
    printf("%lld\n", d[V]);
  else
    cout << -1 << endl;
}
