#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
long long dijkstra(int N, int s, int g, vector<vector<long long>> cost) {
  vector<long long> d(N + 1, INF);
  vector<bool> used(N + 1, false);
  vector<int> back(N + 1, -1);
  fill(d.begin(), d.end(), INF);
  fill(used.begin(), used.end(), false);
  fill(back.begin(), back.end(), -1);
  d[s] = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < N + 1; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) v = u;
    }
    if (v == -1) break;
    used[v] = true;
    for (int u = 0; u < N + 1; u++) {
      if (d[u] > d[v] + cost[v][u]) {
        d[u] = d[v] + cost[v][u];
        back[u] = v;
      }
    }
  }
  return d[g];
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> L, R, C;
  int l, r, c;
  for (int i = 0; i < M; i++) {
    cin >> l >> r >> c;
    L.push_back(l);
    R.push_back(r);
    C.push_back(c);
  }
  vector<vector<long long>> path(N + 1, vector<long long>(N + 1, INF));
  for (int i = N; i > 0; i--) {
    path[i][i - 1] = 0;
    path[i][i] = 0;
  }
  for (int i = 0; i < (int)(M); i++) {
    path[L[i]][R[i]] = C[i];
  }
  long long ans = dijkstra(N, 1, N, path);
  if (ans == INF) {
    cout << -1 << "\n";
  } else {
    cout << ans << "\n";
  }
  return 0;
}
