#include <bits/stdc++.h>
using namespace std;
const int N = 11000;
const int M = 2e5 + 5;
using namespace std;
struct Edge {
  int next;
  int to;
  long long dis;
} edge[M * 2];
int head[M], tot;
long long d[M];
bool vis[M];
inline void add(int from, int to, long long dis) {
  edge[++tot].next = head[from];
  edge[tot].to = to;
  edge[tot].dis = dis;
  head[from] = tot;
}
struct node {
  long long val;
  int id;
  node() {}
  node(int a, long long b) : id(a), val(b) {}
  bool operator<(node A) const { return val > A.val; }
};
void dij(int u) {
  memset(vis, 0, sizeof vis);
  d[u] = 0;
  priority_queue<node> Q;
  Q.push({u, 0});
  node tmp;
  while (Q.size()) {
    tmp = Q.top();
    Q.pop();
    int x = tmp.id;
    if (vis[x]) continue;
    for (int i = head[x]; ~i; i = edge[i].next) {
      int y = edge[i].to;
      long long dis = edge[i].dis;
      if (d[y] > d[x] + dis) {
        d[y] = d[x] + dis;
        Q.push({y, d[y]});
      }
    }
  }
}
inline void init(int n) {
  memset(head, -1, sizeof head);
  tot = 0;
  for (int i = 1; i <= n; ++i) d[i] = (long long)1e11;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  init(n);
  int u, v;
  long long dis;
  for (int i = 1; i <= n; ++i) {
    add(i, i - 1, 0);
  }
  for (int i = 1; i <= m; ++i) {
    cin >> u >> v >> dis;
    add(u, v, dis);
  }
  dij(1);
  if (d[n] == (long long)1e11)
    cout << -1 << endl;
  else
    cout << d[n] << endl;
}
