#include <bits/stdc++.h>
using namespace std;
void Debug() { cerr << '\n'; }
template <class FIRST, class... REST>
void Debug(FIRST arg, REST... rest) {
  cerr << arg << " ";
  Debug(rest...);
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  if (!v.empty()) {
    for (int i = int(0); i < int((int)(v).size() - 1); i++) out << v[i] << ", ";
    out << v.back();
  }
  out << "]";
  return out;
}
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& v) {
  out << "(" << v.first << ", " << v.second << ")";
  return out;
}
const int MAX_N = 2000010;
const int MAX_V = 100010;
const double eps = 1e-6;
const long long mod = 1000000007;
const int inf = (1 << 30) - 1;
const long long linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng;
int N, M, n;
vector<pair<long long, long long> > G[MAX_N];
void add_edge(int a, int b, long long c, int idx, int k, int l, int r) {
  if (b <= l || r <= a) return;
  if (a <= l && r <= b) {
    G[k + 2 * n].push_back(pair<long long, long long>(idx, c));
    G[idx].push_back(pair<long long, long long>(k, 0));
  } else {
    add_edge(a, b, c, idx, k * 2 + 1, l, (l + r) / 2);
    add_edge(a, b, c, idx, k * 2 + 2, (l + r) / 2, r);
  }
}
long long d[MAX_N];
void dijkstra(int s) {
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      que;
  fill(d, d + 4 * n + M, linf);
  d[s] = 0;
  que.push(pair<int, int>(0, s));
  while (!que.empty()) {
    pair<long long, long long> p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) continue;
    for (int i = 0; i < (int)(G[v]).size(); i++) {
      pair<long long, long long> e = G[v][i];
      if (d[e.first] > d[v] + e.second) {
        d[e.first] = d[v] + e.second;
        que.push(pair<int, int>(d[e.first], e.first));
      }
    }
  }
}
void solve() {
  cin >> N >> M;
  n = 1;
  while (n < N) n *= 2;
  for (int i = int(0); i < int(n - 1); i++) {
    G[i].push_back(pair<long long, long long>(i * 2 + 1, 0));
    G[i].push_back(pair<long long, long long>(i * 2 + 2, 0));
    G[i * 2 + 1 + 2 * n].push_back(pair<long long, long long>(i + 2 * n, 0));
    G[i * 2 + 2 + 2 * n].push_back(pair<long long, long long>(i + 2 * n, 0));
  }
  for (int i = n - 1; i < 2 * n - 1; i++) {
    G[i].push_back(pair<long long, long long>(i + 2 * n, 0));
  }
  for (int i = int(0); i < int(M); i++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    add_edge(l, r, c, 4 * n + i, 0, 0, n);
  }
  dijkstra(n - 1);
  if (d[n - 1 + N - 1] == linf)
    cout << -1 << "\n";
  else
    cout << d[n - 1 + N - 1] << "\n";
}
uint32_t rd() {
  uint32_t res;
  res = std::random_device()();
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(20);
  cerr << fixed;
  cerr.precision(6);
  rng.seed(rd());
  solve();
  cerr << "Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}
