#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
using VB = vector<bool>;
using VVI = vector<VI>;
using VVL = vector<VL>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;
const int mod = 1e9 + 7, mod2 = 998244353, mod3 = 1e9 + 9;
const ll inf = 1e18;
const string wsp = " ";
const string tb = "\t";
const string rt = "\n";
template <typename T>
void show1dvec(const vector<T> &v) {
  if (v.size() == 0) return;
  int n = v.size() - 1;
  for (int i = 0; i < (int)(n); i++) cout << v[i] << wsp;
  cout << v[n] << rt;
  return;
}
template <typename T>
void show2dvec(const vector<vector<T>> &v) {
  int n = v.size();
  for (int i = 0; i < (int)(n); i++) show1dvec(v[i]);
}
template <typename T, typename S>
void show1dpair(const vector<pair<T, S>> &v) {
  int n = v.size();
  for (int i = 0; i < (int)(n); i++)
    cout << v[i].first << wsp << v[i].second << rt;
  return;
}
template <typename T, typename S>
void pairzip(const vector<pair<T, S>> &v, vector<T> &t, vector<T> &s) {
  int n = v.size();
  for (int i = 0; i < (int)(n); i++) {
    t.push_back(v[i].first);
    s.push_back(v[i].second);
  }
  return;
}
template <typename T>
void maxvec(vector<T> &v) {
  T s = v[0];
  int n = v.size();
  for (int i = 0; i < (int)(n - 1); i++) {
    if (s > v[i + 1]) {
      v[i + 1] = s;
    }
    s = v[i + 1];
  }
}
template <typename T, typename S>
bool myfind(T t, S s) {
  return find(t.cbegin(), t.cend(), s) != t.cend();
}
bool check(int y, int x, int h, int w) {
  return 0 <= y && y < h && 0 <= x && x < w;
}
template <typename T>
vector<T> cumsum(const vector<T> &v) {
  T s = 0;
  vector<T> ret;
  for (int i = 0; i < (int)(v.size()); i++) {
    s += v[i];
    s %= mod;
    ret.emplace_back(s);
  }
  return ret;
}
bool iskadomatsu(int a, int b, int c) {
  return (a != b && b != c && c != a) && ((a > b && b < c) || (a < b && b > c));
}
VS split(string s, char c) {
  VS ret;
  string part;
  s += c;
  for (int i = 0; i < (int)(s.length()); i++) {
    if (s[i] == c) {
      ret.emplace_back(part);
      part = "";
    } else if (s[i] != c) {
      part += s[i];
    }
  }
  return ret;
}
string removezero(string &s) {
  string ret;
  for (auto z : s)
    if (z != '0') ret += z;
  return ret;
}
ll sumdigit(ll x) {
  ll ans{0};
  while (x > 0) {
    ans += x % 10;
    x /= 10;
  }
  return ans;
}
template <typename T, typename S, typename R>
ll pow_mod(T p, S q, R mod = 1ll) {
  ll ret = 1, r = p;
  while (q) {
    if (q % 2) ret *= r, ret %= mod;
    r = (r * r) % mod, q /= 2;
  }
  return ret % mod;
}
ll pow_mod(int p, int q, int mod) {
  ll ret = 1, r = p;
  while (q) {
    if (q % 2) ret *= r, ret %= mod;
    r = (r * r) % mod, q /= 2;
  }
  return ret % mod;
}
class RangeMinorMaxorSumQuery {
  int const intmax = 2147483647;
  int const intmin = 0;
  vector<int> sgt;
  int n;
  int k;

 public:
  RangeMinorMaxorSumQuery(int n1, int f = -1) {
    if (f == -1)
      f = intmax;
    else if (f == 0)
      f = intmin;
    int na = 1;
    int ka = 0;
    while (na < n1) {
      na *= 2;
      ka++;
    }
    for (int i = 0; i < 2 * na; i++) sgt.push_back(f);
    n = na;
    k = ka;
  }
  void update_min(int i, int x) {
    i += n;
    sgt[i] = x;
    while (i > 1) {
      i /= 2;
      sgt[i] = min(sgt[2 * i], sgt[2 * i + 1]);
    }
  }
  void update_max(int i, int x) {
    i += n;
    sgt[i] = x;
    while (i > 1) {
      i /= 2;
      sgt[i] = max(sgt[2 * i], sgt[2 * i + 1]);
    }
  }
  int getmin(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r == -1) r = n;
    if (r <= a || b <= l) return intmax;
    if (a == l && b == r)
      return sgt[k];
    else
      return min(getmin(a, min(b, (l + r) / 2), 2 * k, l, (l + r) / 2),
                 getmin(max(a, (l + r) / 2), b, 2 * k + 1, (l + r) / 2, r));
  }
  int getmax(int a, int b, int k = 1, int l = 0, int r = -1) {
    if (r == -1) r = n;
    if (r <= a || b <= l) return intmin;
    if (a == l && b == r)
      return sgt[k];
    else
      return max(getmax(a, min(b, (l + r) / 2), 2 * k, l, (l + r) / 2),
                 getmax(max(a, (l + r) / 2), b, 2 * k + 1, (l + r) / 2, r));
  }
  void printsegtree() {
    for (int i = 0; i < 2 * n; i++) {
      cout << sgt[i] << " ";
    }
    cout << endl;
  }
};
void djkstra(int x, int n, const vector<vector<PII>> &connective) {
  const ll maxint = 1e16;
  vector<ll> shortest(n, maxint);
  vector<int> search = {x};
  vector<int> new_search;
  shortest[x] = 0;
  int v;
  ll d;
  while (!search.empty()) {
    for (auto i : search) {
      for (auto j : connective[i]) {
        v = j.first, d = j.second;
        if (shortest[v] > d + shortest[i]) {
          shortest[v] = d + shortest[i];
          new_search.emplace_back(v);
        }
      }
    }
    search.clear();
    search = new_search;
    new_search.clear();
  }
  cout << ((shortest[n - 1] == maxint) ? -1 : shortest[n - 1]) << rt;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m, l, r, c, k;
  cin >> n >> m;
  vector<vector<PII>> connect(n);
  VI backnode(n, 0), backnode2(n, 0);
  for (int i = 0; i < (int)(m); i++) {
    cin >> l >> r >> c;
    l--, r--;
    connect[l].emplace_back(make_pair(r, c));
    backnode[r]--;
    backnode[l]++;
  }
  k = 0;
  for (int i = 0; i < (int)(n); i++) {
    k += backnode[i];
    backnode2[i] += k;
  }
  for (int i = 0; i < (int)(n - 1); i++) {
    if (backnode[i]) {
      connect[i + 1].emplace_back(make_pair(i, 0));
    }
  }
  djkstra(0, n, connect);
  return 0;
}
