#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll INF = (ll)1e18;
constexpr ll MOD = (ll)1e9 + 7;
constexpr double EPS = 1e-9;
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
template <typename T>
inline void pv(vector<T>& v) {
  for (ll i = 0, N = v.size(); i < N; i++)
    std::cout << v[i] << " \n"[i == N - 1];
}
template <typename T>
inline T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  return a > b && (a = b, true);
}
class Edge {
 public:
  ll from, to, cost;
  Edge() {}
  Edge(ll from, ll to, ll cost) : from(from), to(to), cost(cost) {}
};
ostream& operator<<(ostream& s, const Edge& e) {
  s << "{ " << e.from << " -> " << e.to << ", " << e.cost << " }";
  return s;
}
void dijkstra(const vector<vector<Edge>>& g, ll start, vector<ll>& dist,
              vector<ll>& prev) {
  ll gsize = g.size();
  dist.assign(gsize, INF);
  dist[start] = 0;
  prev.assign(gsize, -1);
  priority_queue<Edge, vector<Edge>, function<bool(Edge, Edge)>> que(
      [](const Edge& x, const Edge& y) {
        return x.cost != y.cost
                   ? (x.cost > y.cost)
                   : (x.from != y.from ? x.from < y.from : x.to < y.to);
      });
  que.push(Edge(-1, start, 0));
  while (!que.empty()) {
    Edge e = que.top();
    que.pop();
    if (prev[e.to] != -1) continue;
    prev[e.to] = e.from;
    for (Edge f : g[e.to]) {
      if (dist[f.to] > e.cost + f.cost) {
        dist[f.to] = e.cost + f.cost;
        que.push(Edge(f.from, f.to, e.cost + f.cost));
      }
    }
  }
}
signed main() {
  ll N, M;
  cin >> N >> M;
  vector<vector<Edge>> g(N + M);
  for (ll i = 0, i_length = (M); i < i_length; ++i) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    for (ll j = l; j <= r; j++) {
      Edge e(j, N + i, c);
      g[e.from].push_back(e);
      Edge f(N + i, j, c);
      g[f.from].push_back(f);
    }
  }
  vector<ll> dist(N + M), prev(N + M);
  dijkstra(g, 0, dist, prev);
  ;
  ;
  if (dist[N - 1] == INF)
    std::cout << -1 << std::endl;
  else
    std::cout << dist[N - 1] / 2 << std::endl;
  return 0;
}
