#include <bits/stdc++.h>
using namespace std;
int value[20010], to[20010], nxt[20010];
int head[20010], total;
int book[20010];
int dis[20010];
int n, m;
void adl(int a, int b, int c) {
  total++;
  to[total] = b;
  value[total] = c;
  nxt[total] = head[a];
  head[a] = total;
}
void dijkstra(int u) {
  memset(dis, 88, sizeof(dis));
  memset(book, 0, sizeof(book));
  dis[u] = 0;
  for (int i = 1; i <= n; i++) {
    int start = -1;
    for (int j = 1; j <= n; j++)
      if (book[j] == 0 && (dis[start] > dis[j] || start == -1)) start = j;
    book[start] = 1;
    for (int e = head[start]; e; e = nxt[e])
      dis[to[e]] = min(dis[to[e]], dis[start] + value[e]);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    adl(a, b, c);
    for (int j = b - 1; j >= a; j--) adl(b, j, 0);
  }
  dijkstra(1);
  if (dis[n] != 1482184792)
    cout << dis[n] << endl;
  else
    cout << -1 << endl;
}
