#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const long long inf = 1e18;
int n, m;
struct node {
  int l, r, c;
  bool operator<(const node& b) const { return l < b.l; }
  bool operator<(int x) const { return l < x; }
} p[maxn];
long long dist[maxn], ans = inf;
int inQ[maxn];
queue<int> Q;
int s[maxn], e[maxn];
inline void bfs() {
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    inQ[x] = 0;
    if (p[x].r == n) {
      ans = min(ans, dist[x]);
      continue;
    }
    int s = lower_bound(p + 1, p + m + 1, p[x].l) - p;
    int e = lower_bound(p + 1, p + m + 1, p[x].r + 1) - p;
    for (int i = s; i < e; i++) {
      if (p[i].r > p[x].r && dist[i] > dist[x] + p[i].c) {
        if (!inQ[i]) {
          Q.push(i);
          inQ[i] = 1;
        }
        dist[i] = dist[x] + p[i].c;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> p[i].l >> p[i].r >> p[i].c;
    dist[i] = inf;
  }
  sort(p + 1, p + m + 1);
  for (int i = 1; i <= m; i++) {
    if (p[i].l == 1) {
      Q.push(i);
      inQ[i] = 1;
      dist[i] = p[i].c;
    }
  }
  bfs();
  if (ans == inf)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
