#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<int>> vii;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long infll = (1LL << 62) - 1;
const long long inf = (1LL << 62) - 1;
template <typename T>
class Edge {
 public:
  int srch;
  int to;
  T cost;
  Edge(int t, T w) : srch(-1), to(t), cost(w) {}
  Edge(int src, int t, T w) : srch(src), to(t), cost(w) {}
};
template <typename T>
using W_Graph = vector<vector<Edge<T>>>;
using Graph = vector<vector<int>>;
template <typename T>
using Matrix = vector<vector<T>>;
signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  W_Graph<long long> G(n);
  for (int j = 0; j < n - 1; ++j) {
    G[j + 1].push_back(Edge<long long>(j, 0));
  }
  for (int i = 0; i < ((int)(m)); i++) {
    int l, r;
    long long c;
    cin >> l >> r >> c;
    --l;
    --r;
    G[l].push_back(Edge<long long>(r, c));
    G[r].push_back(Edge<long long>(l, c));
  }
  using p = pair<long long, int>;
  priority_queue<p, vector<p>, greater<p>> que;
  vector<int> min_dist(n + 10, inf);
  min_dist[0] = 0;
  que.emplace(min_dist[0], 0);
  while (que.size()) {
    long long dist;
    int index;
    tie(dist, index) = que.top();
    que.pop();
    if (dist > min_dist[index]) continue;
    for (auto edge : G[index]) {
      long long new_dist = dist + edge.cost;
      if (min_dist[edge.to] <= new_dist) continue;
      chmin(min_dist[edge.to], new_dist);
      que.emplace(min_dist[edge.to], edge.to);
    }
  }
  if (min_dist[n - 1] != inf)
    cout << min_dist[n - 1] << '\n';
  else
    cout << -1 << '\n';
}
