#include <bits/stdc++.h>
using namespace std;
const int N = 110000;
const long long mod = 1e9 + 7;
int l[N], r[N];
int a[N], b[N], c[N];
int n, m;
vector<int> g;
vector<pair<int, long long> > f[N * 8];
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
               greater<pair<long long, int> > >
    q;
const long long INF = 1e15;
long long dis[N];
int pp[N];
int idx[N];
int maxx;
void dijkstra(int x, int n) {
  int i, u;
  for (i = 1; i <= n; ++i) dis[i] = INF, pp[i] = 0;
  dis[x] = 0;
  for (i = 1; i <= n; ++i) q.push(make_pair(dis[i], i));
  while (!q.empty()) {
    u = q.top().second;
    q.pop();
    if (pp[u]) continue;
    pp[u] = 1;
    for (i = 0; i < f[u].size(); i++)
      if (dis[f[u][i].first] > dis[u] + f[u][i].second) {
        dis[f[u][i].first] = dis[u] + f[u][i].second;
        q.push(make_pair(dis[f[u][i].first], f[u][i].first));
      }
  }
}
void build(int k, int l, int r) {
  maxx = max(maxx, k);
  if (l == r) {
    idx[l] = k;
    return;
  }
  int mid = (l + r) / 2;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
}
void build2(int k, int l, int r) {
  f[k + maxx].push_back(make_pair(k, 0));
  if (l == r) {
    return;
  }
  int mid = (l + r) / 2;
  build2(k << 1, l, mid), f[k << 1].push_back(make_pair(k, 0)),
      f[k + maxx].push_back(make_pair((k << 1) + maxx, 0));
  build2(k << 1 | 1, mid + 1, r), f[k << 1 | 1].push_back(make_pair(k, 0)),
      f[k + maxx].push_back(make_pair((k << 1 | 1) + maxx, 0));
}
void add(int k, int l, int r, int a, int b, int c) {
  if (l == a && r == b) {
    g.push_back(k);
    return;
  }
  int mid = (l + r) / 2;
  if (b <= mid)
    add(k << 1, l, mid, a, b, c);
  else if (a > mid)
    add(k << 1 | 1, mid + 1, r, a, b, c);
  else
    add(k << 1, l, mid, a, mid, c), add(k << 1 | 1, mid + 1, r, mid + 1, b, c);
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) l[i] = i - 1, r[i] = i + 1;
  r[0] = 1, l[n + 1] = n;
  build(1, 1, n);
  build2(1, 1, n);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &a[i], &b[i], &c[i]);
    g.clear();
    add(1, 1, n, a[i], b[i], i);
    for (int j = 0; j < g.size(); j++)
      for (int k = 0; k < g.size(); k++)
        f[g[j]].push_back(make_pair(g[k] + maxx, c[i]));
  }
  dijkstra(idx[1], maxx + maxx);
  if (dis[idx[n]] == INF)
    cout << -1 << endl;
  else
    cout << dis[idx[n]] << endl;
  return 0;
}
