#include <bits/stdc++.h>
using namespace std;
long long mod(long long a) {
  long long c = a % 998244353;
  if (c >= 0) return c;
  return c + 998244353;
}
long long inf = (long long)1E15;
long long max(long long a, long long b) {
  if (a < b)
    return b;
  else
    return a;
}
long long min(long long a, long long b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(long long &pos, long long val) { pos = max(pos, val); }
void Min(long long &pos, long long val) { pos = min(pos, val); }
void Add(long long &pos, long long val) { pos = mod(pos + val); }
long double EPS = 1E-9;
struct len {
  long long l, r, c;
};
bool comp(len a, len b) { return a.r < b.r; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  len a[m];
  for (long long i = 0; i <= m - 1; i++) {
    len b;
    cin >> b.l >> b.r >> b.c;
    a[i] = b;
  }
  sort(a, a + m, comp);
  long long dp[n + 1];
  for (long long i = 0; i <= n; i++) dp[i] = inf;
  dp[1] = 0;
  long long lasr = 1;
  long long pos = 0;
  while (pos <= m - 1) {
    vector<len> v;
    v.push_back(a[pos]);
    while (pos + 1 <= m - 1 && a[pos].r == a[pos + 1].r) {
      v.push_back(a[pos + 1]);
      pos++;
    }
    for (auto x : v) {
      Min(dp[x.r], dp[x.l] + x.c);
    }
    for (long long i = lasr + 1; i <= v[0].r; i++) {
      dp[i] = dp[v[0].r];
    }
    lasr = v[0].r;
    pos++;
  }
  if (dp[n] == inf)
    cout << -1 << endl;
  else
    cout << dp[n] << endl;
  return 0;
}
