#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")
#pragma GCC target("avx")
using namespace std;
const long long mod = 1000000007;
long long n, m;
vector<pair<pair<long long, long long>, long long>> q;
long long l, r, c;
class segment_tree_beats {
  typedef struct {
    long long max;
    long long max_second;
    long long max_count;
    long long min;
    long long min_second;
    long long min_count;
    long long lazy_add;
    long long lazy_update;
    long long sum;
  } value_type;
  long long n;
  std::vector<value_type> a;

 public:
  segment_tree_beats() = default;
  segment_tree_beats(long long n_) {
    n = 1;
    while (n < n_) n *= 2;
    a.resize(2 * n - 1);
    tag<UPDATE>(0, 0);
  }
  template <class InputIterator>
  segment_tree_beats(InputIterator first, InputIterator last) {
    long long n_ = std::distance(first, last);
    n = 1;
    while (n < n_) n *= 2;
    a.resize(2 * n - 1);
    for (long long i = 0; i < (long long)(n_); i++) {
      tag<UPDATE>(n - 1 + i, *(first + i));
    }
    for (long long i = (long long)(n_); i < (n); i++) {
      tag<UPDATE>(n - 1 + i, 0);
    }
    for (long long i = (long long)(n - 1) - 1; i >= 0; i--) {
      update(i);
    }
  }
  void range_chmin(long long l, long long r, long long value) {
    assert(0 <= l and l <= r and r <= n);
    range_apply<CHMIN>(0, 0, n, l, r, value);
  }
  void range_chmax(long long l, long long r, long long value) {
    assert(0 <= l and l <= r and r <= n);
    range_apply<CHMAX>(0, 0, n, l, r, value);
  }
  void range_add(long long l, long long r, long long value) {
    assert(0 <= l and l <= r and r <= n);
    range_apply<ADD>(0, 0, n, l, r, value);
  }
  void range_update(long long l, long long r, long long value) {
    assert(0 <= l and l <= r and r <= n);
    range_apply<UPDATE>(0, 0, n, l, r, value);
  }
  long long range_min(long long l, long long r) {
    assert(0 <= l and l <= r and r <= n);
    return range_get<MIN>(0, 0, n, l, r);
  }
  long long range_max(long long l, long long r) {
    assert(0 <= l and l <= r and r <= n);
    return range_get<MAX>(0, 0, n, l, r);
  }
  long long range_sum(long long l, long long r) {
    assert(0 <= l and l <= r and r <= n);
    return range_get<SUM>(0, 0, n, l, r);
  }

 private:
  static constexpr char CHMIN = 0;
  static constexpr char CHMAX = 1;
  static constexpr char ADD = 2;
  static constexpr char UPDATE = 3;
  static constexpr char MIN = 10;
  static constexpr char MAX = 11;
  static constexpr char SUM = 12;
  template <char TYPE>
  void range_apply(long long i, long long il, long long ir, long long l,
                   long long r, long long g) {
    if (ir <= l or r <= il or break_condition<TYPE>(i, g)) {
    } else if (l <= il and ir <= r and tag_condition<TYPE>(i, g)) {
      tag<TYPE>(i, g);
    } else {
      pushdown(i);
      range_apply<TYPE>(2 * i + 1, il, (il + ir) / 2, l, r, g);
      range_apply<TYPE>(2 * i + 2, (il + ir) / 2, ir, l, r, g);
      update(i);
    }
  }
  template <char TYPE>
  inline bool break_condition(long long i, long long g) {
    switch (TYPE) {
      case CHMIN:
        return a[i].max <= g;
      case CHMAX:
        return g <= a[i].min;
      case ADD:
        return false;
      case UPDATE:
        return false;
      default:
        assert(false);
    }
  }
  template <char TYPE>
  inline bool tag_condition(long long i, long long g) {
    switch (TYPE) {
      case CHMIN:
        return a[i].max_second < g and g < a[i].max;
      case CHMAX:
        return a[i].min < g and g < a[i].min_second;
      case ADD:
        return true;
      case UPDATE:
        return true;
      default:
        assert(false);
    }
  }
  template <char TYPE>
  inline void tag(long long i, long long g) {
    long long length = n >> (64 - __builtin_clz(i + 1) - 1);
    if (TYPE == CHMIN) {
      if (a[i].max == a[i].min or g <= a[i].min) {
        tag<UPDATE>(i, g);
        return;
      }
      if (a[i].max != INT64_MIN) {
        a[i].sum -= a[i].max * a[i].max_count;
      }
      a[i].max = g;
      a[i].min_second = std::min(a[i].min_second, g);
      if (a[i].lazy_update != INT64_MAX) {
        a[i].lazy_update = std::min(a[i].lazy_update, g);
      }
      a[i].sum += g * a[i].max_count;
    } else if (TYPE == CHMAX) {
      if (a[i].max == a[i].min or a[i].max <= g) {
        tag<UPDATE>(i, g);
        return;
      }
      if (a[i].min != INT64_MAX) {
        a[i].sum -= a[i].min * a[i].min_count;
      }
      a[i].min = g;
      a[i].max_second = std::max(a[i].max_second, g);
      if (a[i].lazy_update != INT64_MAX) {
        a[i].lazy_update = std::max(a[i].lazy_update, g);
      }
      a[i].sum += g * a[i].min_count;
    } else if (TYPE == ADD) {
      if (a[i].max != INT64_MAX) {
        a[i].max += g;
      }
      if (a[i].max_second != INT64_MIN) {
        a[i].max_second += g;
      }
      if (a[i].min != INT64_MIN) {
        a[i].min += g;
      }
      if (a[i].min_second != INT64_MAX) {
        a[i].min_second += g;
      }
      a[i].lazy_add += g;
      if (a[i].lazy_update != INT64_MAX) {
        a[i].lazy_update += g;
      }
      a[i].sum += g * length;
    } else if (TYPE == UPDATE) {
      a[i].max = g;
      a[i].max_second = INT64_MIN;
      a[i].max_count = length;
      a[i].min = g;
      a[i].min_second = INT64_MAX;
      a[i].min_count = length;
      a[i].lazy_add = 0;
      a[i].lazy_update = INT64_MAX;
      a[i].sum = g * length;
    } else {
      assert(false);
    }
  }
  void pushdown(long long i) {
    long long l = 2 * i + 1;
    long long r = 2 * i + 2;
    if (a[i].lazy_update != INT64_MAX) {
      tag<UPDATE>(l, a[i].lazy_update);
      tag<UPDATE>(r, a[i].lazy_update);
      a[i].lazy_update = INT64_MAX;
      return;
    }
    if (a[i].lazy_add != 0) {
      tag<ADD>(l, a[i].lazy_add);
      tag<ADD>(r, a[i].lazy_add);
      a[i].lazy_add = 0;
    }
    if (a[i].max < a[l].max) {
      tag<CHMIN>(l, a[i].max);
    }
    if (a[i].max < a[r].max) {
      tag<CHMIN>(r, a[i].max);
    }
    if (a[l].min < a[i].min) {
      tag<CHMAX>(l, a[i].min);
    }
    if (a[r].min < a[i].min) {
      tag<CHMAX>(r, a[i].min);
    }
  }
  void update(long long i) {
    long long l = 2 * i + 1;
    long long r = 2 * i + 2;
    std::vector<long long> b{a[l].max, a[l].max_second, a[r].max,
                             a[r].max_second};
    std::sort(b.rbegin(), b.rend());
    b.erase(std::unique((b).begin(), (b).end()), b.end());
    a[i].max = b[0];
    a[i].max_second = b[1];
    a[i].max_count = (b[0] == a[l].max ? a[l].max_count : 0) +
                     (b[0] == a[r].max ? a[r].max_count : 0);
    std::vector<long long> c{a[l].min, a[l].min_second, a[r].min,
                             a[r].min_second};
    std::sort((c).begin(), (c).end());
    c.erase(std::unique((c).begin(), (c).end()), c.end());
    a[i].min = c[0];
    a[i].min_second = c[1];
    a[i].min_count = (c[0] == a[l].min ? a[l].min_count : 0) +
                     (c[0] == a[r].min ? a[r].min_count : 0);
    a[i].lazy_add = 0;
    a[i].lazy_update = INT64_MAX;
    a[i].sum = a[l].sum + a[r].sum;
  }
  template <char TYPE>
  long long range_get(long long i, long long il, long long ir, long long l,
                      long long r) {
    if (ir <= l or r <= il) {
      return 0;
    } else if (l <= il and ir <= r) {
      switch (TYPE) {
        case MIN:
          return a[i].min;
        case MAX:
          return a[i].max;
        case SUM:
          return a[i].sum;
        default:
          assert(false);
      }
    } else {
      pushdown(i);
      long long value_l = range_get<TYPE>(2 * i + 1, il, (il + ir) / 2, l, r);
      long long value_r = range_get<TYPE>(2 * i + 2, (il + ir) / 2, ir, l, r);
      switch (TYPE) {
        case MIN:
          return std::min(value_l, value_r);
        case MAX:
          return std::max(value_l, value_r);
        case SUM:
          return value_l + value_r;
        default:
          assert(false);
      }
    }
  }
};
signed main() {
  scanf("%lld", &(n));
  scanf("%lld", &(m));
  for (long long i = 0; i < (long long)(m); i++) {
    scanf("%lld", &(l));
    scanf("%lld", &(r));
    scanf("%lld", &(c));
    l--;
    r--;
    q.emplace_back((make_pair((l), (c))), (r));
  }
  long long inf = 1145141919810931LL;
  segment_tree_beats sb(n);
  sb.range_chmax(0, n, inf);
  sb.range_update(0, 1, 0);
  sort((q).begin(), (q).end());
  for (auto p : q) {
    l = p.first.first;
    c = p.first.second;
    r = p.second;
    long long lc = sb.range_max(l, l + 1);
    sb.range_chmin(l + 1, r + 1, lc + c);
  }
  long long ret = sb.range_max(n - 1, n);
  cout << (ret == inf ? -1 : ret) << endl;
}
