#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,inline,unroll-loops,fast-math")
using namespace std;
vector<pair<int, int> > e[100020 << 2];
int k[100020];
long long d[100020 << 2];
priority_queue<pair<int, long long>, vector<pair<int, long long> >,
               greater<pair<int, long long> > >
    q;
void InitSegment(int x, int l, int r) {
  int m;
  if (l == r) {
    k[l] = x;
    return;
  }
  m = (l + r) >> 1;
  InitSegment(x << 1, l, m);
  InitSegment(x << 1 | 1, m + 1, r);
  e[x << 1].push_back(make_pair(x, 0));
  e[x << 1 | 1].push_back(make_pair(x, 0));
  return;
}
void AddSegment(int x, int l, int r, int s, int t, int k, int w) {
  int m;
  if (l > t || r < s) return;
  if (l >= s && r <= t) {
    e[x].push_back(make_pair(k, w));
    return;
  }
  m = (l + r) >> 1;
  AddSegment(x << 1, l, m, s, t, k, w);
  AddSegment(x << 1 | 1, m + 1, r, s, t, k, w);
  return;
}
int main(void) {
  int n, m, l, r, w;
  int i;
  pair<int, long long> t;
  scanf("%d %d", &n, &m);
  InitSegment(1, 1, n);
  while (m--) {
    scanf("%d %d %d", &l, &r, &w);
    AddSegment(1, 1, n, l, r - 1, k[r], w);
  }
  for (i = 0; i < (100020 << 2); i++) d[i] = LLONG_MAX >> 2;
  d[k[1]] = 0;
  for (q.push(make_pair(k[1], 0)); !q.empty();) {
    t = q.top();
    q.pop();
    if (d[t.first] == t.second)
      for (i = 0; i < (signed)e[t.first].size(); i++)
        if (d[e[t.first].at(i).first] > t.second + e[t.first].at(i).second) {
          d[e[t.first].at(i).first] = t.second + e[t.first].at(i).second;
          q.push(make_pair(e[t.first].at(i).first, d[e[t.first].at(i).first]));
        }
  }
  printf("%lld\n", d[k[n]] == LLONG_MAX >> 2 ? -1 : d[k[n]]);
  return 0;
}
