#include <algorithm>
#include <bits/stdc++.h>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <type_traits>
#include <vector>

// lib/ceil_log2.hpp
#ifndef CEIL_LOG2_HPP
#define CEIL_LOG2_HPP


namespace tools {
  ::std::uint32_t ceil_log2(::std::uint32_t x);
  ::std::uint64_t ceil_log2(::std::uint64_t x);
  ::std::int32_t ceil_log2(::std::int32_t x);
  ::std::int64_t ceil_log2(::std::int64_t x);
}

#endif

// lib/less.hpp
#ifndef LESS_HPP
#define LESS_HPP


/*
usage:

std::sort(a.begin(), a.end(), tools::less<int, int>([](int a_i) { return std::abs(x); }));
*/

namespace tools {
  template <class T, class R>
  ::std::function<bool(T, T)> less(const ::std::function<R(T)>& selector) {
    return [selector](const T& x, const T& y) {
      return selector(x) < selector(y);
    };
  }
}

#endif

// lib/pow2.hpp
#ifndef POW2_HPP
#define POW2_HPP

/*
usage:

tools::pow2(5) => 32
*/


namespace tools {
  template <typename T, typename std::enable_if<std::is_unsigned<T>::value, std::nullptr_t>::type = nullptr>
  constexpr T pow2(const T x) {
    return static_cast<T>(1) << x;
  }

  template <typename T, typename std::enable_if<std::is_signed<T>::value, std::nullptr_t>::type = nullptr>
  constexpr T pow2(const T x) {
    return static_cast<T>(static_cast<typename std::make_unsigned<T>::type>(1) << static_cast<typename std::make_unsigned<T>::type>(x));
  }
}

#endif

// lib/monoid.hpp
#ifndef MONOID_HPP
#define MONOID_HPP


namespace tools {
  template <typename T>
  class monoid {
  private:
    ::std::function<T(T, T)> m_operation;
    T m_identity;

  public:
    monoid(const ::std::function<T(T, T)>& operation, const T& identity) :
      m_operation(operation),
      m_identity(identity) {
    }
    monoid(const ::tools::monoid<T>&) = default;
    monoid(::tools::monoid<T>&&) = default;
    ::tools::monoid<T>& operator=(const ::tools::monoid<T>&) = default;
    ::tools::monoid<T>& operator=(::tools::monoid<T>&&) = default;
    virtual ~monoid() = default;

    T operation(const T& x, const T& y) const {
      return this->m_operation(x, y);
    }
    const T& identity() const {
      return this->m_identity;
    }

    template <typename U = T, typename ::std::enable_if<::std::is_integral<U>::value, ::std::nullptr_t>::type = nullptr>
    static ::tools::monoid<U> multiplies() {
      return ::tools::monoid<U>(::std::multiplies<U>(), 1);
    }
  };
}

#endif

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP


typedef ::std::int_fast64_t i64;
typedef ::std::uint_fast64_t u64;
typedef ::std::int_fast32_t i32;
typedef ::std::uint_fast32_t u32;

namespace tools {
  template <typename T>
  void read(::std::istream& is, ::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    vector.reserve(size);
    ::std::copy_n(::std::istream_iterator<T>(is), size, ::std::back_inserter(vector));
  }
  template <typename T>
  void read(::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    ::tools::read(::std::cin, vector, size);
  }

  template <typename T, ::std::size_t N>
  void read(::std::istream& is, ::std::array<T, N>& array) {
    ::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
  }
  template <typename T, ::std::size_t N>
  void read(::std::array<T, N>& array) {
    ::tools::read(::std::cin, array);
  }
}

#endif

// lib/ceil_log2.cpp

// Source: Stack Overflow ( https://stackoverflow.com/questions/3272424/compute-fast-log-base-2-ceiling/15327567#15327567 )
// Author: dgobbi ( https://stackoverflow.com/users/2154690/dgobbi )
// License: CC BY-SA 3.0
namespace tools {
  std::uint32_t ceil_log2(std::uint32_t x) {
    static const ::std::uint32_t t[6] = {
      0xFFFF0000u,
      0x0000FF00u,
      0x000000F0u,
      0x0000000Cu,
      0x00000002u
    };

    ::std::uint32_t y = (((x & (x - 1)) == 0) ? 0 : 1);
    ::std::uint32_t j = 16;

    for (const ::std::uint32_t& t_i : t) {
      ::std::uint32_t k = (((x & t_i) == 0) ? 0 : j);
      y += k;
      x >>= k;
      j >>= 1;
    }

    return y;
  }

  ::std::uint64_t ceil_log2(::std::uint64_t x) {
    static const ::std::uint64_t t[6] = {
      0xFFFFFFFF00000000u,
      0x00000000FFFF0000u,
      0x000000000000FF00u,
      0x00000000000000F0u,
      0x000000000000000Cu,
      0x0000000000000002u
    };

    ::std::uint64_t y = (((x & (x - 1)) == 0) ? 0 : 1);
    ::std::uint64_t j = 32;

    for (const ::std::uint64_t& t_i : t) {
      ::std::uint64_t k = (((x & t_i) == 0) ? 0 : j);
      y += k;
      x >>= k;
      j >>= 1;
    }

    return y;
  }

  ::std::int32_t ceil_log2(::std::int32_t x) {
    return static_cast<::std::int32_t>(::tools::ceil_log2(static_cast<::std::uint32_t>(x)));
  }

  ::std::int64_t ceil_log2(::std::int64_t x) {
    return static_cast<::std::int64_t>(::tools::ceil_log2(static_cast<::std::uint64_t>(x)));
  }
}

// lib/dual_segtree.hpp
#ifndef DUAL_SEGTREE_HPP
#define DUAL_SEGTREE_HPP


/*
usage:

// 6要素の区間加算一点取得双対セグ木
tools::dual_segtree<i64, i64> dual_segtree(
  tools::group<i64>::plus(),
  [](const i64& t, const i64& e) {
    return t + e;
  },
  6,
  0
);

// [0, 3)に一様に30を加算
dual_segtree.update(0, 3, 30);

// 0始まりで2番目の値を取得
std::cout << dual_segtree.query(2) << std::endl;
*/

namespace tools {
  template <typename T, typename E>
  class dual_segtree {
  private:
    ::tools::monoid<E> operator_monoid;
    ::std::function<T(T, E)> act;
    ::std::size_t height;
    ::std::vector<T> data;
    ::std::vector<E> lazy;

    void propagate(const ::std::size_t node_id) {
      if(this->lazy[node_id] == this->operator_monoid.identity()) return;
      this->lazy[(node_id << 1) | 0] = this->operator_monoid.operation(this->lazy[(node_id << 1) | 0], this->lazy[node_id]);
      this->lazy[(node_id << 1) | 1] = this->operator_monoid.operation(this->lazy[(node_id << 1) | 1], this->lazy[node_id]);
      this->lazy[node_id] = this->operator_monoid.identity();
    }

    void thrust(const ::std::size_t node_id) {
      for (::std::size_t h = this->height; h > 0; --h) {
        this->propagate(node_id >> h);
      }
    }

    ::std::size_t capacity() const {
      return this->lazy.size() / 2;
    }

  public:
    dual_segtree(const ::tools::monoid<E>& operator_monoid, const ::std::function<T(T, E)>& act, const ::std::size_t& n, const T& t) :
      operator_monoid(operator_monoid),
      act(act),
      height(::tools::ceil_log2(n)),
      data(n, t),
      lazy(::tools::pow2(this->height + 1), operator_monoid.identity()) {
    }

    template <typename InputIter>
    dual_segtree(const ::tools::monoid<E>& operator_monoid, const ::std::function<T(T, E)>& act, const InputIter& begin, const InputIter& end) :
      operator_monoid(operator_monoid),
      act(act),
      height(::tools::ceil_log2(::std::distance(begin, end))),
      data(begin, end),
      lazy(::tools::pow2(this->height + 1), operator_monoid.identity()) {
    }

    ::std::size_t size() const {
      return this->data.size();
    }

    void update(const ::std::size_t a, const ::std::size_t b, const E& x) {
      if(a >= b) return;

      const ::std::size_t a_id = a + this->capacity();
      const ::std::size_t b_id = b + this->capacity() - 1;

      this->thrust(a_id);
      this->thrust(b_id);

      for (::std::size_t l = a_id, r = b_id + 1; l < r; l >>= 1, r >>= 1) {
        if (l & 1) {
          this->lazy[l] = this->operator_monoid.operation(this->lazy[l], x);
          ++l;
        }
        if (r & 1) {
          --r;
          this->lazy[r] = this->operator_monoid.operation(this->lazy[r], x);
        }
      }
    }

    T query(const ::std::size_t a) {
      const ::std::size_t node_id = a + this->capacity();
      this->thrust(node_id);
      return this->data[a] = this->act(this->data[a], this->lazy[node_id]);
    }
  };
}

#endif

// main.cpp

class query {
public:
  i64 L;
  i64 R;
  i64 C;
  friend std::istream& operator>>(std::istream& is, query& self) {
    is >> self.L >> self.R >> self.C;
    --self.L;
    return is;
  }
};

int main() {
  i64 N, M;
  std::cin >> N >> M;
  std::vector<query> queries;
  tools::read(queries, M);

  tools::dual_segtree<i64, i64> dual_segtree(
    tools::monoid<i64>(
      [](const i64& e1, const i64& e2) {
        return std::min(e1, e2);
      },
      std::numeric_limits<i64>::max()
    ),
    [](const i64& t, const i64& e) {
      return std::min(t, e);
    },
    static_cast<std::size_t>(N),
    std::numeric_limits<i64>::max()
  );
  dual_segtree.update(0, 1, 0);

  std::sort(queries.begin(), queries.end(), tools::less<query, i64>([](const query& query) { return query.L; }));
  const auto safe_sum = [](const i64& x, const i64& y) {
    if (x == std::numeric_limits<i64>::max()) return x;
    if (y == std::numeric_limits<i64>::max()) return y;
    return x + y;
  };
  for (const query& query : queries) {
    dual_segtree.update(query.L, query.R, safe_sum(dual_segtree.query(query.L), query.C));
  }

  i64 answer = dual_segtree.query(N - 1);
  if (answer == std::numeric_limits<i64>::max()) answer = -1;
  std::cout << answer << std::endl;
  return 0;
}

Measured memory_bytes: 5376