#include <bits/stdc++.h>
const long long mod = 1e9 + 7;
const long long INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);
using namespace std;
void dijkstra(int s, vector<vector<long long>> &to,
              vector<vector<long long>> &cost, vector<long long> &d) {
  d[s] = 0;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  q.push({0, s});
  while (!q.empty()) {
    pair<long long, long long> now = q.top();
    q.pop();
    for (int i = 0; i < to[now.second].size(); i++) {
      int next = to[now.second][i];
      if (d[next] > now.first + cost[now.second][i]) {
        d[next] = now.first + cost[now.second][i];
        q.push({d[next], next});
      }
    }
  }
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N, M;
  cin >> N >> M;
  vector<long long> L(M), R(M), C(M);
  vector<vector<long long>> to(N + 1), cost(N + 1);
  vector<long long> d(N + 1, 2e12);
  for (int i = 0; i < (M); i++) {
    cin >> L[i] >> R[i] >> C[i];
    to[L[i]].push_back(R[i]);
    cost[L[i]].push_back(C[i]);
    to[R[i]].push_back(L[i]);
    cost[R[i]].push_back(C[i]);
  }
  for (int i = 0; i < (N); i++) {
    to[i + 1].push_back(i);
    cost[i + 1].push_back(0);
  }
  dijkstra(1, to, cost, d);
  cout << (d[N] != 2e12 ? d[N] : -1) << endl;
}
