#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int, int> > > graph;
vector<int> dist;
void dijkstra() {
  dist[1] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      pq;
  pq.push(pair<int, int>(0, 1));
  while (!pq.empty()) {
    pair<int, int> x = pq.top();
    pq.pop();
    int a = x.first;
    int b = x.second;
    if (dist[b] == a) {
      for (int i = 0; i < graph[b].size(); ++i) {
        pair<int, int> c = graph[b][i];
        if (dist[c.first] == -1 || dist[c.first] > dist[b] + c.second) {
          dist[c.first] = dist[b] + c.second;
          pq.push(pair<int, int>(dist[c.first], c.first));
        }
      }
    }
  }
}
int main() {
  int n, m, i, x, y, w;
  cin >> n >> m;
  graph.assign(n + 1, vector<pair<int, int> >());
  dist.assign(n + 1, -1);
  for (i = 1; i <= n; ++i) {
    graph[i].push_back(pair<int, int>(i - 1, 0));
  }
  for (i = 1; i <= m; ++i) {
    cin >> x >> y >> w;
    graph[x].push_back(pair<int, int>(y, w));
    graph[y].push_back(pair<int, int>(x, w));
  }
  dijkstra();
  printf("%d", dist[n]);
}
