#include <bits/stdc++.h>
using namespace std;
template <typename T>
string to_string(const vector<T> &xs) {
  string s = "[";
  for (int i = 0; i < xs.size(); i++)
    s += (i > 0 ? " " : "") + to_string(xs[i]);
  return s + "]";
}
long long int extgcd(long long int a, long long int b, long long int &x,
                     long long int &y) {
  long long int g = a;
  x = 1;
  y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
long long int gcd(long long int a, long long int b) {
  long long int x, y;
  return extgcd(a, b, x, y);
}
class llm {
  long long int n;

 public:
  llm() { n = 0; }
  llm(long long int n) {
    this->n =
        (n >= 0) ? (n % 1000000007) : (1000000007 - 1 - (-n - 1 % 1000000007));
  }
  operator long long int() const { return n; }
  llm operator+(const llm &y) const {
    return llm((long long int)*this + (long long int)y);
  }
  llm operator+=(const llm &y) {
    *this = *this + y;
    return *this;
  }
  llm operator-(const llm &y) const {
    return llm((long long int)*this - (long long int)y);
  }
  llm operator-=(const llm &y) {
    *this = *this - y;
    return *this;
  }
  llm operator-() const { return llm(0) - *this; }
  llm operator*(const llm &y) const {
    return llm((long long int)*this * (long long int)y);
  }
  llm operator*=(const llm &y) {
    *this = *this * y;
    return *this;
  }
  llm operator/(const llm &y) const { return *this * y.inv(); }
  llm operator/=(const llm &y) {
    *this = *this / y;
    return *this;
  }
  llm inv() const {
    long long int x, y;
    if (extgcd(n, 1000000007, x, y) == 1) return llm(x);
    return llm(0);
  }
  friend istream &operator>>(istream &os, llm &x);
  friend ostream &operator<<(ostream &os, const llm &x);
};
istream &operator>>(istream &is, llm &x) {
  long long int n;
  is >> n;
  x = llm(n);
  return is;
}
ostream &operator<<(ostream &os, const llm &x) {
  os << (long long int)x;
  return os;
}
template <typename T>
T comb(long long int n, long long int k) {
  T ret = 1;
  for (long long int i = 1; i <= k; i++) ret = ret * T(n - k + i) / T(i);
  return ret;
}
class MinSegTree {
  vector<long long int> data;
  int baseSize, baseOffset;

 public:
  MinSegTree(vector<long long int> &ary) {
    baseSize = 1;
    for (;; baseSize *= 2) {
      if (ary.size() <= baseSize) break;
    }
    baseOffset = baseSize - 1;
    data = vector<long long int>(baseSize * 2, LLONG_MAX);
    for (int i = 0; i < ary.size(); i++) data[baseOffset + i] = ary[i];
    for (int i = baseOffset - 1; i >= 0; i--) {
      data[i] = min(data[2 * i + 1], data[2 * i + 2]);
    }
  }
  long long int getMin(int s, int e, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = baseSize;
    long long int ret;
    if (e <= l || r <= s)
      ret = LLONG_MAX;
    else if (s <= l && r <= e)
      ret = data[k];
    else
      ret = min(getMin(s, e, k * 2 + 1, l, (l + r) / 2),
                getMin(s, e, k * 2 + 2, (l + r) / 2, r));
    return ret;
  }
  void update(int idx, long long int val) {
    idx += baseOffset;
    data[idx] = val;
    while (idx > 0) {
      idx = (idx - 1) / 2;
      data[idx] = min(data[idx * 2 + 1], data[idx * 2 + 2]);
    }
    return;
  }
  long long int at(int idx) { return data[idx + baseOffset]; }
};
long long int n, m;
long long int l[114514], r[114514], c[114514];
int main(void) {
  cin >> n >> m;
  vector<vector<long long int>> edges;
  for (int i = 0; i < m; i++) {
    cin >> l[i] >> r[i] >> c[i];
    l[i]--;
    r[i]--;
    edges.push_back(vector<long long int>{r[i], l[i], c[i]});
  }
  sort(edges.begin(), edges.end());
  vector<long long int> ary(n, INT_MAX);
  ary[0] = 0;
  auto st = MinSegTree(ary);
  for (auto &edge : edges) {
    long long int cost =
        min(st.at(edge[0]), st.getMin(edge[1], edge[0]) + edge[2]);
    st.update(edge[0], cost);
  }
  long long int ans = st.at(n - 1);
  if (ans == INT_MAX) ans = -1;
  cout << ans << endl;
  return 0;
}
