#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T1, class T2>
ostream& operator<<(ostream& s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T>
ostream& operator<<(ostream& s, vector<T> P) {
  for (long long i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T>
ostream& operator<<(ostream& s, vector<vector<T>> P) {
  for (long long i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T>
ostream& operator<<(ostream& s, set<T> P) {
  for (__typeof__((P).begin()) it = (P).begin(); it != (P).end(); ++it) {
    s << "<" << *it << "> ";
  }
  return s << endl;
}
template <class T1, class T2>
ostream& operator<<(ostream& s, map<T1, T2> P) {
  for (__typeof__((P).begin()) it = (P).begin(); it != (P).end(); ++it) {
    s << "<" << it->first << "->" << it->second << "> ";
  }
  return s << endl;
}
template <class T>
void show(vector<T> v) {
  for (long long i = 0; i < v.size(); i++) {
    cerr << v[i] << " ";
  }
  cerr << "\n";
}
using edge = pair<long long, long long>;
using Graph = vector<vector<edge>>;
Graph G;
long long d[100010];
void dijkstra(long long s) {
  for (long long i = 0; i < 100010; i++) {
    d[i] = 1e9;
  }
  d[s] = 0;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      pq;
  pq.push({0, s});
  while (!pq.empty()) {
    pair<long long, long long> p = pq.top();
    pq.pop();
    long long v = p.second;
    long long dis = p.first;
    if (d[v] < dis) {
      continue;
    }
    for (auto u : G[v]) {
      if (d[v] + u.second < d[u.first]) {
        d[u.first] = d[v] + u.second;
        pq.push({d[u.first], u.first});
      }
    }
  }
}
signed main() {
  long long n, m;
  cin >> n >> m;
  G.assign(n, vector<edge>());
  for (long long i = 0; i < m; ++i) {
    long long l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    G[l].push_back(edge(r, c));
    G[r].push_back(edge(l, c));
  }
  for (long long i = 1; i < n; i++) {
    G[i].push_back(edge(i - 1, 0));
  }
  dijkstra(0);
  if (d[n - 1] == 1e9) {
    cout << -1 << endl;
  } else {
    cout << d[n - 1] << endl;
  }
  return 0;
}
