#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e9 + 7;
const int N = 1e6 + 7;
struct aa {
  int u, d;
  bool operator<(const aa& a) const { return d > a.d; }
};
vector<aa> arr[N];
bool vis[N];
int n, m;
int d[N];
void Dij() {
  fill(d + 1, d + n + 1, MAX);
  d[1] = 0;
  priority_queue<aa> Q;
  Q.push({1, 0});
  while (!Q.empty()) {
    aa x = Q.top();
    Q.pop();
    int u = x.u;
    if (vis[u]) continue;
    vis[u] = 1;
    for (int i = 0; i < arr[u].size(); i++) {
      aa e = arr[u][i];
      if (d[e.u] > d[u] + e.d) {
        d[e.u] = d[u] + e.d;
        Q.push({e.u, d[e.u]});
      }
    }
  }
}
int main() {
  int a, b, w;
  scanf("%d%d", &n, &m);
  for (int i = 2; i <= n; i++) {
    arr[i].push_back({i - 1, 0});
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &w);
    arr[b].push_back({a, w});
    arr[a].push_back({b, w});
  }
  Dij();
  if (d[n] == MAX) d[n] = -1;
  cout << d[n] << endl;
  return 0;
}
