#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int64_t mod = 998244353;
int64_t mod1 = 1e9 + 5;
int64_t power(int64_t a, int64_t b) {
  if (b == 0)
    return 1;
  else if (b % 2 == 0)
    return power((((a % mod) * (a % mod)) % mod), b / 2) % mod;
  else
    return ((a % mod) * (power((((a % mod) * (a % mod)) % mod), b / 2) % mod)) %
           mod;
}
vector<pair<int64_t, int64_t>> v[100001];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int64_t n, m;
  cin >> n >> m;
  for (int64_t i = 0; i < m; ++i) {
    int64_t a, b, c;
    cin >> a >> b >> c;
    v[a].push_back({c, b});
  }
  for (int64_t i = 1; i <= n; ++i) {
    sort(v[i].begin(), v[i].end());
  }
  int64_t dis[n + 1];
  memset(dis, -1, sizeof(dis));
  int64_t upd[n + 1];
  memset(upd, -1, sizeof(upd));
  dis[1] = 0;
  for (int64_t i = 1; i <= n; ++i) {
    if (dis[i] == -1) continue;
    for (int64_t j = 0; j < v[i].size(); ++j) {
      int64_t k = v[i][j].second;
      int64_t cos = v[i][j].first;
      if (dis[k] == -1) {
        while (dis[k] == -1) {
          dis[k] = dis[i] + cos;
          upd[k] = i;
          k--;
        }
        if (k == i)
          continue;
        else {
          if (upd[k] == i)
            continue;
          else {
            while (k != i) {
              if (dis[k] > dis[i] + cos) {
                dis[k] = dis[i] + cos;
                upd[k] = i;
              }
              k--;
            }
          }
        }
      } else {
        if (upd[k] == i) continue;
        while (k != i) {
          if (dis[k] > dis[i] + cos) {
            dis[k] = dis[i] + cos;
            upd[k] = i;
          }
          k--;
        }
      }
    }
  }
  cout << dis[n];
}
