#include <bits/stdc++.h>
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
static const int NIL = -1;
static const int INF = 1000000007;
deque<int> deq;
long long N, M, H, W, K, Q, A, B;
string S;
const long long MOD = (1e+9) + 7;
bool chmx(int &a, int b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
bool chmn(int &a, int b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
int n;
vector<pair<int, int>> to[100005];
int main() {
  int m;
  cin >> n >> m;
  long long dis[n];
  for (int i = 0; i < (int)(n); i++) {
    dis[i] = INF;
    if (i != n - 1) {
      to[i + 1].push_back(make_pair(i, 0));
    }
  }
  dis[0] = 0;
  for (int i = 0; i < (int)(m); i++) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    to[r].push_back(make_pair(l, c));
    to[l].push_back(make_pair(r, c));
  }
  pq.push(0);
  while (!pq.empty()) {
    int k = pq.top();
    pq.pop();
    for (int j = 0; j < to[k].size(); j++) {
      pair<int, int> q = to[k][j];
      if (dis[q.first] > dis[k] + q.second) {
        dis[q.first] = dis[k] + q.second;
        pq.push(q.first);
      }
    }
  }
  if (dis[n - 1] == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << dis[n - 1] << endl;
  return 0;
}
