#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1 << 30;
const ll mod = 1000000007LL;
struct dijkstra {
  struct Node {
    vector<int> edges_to;
    vector<int> edges_cost;
    bool done;
    int cost = -1;
  };
  vector<Node> graph;
  void init(int N) { graph.resize(N); }
  void add(int from, int to, int cost) {
    graph[from].edges_to.push_back(to);
    graph[from].edges_cost.push_back(cost);
  }
  void shortest(int start) {
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        d;
    d.push(make_pair(0, start));
    while (!d.empty()) {
      pair<int, int> p = d.top();
      d.pop();
      Node &point = graph[p.second];
      if (point.done) continue;
      point.done = true;
      point.cost = p.first;
      for (int i = 0; i < (int)point.edges_to.size(); i++) {
        if (graph[point.edges_to[i]].done) continue;
        d.push(make_pair(point.cost + point.edges_cost[i], point.edges_to[i]));
      }
    }
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  dijkstra line;
  line.init(N);
  for (int i = 0; i < M; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    line.add(x - 1, y - 1, z);
  }
  for (int i = 0; i < N - 1; i++) {
    line.add(i + 1, i, 0);
  }
  line.shortest(0);
  cout << line.graph[N - 1].cost << endl;
}
