#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> v[100500];
vector<long long> cost(100500, 1e18);
bool dijkstra(long long st) {
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      que;
  cost[st] = 0;
  que.push(make_pair(0, st));
  while (!que.empty()) {
    pair<long long, long long> p = que.top();
    que.pop();
    long long now = p.second;
    long long nc = p.first;
    cout << now << " " << nc << endl;
    for (long long i = 0; i < v[now].size(); i++) {
      if (cost[v[now][i].first] > nc + v[now][i].second) {
        cost[v[now][i].first] = nc + v[now][i].second;
        que.push(make_pair(cost[v[now][i].first], v[now][i].first));
      }
    }
  }
}
int main() {
  long long N, M;
  cin >> N >> M;
  long long tmp, tmp1, tmp2;
  for (long long i = 0; i < M; i++) {
    cin >> tmp >> tmp1 >> tmp2;
    tmp--;
    tmp1--;
    v[tmp].emplace_back(make_pair(tmp1, tmp2));
  }
  for (long long i = 1; i < N; i++) {
    v[i].emplace_back(make_pair(i - 1, 0));
  }
  dijkstra(0);
  cout << cost[N - 1] << endl;
  return 0;
}
