#include <bits/stdc++.h>
using namespace std;
using cd = complex<double>;
const long long INF = 1e15;
const int mod = 998244353;
const double Pi = acos(-1);
void Fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int n, m;
int Dist[100005];
vector<pair<int, int> > Adj[100005];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    pq;
int main() {
  cin >> n >> m;
  Dist[1] = 0;
  for (int i = 2; i <= n; i++) {
    Dist[i] = 1e9;
    Adj[i].emplace_back(make_pair(i - 1, 0));
  }
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    Adj[u].emplace_back(make_pair(v, w));
    Adj[v].emplace_back(make_pair(u, w));
  }
  pq.push(make_pair(0, 1));
  while (pq.empty() == false) {
    int node = pq.top().second, d = pq.top().first;
    pq.pop();
    if (d == Dist[node]) {
      for (auto x : Adj[node]) {
        if (Dist[x.first] > x.second + Dist[node]) {
          Dist[x.first] = x.second + Dist[node];
          pq.push(make_pair(Dist[x.first], x.first));
        }
      }
    }
  }
  if (Dist[n] == 1e9) {
    Dist[n] = -1;
  }
  cout << Dist[n];
}
