#include <bits/stdc++.h>
using namespace std;
long long MOD = 998244353;
long long INF = 1000000000000000;
int pos[100010], usd[100010] = {};
long long d[100010];
int n, m, l[100010], r[100010];
long long c[100010];
pair<int, int> p[100010];
void dijkstra(int s) {
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      que;
  fill(d, d + 100010, INF);
  d[s] = 0;
  que.push(pair<long long, long long>(0, s));
  while (!que.empty()) {
    pair<long long, long long> q = que.top();
    que.pop();
    int v = q.second;
    int u = p[q.second].second;
    if (d[v] < q.first) continue;
    for (int i = pos[l[u]]; i <= pos[r[u]]; i++) {
      if (d[i] > d[v] + c[u]) {
        d[i] = d[v] + c[u];
        que.push(pair<long long, long long>(d[i], i));
      }
    }
  }
}
int main(void) {
  cin >> n >> m;
  for (int i = 0; i < int(m); i++) {
    cin >> l[i + 1] >> r[i + 1] >> c[i + 1];
    p[i + 1].first = l[i + 1];
    p[i + 1].second = i + 1;
  }
  sort(p + 1, p + (m + 1));
  p[m + 1].first = n;
  p[m + 1].second = m + 1;
  l[m + 1] = n;
  r[m + 1] = n;
  c[m + 1] = 0;
  p[0].first = 1;
  p[0].second = 1;
  l[0] = 1;
  r[0] = 1;
  c[0] = 0;
  m += 2;
  for (int i = 0; i < n + 10; i++) pos[i] = -9999;
  for (int i = m - 1; i >= 0; i--) {
    if (!usd[p[i].first]) {
      pos[p[i].first] = i;
      usd[p[i].first] = 1;
    }
  }
  int tr = -1;
  for (int i = 1; i <= n; i++) {
    if (pos[i] == -9999)
      pos[i] = tr;
    else
      tr = pos[i];
  }
  dijkstra(0);
  if (d[m - 1] != INF)
    cout << d[m - 1] << endl;
  else
    cout << -1 << endl;
}
