#include <bits/stdc++.h>
using namespace std;
static const int INF = 1e9;
vector<int> value;
int N;
void update(int i, int x) {
  i += N - 1;
  value[i] = x;
  while (i > 0) {
    i = (i - 1) / 2;
    value[i] = min(value[i * 2 + 1], value[i * 2 + 2]);
  }
}
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) return INF;
  if (a <= l && r <= b)
    return value[k];
  else {
    int c1 = query(a, b, 2 * k + 1, l, (l + r) / 2);
    int c2 = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(c1, c2);
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  N = 1;
  while (N < n) N *= 2;
  value = vector<int>(2 * N - 1, INF);
  vector<vector<int>> x(n, vector<int>(3));
  for (int i = 0; i < m; i++) {
    cin >> x.at(i).at(0) >> x.at(i).at(1) >> x.at(i).at(2);
  }
  update(0, 0);
  for (int i = 0; i < m; i++) {
    int a = query(x.at(i).at(0) - 1, x.at(i).at(1), 0, 0, N);
    update(x.at(i).at(1) - 1,
           min(a + x.at(i).at(2), value[x.at(i).at(1) - 1 + N - 1]));
    update(x.at(i).at(0) - 1,
           min(a + x.at(i).at(2), value[x.at(i).at(0) - 1 + N - 1]));
  }
  if (value[n - 1 + N - 1] == INF)
    cout << -1 << "\n";
  else
    cout << value[n - 1 + N - 1] << "\n";
}
