#include <bits/stdc++.h>
using namespace std;
const int CN = 1e5 + 5;
const long long INF = 0x3f3f3f3f3f3f3f2f;
long long read() {
  long long s = 0, ne = 1;
  char c = getchar();
  for (; c < '0' || c > '9'; c = getchar())
    if (c == '-') ne = -1;
  for (; c >= '0' && c <= '9'; c = getchar()) s = (s << 1) + (s << 3) + c - '0';
  return s * ne;
}
class fs {
 public:
  int to, nxt;
  long long di;
  void init(int t, int n, long long d) {
    to = t;
    nxt = n;
    di = d;
  }
} E[CN * 51];
int hd[CN], ecnt = 0;
void add(int x, int y, long long z) {
  E[++ecnt].init(y, hd[x], z);
  hd[x] = ecnt;
}
int n, m;
class DJ {
 public:
  int id;
  long long v;
  bool operator<(const DJ& a) const { return v > a.v; }
};
priority_queue<DJ> Q;
long long d[CN];
bool vis[CN];
long long SP(int st, int ed) {
  memset(vis, 0, sizeof(vis));
  memset(d, 0x3f, sizeof(d));
  Q.push((DJ){st, d[st] = 0});
  while (!Q.empty()) {
    int u = Q.top().id;
    Q.pop();
    for (int k = hd[u]; k; k = E[k].nxt) {
      int v = E[k].to;
      if (d[v] > d[u] + E[k].di) {
        d[v] = d[u] + E[k].di;
        if (!vis[v]) Q.push((DJ){v, d[v]}), vis[v] = true;
      }
    }
  }
  return d[ed] < INF ? d[ed] : -1;
}
int main() {
  n = read();
  m = read();
  while (m--) {
    int u = read(), v = read();
    long long c = read();
    add(u, v, c);
  }
  for (int i = 1; i < n; i++) add(i + 1, i, 0);
  printf("%lld", SP(1, n));
}
