#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long GCD(long long a, long long b) { return b ? GCD(b, a % b) : a; }
long long LCM(long long a, long long b) { return a / GCD(a, b) * b; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
long long int cmp(pair<long long int, string> a,
                  pair<long long int, string> b) {
  if (a.first != b.first)
    return a.first < b.first;
  else
    return a.second < b.second;
}
long long int n, m;
vector<pair<long long int, long long int> > adj[100001];
vector<long long int> d;
void dijkstraplus() {
  d.resize(n);
  priority_queue<pair<long long int, long long int> > pq;
  long long int color[10001];
  for (long long int i = 0; i < n; i++) {
    d[i] = 10e16;
    color[i] = 0;
  }
  d[0] = 0;
  pq.push(make_pair(0, 0));
  color[0] = 1;
  while (!pq.empty()) {
    pair<long long int, long long int> f = pq.top();
    pq.pop();
    long long int u = f.second;
    color[u] = 2;
    if (d[u] < f.first * (-1)) continue;
    for (long long int j = 0; j < adj[u].size(); j++) {
      long long int v = adj[u][j].first;
      if (color[v] == 2) continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        pq.push(make_pair(d[v] * (-1), v));
        color[v] = 1;
      }
    }
  }
  if (d[n - 1] == 10e16) d[n - 1] = -1;
  cout << d[n - 1] << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    long long int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    long long int t = r;
    for (; l < t; l++)
      for (; r > l; r--) {
        adj[l].push_back(make_pair(r, c));
        adj[r].push_back(make_pair(l, c));
      }
  }
  dijkstraplus();
  return 0;
}
