#include <bits/stdc++.h>
using namespace std;
const int MAX = 100000 + 10;
const int INF = 1000000000;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    q;
vector<pair<int, int>> v[MAX];
int dis[MAX];
bool vis[MAX];
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 2; i <= n; i++) v[i].push_back(make_pair(i - 1, 0));
  for (int i = 0; i < m; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    v[x].push_back(make_pair(y, z));
    v[y].push_back(make_pair(x, z));
  }
  dis[1] = 0;
  vis[0] = false;
  for (int i = 2; i <= n; i++) dis[i] = INF, vis[i] = false;
  q.push(make_pair(0, 1));
  while (!q.empty()) {
    while (!q.empty() && vis[q.top().second]) q.pop();
    if (q.empty()) break;
    pair<int, int> tmp = q.top();
    q.pop();
    int nn = tmp.second;
    int ss = tmp.first;
    vis[nn] = true;
    for (int i = 0; i < v[nn].size(); i++) {
      int tar = v[nn][i].first;
      int dd = ss + v[nn][i].second;
      if (dd < dis[tar]) {
        dis[tar] = dd;
        q.push(make_pair(dd, tar));
      }
    }
  }
  printf("%d\n", dis[n] == INF ? -1 : dis[n]);
  return 0;
}
