#include <bits/stdc++.h>
using namespace std;
static const int INF = INT_MAX;
static const long long LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const long long LMIN = LLONG_MIN;
static const int MOD = 1000000007;
static const int SIZE = 200005;
const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};
vector<int> Div(int n) {
  vector<int> ret;
  for (int i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}
long long dijkstra();
vector<pair<long long, long long> > graph[SIZE];
long long dist[SIZE];
int n, m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    graph[l].push_back(make_pair(r, c));
    graph[r].push_back(make_pair(l, c));
  }
  for (int i = 2; i <= n; ++i) {
    graph[i].push_back(make_pair(i - 1, 0));
  }
  long long res = dijkstra();
  if (res == LINF) res = -1;
  cout << res << '\n';
  return 0;
}
long long dijkstra() {
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      pq;
  pq.push(make_pair(1, 0));
  for (int i = 0; i < SIZE; ++i) {
    dist[i] = LINF;
  }
  while (!pq.empty()) {
    pair<long long, long long> node = pq.top();
    pq.pop();
    if (node.first == n) continue;
    for (int i = 0; i < graph[node.first].size(); ++i) {
      pair<long long, long long> p = graph[node.first][i];
      if (dist[p.first] > node.second + p.second) {
        dist[p.first] = node.second + p.second;
        pq.push(make_pair(p.first, dist[p.first]));
      }
    }
  }
  return dist[n];
}
