#include <bits/stdc++.h>
using namespace std;
struct dat {
  long a, b, c;
};
signed main() {
  long n, m;
  cin >> n >> m;
  vector<dat> g(m);
  for (long i = 0; i < m; ++i) {
    long a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    g[i].a = a, g[i].b = b, g[i].c = c;
  }
  bool ref = true;
  while (ref) {
    ref = false;
    sort(g.begin(), g.begin() + m, [&](dat p, dat q) {
      return q.a == n || p.a == q.a ? (p.b == q.b ? p.c < q.c : p.b < q.b)
                                    : p.a < q.a;
    });
    for (long i = 1; i < m && g[i].a < n; ++i) {
      if (g[i - 1].a != g[i].a) continue;
      ref = true;
      if (g[i - 1].c <= g[i].c) {
        g[i].a = g[i - 1].b;
        if (g[i].a >= g[i].b) {
          g[i].a = n, swap(g[i], g[(m--) - 1]);
        }
      } else
        g[i - 1].a = n, swap(g[i - 1], g[(m--) - 1]);
    }
  }
  vector<long> dp(n, -1);
  dp[0] = 0;
  for (long i = 0; i < m; ++i) {
    if (dp[g[i].a] < 0) return cout << -1, 0;
    if (dp[n - 1] > 0 && dp[g[i].a] + g[i].c > dp[n - 1])
      return cout << dp[n - 1], 0;
    for (long j = g[i].a + 1; j <= g[i].b; ++j) {
      if (dp[j] < 0)
        dp[j] = dp[g[i].a] + g[i].c;
      else
        dp[j] = min(dp[j], dp[g[i].a] + g[i].c);
    }
  }
  cout << dp[n - 1];
}
