#include <bits/stdc++.h>
using namespace std;
int inf = 1000000007;
class Edge {
 public:
  int s;
  int t;
  int len;
};
class Node {
 public:
  int num;
  long long dist;
  vector<Edge> edge;
  bool comp;
  void in_edge(Edge &edge_in) { edge.push_back(edge_in); }
};
bool operator>(const Node &a, const Node &r) { return a.dist > r.dist; }
void dijkstra(vector<Node> &node, int s, int t, int n) {
  for (int i = 1; i <= n; i++) {
    node[i].dist = inf;
    node[i].num = i;
    node[i].comp = true;
  }
  node[s].dist = 0;
  priority_queue<Node, vector<Node>, greater<Node>> que;
  que.push(node[s]);
  while (!que.empty()) {
    Node buf;
    buf = que.top();
    que.pop();
    int k = buf.num;
    if (node[k].comp) {
      node[k].comp = false;
      int edge_num = buf.edge.size();
      for (int i = 0; i < edge_num; i++) {
        int buf_t = node[k].edge[i].t;
        int buf_len = node[k].edge[i].len;
        node[buf_t].dist = min(node[buf_t].dist, node[k].dist + buf_len);
        que.push(node[buf_t]);
      }
    }
  }
}
int main() {
  int n, s, t, m;
  cin >> n >> m;
  s = 1;
  t = n;
  vector<Node> node(n + 1);
  for (int i = n; i > 1; i--) {
    Edge pri;
    pri.s = i;
    pri.t = i - 1;
    pri.len = 0;
    node[i].in_edge(pri);
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    long long l;
    cin >> a >> b >> l;
    Edge in;
    in.s = a;
    in.t = b;
    in.len = l;
    node[a].in_edge(in);
    in.s = b;
    in.t = a;
    node[b].in_edge(in);
  }
  dijkstra(node, s, t, n);
  if (node[t].dist == inf) node[t].dist = -1;
  cout << node[t].dist << endl;
  return 0;
}
