#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int64_t infi = 1147483600;
const long long infl = 4e18 + 5;
const char EOL = '\n';
const long long MOD = 998244353;
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << std::fixed << setprecision(15);
  auto unit_r = 180.0 / M_PI;
  int64_t n, m;
  cin >> n >> m;
  vector<vector<pair<int64_t, int64_t>>> costs(n);
  vector<int64_t> mincost(n);
  for (int64_t i = 0; i < n; ++i) {
    mincost[i] = -1;
  }
  mincost[0] = 0;
  for (int64_t i = 0; i < m; ++i) {
    int64_t l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    costs[l].emplace_back(make_pair(r, c));
    costs[r].emplace_back(make_pair(l, c));
  }
  set<int64_t> s;
  s.emplace(0);
  while (!s.empty()) {
    auto l = *s.begin();
    auto c = costs[l];
    for (auto iter = c.begin(); iter != c.end(); ++iter) {
      auto r = get<0>(*iter);
      if (l < r) {
        for (auto j = l + 1; j <= r; ++j) {
          if (mincost[j] == -1 || mincost[j] > (mincost[l] + get<1>(*iter))) {
            mincost[j] = mincost[l] + get<1>(*iter);
          }
          s.emplace(j);
        }
      } else {
        for (auto j = r; j < l; ++j) {
          if (mincost[j] == -1 || mincost[j] > (mincost[l] + get<1>(*iter))) {
            mincost[j] = mincost[l] + get<1>(*iter);
          }
          s.emplace(j);
        }
      }
    }
    s.erase(s.begin());
    costs[l].clear();
  }
  cout << mincost[n - 1] << endl;
}
