#include <bits/stdc++.h>
using namespace std;
template <class T>
void printvec(const vector<T>& v) {
  for (auto x : v) {
    cout << x << " ";
  }
  cout << endl;
}
template <class T>
void printtree(const vector<vector<T> >& tree) {
  for (long long i = 0; i < tree.size(); ++i) {
    cout << i + 1 << ": ";
    printvec(tree[i]);
  }
}
template <class T, class U>
void printmap(const map<T, U>& mp) {
  for (auto x : mp) {
    cout << x.first << "=>" << x.second << endl;
  }
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1e9;
const long long MOD = 1000000007;
int main(int argc, char** argv) {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  long long n, m;
  cin >> n >> m;
  vector<vector<long long> > graph(m);
  vector<long long> s;
  vector<long long> t;
  vector<tuple<long long, long long, long long> > ops;
  for (long long i = 0; i < m; ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    ops.emplace_back(l, c, r);
  }
  sort(ops.begin(), ops.end());
  set<pair<long long, long long> > rs;
  for (long long i = 0; i < m; ++i) {
    long long l, r, c;
    tie(l, c, r) = ops[i];
    if (i == 0 && l != 1) {
      cout << -1 << endl;
      return 0;
    }
    if (l == 1) {
      auto it = rs.upper_bound(make_pair(r, -1LL));
      if (it == rs.end()) {
        rs.emplace(r, c);
      } else {
        if (r == (*it).first) {
          if (c < (*it).second) {
            rs.erase(it);
            rs.emplace(r, c);
          }
        } else {
          if (c < (*it).second) {
            rs.emplace(r, c);
          }
        }
      }
    } else {
      auto it = rs.lower_bound(make_pair(l, -1));
      if (it == rs.end()) {
        cout << -1 << endl;
        return 0;
      } else {
        long long new_c = (*it).second + c;
        auto rit = rs.upper_bound(make_pair(r, -1LL));
        if (rit == rs.end()) {
          rs.emplace(r, new_c);
        } else {
          if (r == (*rit).first) {
            if (new_c < (*rit).second) {
              rs.erase(rit);
              rs.emplace(r, new_c);
            }
          } else {
            if (new_c < (*rit).second) {
              rs.emplace(r, new_c);
            }
          }
        }
      }
    }
  }
  if (rs.size() > 0) {
    auto it = rs.lower_bound(make_pair(n, -1));
    if (it == rs.end()) {
      cout << -1 << endl;
      return 0;
    } else {
      long long r, c;
      tie(r, c) = *it;
      cout << c << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
