#include <bits/stdc++.h>
using namespace std;
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T> >;
template <class T, class U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
  os << p.first << ',' << p.second;
  return os;
}
template <class T, class U>
istream& operator>>(istream& is, pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 < vec.size()) os << ' ';
  }
  return os;
}
template <class T>
istream& operator>>(istream& is, vector<T>& vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}
void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}
long long d[500100];
int V;
struct edge {
  int to;
  long long cost;
  edge(int to, long long cost) : to(to), cost(cost) {}
};
vector<edge> g[500100];
map<pair<int, int>, int> mu, md;
void dijkstra(int s) {
  fill(d, d + V + 1, 1000000000000000ll);
  priority_queue<pair<int, long long>, vector<pair<int, long long> >,
                 greater<pair<int, long long> > >
      q;
  d[s] = 0;
  q.push(pair<int, long long>(0ll, s));
  while (!q.empty()) {
    pair<int, long long> a = q.top();
    q.pop();
    if (d[a.second] < a.first) continue;
    for (int i = 0; i < g[a.second].size(); i++) {
      edge e = g[a.second][i];
      if (d[e.to] > d[a.second] + e.cost) {
        d[e.to] = d[a.second] + e.cost;
        q.push(pair<int, long long>(d[e.to], e.to));
      }
    }
  }
  return;
}
void add_edge(int s, int t, long long c) { g[s].push_back(edge(t, c)); }
int A;
int N, M;
int n;
int L[100100], R[100100], C[100100];
void register_id(int l, int r, bool flag) {
  if (l + 1 == r) {
    if (flag)
      mu[make_pair(l, r)] = l;
    else
      md[make_pair(l, r)] = l;
    return;
  }
  if (flag)
    mu[make_pair(l, r)] = A++;
  else
    md[make_pair(l, r)] = A++;
  int mid = (l + r) / 2;
  register_id(l, mid, flag);
  register_id(mid, r, flag);
  return;
}
int construct(int l, int r, bool flag) {
  if (l + 1 == r) {
    if (flag)
      return mu[make_pair(l, r)];
    else
      return md[make_pair(l, r)];
  }
  int mid = (l + r) / 2;
  int self;
  if (flag)
    self = mu[make_pair(l, r)];
  else
    self = md[make_pair(l, r)];
  int to1 = construct(l, mid, flag);
  int to2 = construct(mid, r, flag);
  if (flag) {
    add_edge(self, to1, 0ll);
    add_edge(self, to2, 0ll);
  } else {
    add_edge(to1, self, 0ll);
    add_edge(to2, self, 0ll);
  }
  return self;
}
void range_to_vec(int a, int b, int l, int r, bool flag, vector<int>& v) {
  if (r <= a || b <= l) return;
  if (a <= l && r <= b) {
    if (flag)
      v.push_back(mu[make_pair(l, r)]);
    else
      v.push_back(md[make_pair(l, r)]);
  } else {
    int mid = (l + r) / 2;
    range_to_vec(a, b, l, mid, flag, v);
    range_to_vec(a, b, mid, r, flag, v);
  }
}
int main() {
  fastio();
  cin >> N >> M;
  n = 1;
  A = N;
  while (n < N) n *= 2;
  register_id(0, n, true);
  register_id(0, n, false);
  construct(0, n, true);
  construct(0, n, false);
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i] >> C[i];
    L[i]--;
    vector<int> s, t;
    range_to_vec(L[i], R[i], 0, n, true, s);
    range_to_vec(L[i], R[i], 0, n, false, t);
    for (int j = 0; j < t.size(); j++) {
      add_edge(t[j], A, 0);
    }
    for (int j = 0; j < s.size(); j++) {
      add_edge(A + 1, s[j], 0);
    }
    add_edge(A, A + 1, C[i]);
    A += 2;
  }
  V = A;
  dijkstra(0);
  if (d[N - 1] == 1000000000000000ll)
    cout << -1 << endl;
  else
    cout << d[N - 1] << endl;
  return 0;
}
