#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<int64_t>> costs(n, vector<int64_t>(n, -1));
  vector<int64_t> mincost(n);
  for (int64_t i = 0; i < n; ++i) {
    costs[i][i] = 0;
    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--;
    auto p = l;
    auto q = r;
    if (l > r) {
      p = r;
      q = l;
    }
    for (auto i = p; i <= q; ++i) {
      for (auto j = p; j <= q; ++j) {
        if (costs[i][j] == -1 || costs[i][j] > c) {
          costs[i][j] = c;
        }
      }
    }
  }
  set<int64_t> s;
  s.emplace(0);
  while (!s.empty()) {
    auto l = *s.begin();
    for (int64_t i = 0; i < n; ++i) {
      if (costs[l][i] > 0) {
        bool end = true;
        if (mincost[i] == -1 || mincost[i] > (mincost[l] + costs[l][i])) {
          mincost[i] = mincost[l] + costs[l][i];
          costs[l][i] = -1;
          end = false;
        }
        if (!end) {
          s.emplace(i);
        }
      }
    }
    s.erase(s.begin());
  }
  cout << mincost[n - 1] << endl;
}
