#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 50;
template <class T>
class Dijkstra {
 private:
  using P = pair<int, int>;
  struct edge {
    int to;
    T cost;
  };
  T identity;
  vector<vector<edge>> graph;
  vector<T> dist;

 public:
  Dijkstra(int node_num, T _identity)
      : identity(_identity),
        graph(vector<vector<edge>>(node_num)),
        dist(vector<T>(node_num, identity)) {}
  void add_edge(int from, int to, T cost) {
    edge now_edge = {to, cost};
    graph[from].push_back(now_edge);
  }
  void calc_shortest_path(int start_node) {
    priority_queue<P, vector<P>, greater<P>> PQ;
    dist[start_node] = 0;
    PQ.push(P(0, start_node));
    while (!PQ.empty()) {
      P now = PQ.top();
      PQ.pop();
      int now_u = now.second;
      if (dist[now_u] < now.first) continue;
      for (int i = 0; i < graph[now_u].size(); i++) {
        edge e = graph[now_u][i];
        if (dist[e.to] > dist[now_u] + e.cost) {
          dist[e.to] = dist[now_u] + e.cost;
          PQ.push(P(dist[e.to], e.to));
        }
      }
    }
  }
  T get_dist(int u) { return dist[u]; }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  int L, R;
  ll C;
  Dijkstra<ll> dijkstra(N, INF);
  for (int i = (int)0; i < (int)M; ++i) {
    cin >> L >> R >> C;
    --L, --R;
    dijkstra.add_edge(L, R, C);
    dijkstra.add_edge(R, L, C);
  }
  for (int i = (int)1; i < (int)N; ++i) {
    dijkstra.add_edge(i, i - 1, 0);
  }
  dijkstra.calc_shortest_path(0);
  ll ans = dijkstra.get_dist(N - 1);
  cout << (ans == INF ? -1 : ans) << endl;
  return 0;
}
