#include <bits/stdc++.h>
using namespace std;
const long long INF = 1E9 + 7;
const long long N = 1E5 + 7;
long long n, m;
struct stu {
  long long a, b;
  bool friend operator<(const stu &x, const stu &y) { return x.b > y.b; }
};
vector<stu> ve[N];
bool mark[N];
long long dp[N];
void djstrea() {
  priority_queue<stu> que;
  que.push({1, 0});
  dp[1] = 0;
  while (que.size()) {
    stu x = que.top();
    que.pop();
    if (mark[x.a]) continue;
    mark[x.a] = 1;
    for (long long i = 0; i < ve[x.a].size(); i++) {
      long long dx = ve[x.a][i].a;
      long long dy = ve[x.a][i].b;
      if (dp[dx] > dp[x.a] + dy) {
        dp[dx] = dp[x.a] + dy;
        que.push({dx, dp[dx]});
      }
    }
  }
}
int main() {
  cin >> n >> m;
  long long x, y, z;
  for (long long i = 1; i <= n; i++) {
    ve[i].push_back({i - 1, 0});
  }
  for (long long i = 0; i <= n; i++) dp[i] = INF;
  for (long long i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    ve[x].push_back({y, z});
  }
  djstrea();
  if (dp[n] == INF)
    cout << -1 << endl;
  else
    cout << dp[n] << endl;
  return 0;
}
