#include <bits/stdc++.h>
using namespace std;
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
const int N = 1e5;
int n, m;
set<pair<pair<int, int>, int>> st;
struct node {
  int u;
  long long val;
  long long lazy;
  node() {
    u = 0;
    val = 0;
    lazy = 1e18;
  }
} seg[4 * N];
void fix(int c, int l, int r) {
  seg[c].val = min(seg[c].val, seg[c].lazy);
  if (l != r) {
    seg[2 * c + 1].lazy = min(seg[2 * c + 1].lazy, seg[c].lazy);
    seg[2 * c + 2].lazy = min(seg[2 * c + 2].lazy, seg[c].lazy);
  }
  seg[c].lazy = 1e18;
}
pair<long long, int> get(int c, int l, int r, int s, int e) {
  fix(c, l, r);
  if (e < l || r < s) return {1e18, -1};
  if (s <= l && r <= e) return {seg[c].val, seg[c].u};
  pair<long long, int> x = get(2 * c + 1, l, (l + r) / 2, s, e);
  pair<long long, int> y = get(2 * c + 2, (l + r) / 2 + 1, r, s, e);
  if (x.first == -1) x.first = 1e18;
  if (y.first == -1) y.first = 1e18;
  return min(x, y);
}
void update(int c, int l, int r, int s, int e, long long val) {
  fix(c, l, r);
  if (e < l || r < s) return;
  if (s <= l && r <= e) {
    seg[c].lazy = val;
    fix(c, l, r);
    return;
  }
  update(2 * c + 1, l, (l + r) / 2, s, e, val);
  update(2 * c + 2, (l + r) / 2 + 1, r, s, e, val);
  pair<long long, int> x = {seg[2 * c + 1].val, seg[2 * c + 1].u};
  pair<long long, int> y = {seg[2 * c + 2].val, seg[2 * c + 2].u};
  if (x.first == -1) x.first = 1e18;
  if (y.first == -1) y.first = 1e18;
  if (x.first < y.first) {
    seg[c].val = x.first;
    seg[c].u = x.second;
  } else {
    seg[c].val = y.first;
    seg[c].u = y.second;
  }
}
void print(int c, int l, int r) {
  cout << "from " << l << " to " << r << endl;
  cout << seg[c].u << " " << seg[c].val << endl;
  if (l == r) return;
  print(2 * c + 1, l, (l + r) / 2);
  print(2 * c + 2, (l + r) / 2 + 1, r);
}
void dijk() {
  while (1) {
    pair<long long, int> temp = get(0, 0, n - 1, 0, n - 1);
    long long dis = temp.first;
    int u = temp.second;
    if (dis == 1e18) return;
    if (u == n - 1) {
      cout << dis << endl;
      exit(0);
    }
    int lol = 0;
    while (1) {
      auto it = st.lower_bound({{u, -1}, -1});
      if (it == st.end()) break;
      if (it->first.second > u) break;
      int l = it->first.second;
      int r = it->first.first;
      int c = it->second;
      update(0, 0, n - 1, l, r, dis + c);
      st.erase(it);
    }
    update(0, 0, n - 1, u, u, -1);
  }
}
void build(int c, int l, int r) {
  if (l == r) {
    seg[c].u = l;
    seg[c].val = 1e18;
    if (!l) seg[c].val = 0;
    return;
  }
  build(2 * c + 1, l, (l + r) / 2);
  build(2 * c + 2, (l + r) / 2 + 1, r);
  pair<long long, int> x = {seg[2 * c + 1].val, seg[2 * c + 1].u};
  pair<long long, int> y = {seg[2 * c + 2].val, seg[2 * c + 1].u};
  if (x.first == -1) x.first = 1e18;
  if (y.first == -1) y.first = 1e18;
  if (x.first < y.first) {
    seg[c].val = x.first;
    seg[c].u = x.second;
  } else {
    seg[c].val = y.first;
    seg[c].u = y.second;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  build(0, 0, n - 1);
  for (int i = 0, l, r, c; i < m; i++) {
    scanf("%d%d%d", &l, &r, &c);
    st.insert({{r - 1, l - 1}, c});
  }
  dijk();
  puts("-1");
}
