#include <bits/stdc++.h>
using namespace std;
template <typename T>
std::vector<T> enum_div(T n) {
  std::vector<T> ret;
  for (T i = 1; i * i <= n; ++i) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i != 1 && i * i != n) {
        ret.push_back(n / i);
      }
    }
  }
  return ret;
}
int N;
string S;
vector<vector<int>> li;
vector<int> siz;
bool judge(int len) {
  map<string, int> s;
  string now = S.substr(0, len);
  for (int i = 0; i < N - len + 1; i++) {
    if (s.find(now) != s.end()) {
      int t = s.find(now)->second;
      if (t + len <= i) {
        return true;
      }
    } else {
      s[now] = i;
    }
    now = S.substr(i + 1, len);
  }
  return false;
}
template <typename T>
struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T>
using Matrix = vector<vector<T>>;
template <typename T>
vector<T> dijkstra(WeightedGraph<T> &g, int s) {
  const auto INF = numeric_limits<T>::max();
  vector<T> dist(g.size(), INF);
  using Pi = pair<T, int>;
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  dist[s] = 0;
  que.emplace(dist[s], s);
  while (!que.empty()) {
    T cost;
    int idx;
    tie(cost, idx) = que.top();
    que.pop();
    if (dist[idx] < cost) continue;
    for (auto &e : g[idx]) {
      auto next_cost = cost + e.cost;
      if (dist[e.to] <= next_cost) continue;
      dist[e.to] = next_cost;
      que.emplace(dist[e.to], e.to);
    }
  }
  return dist;
}
int main() {
  int N, M;
  cin >> N >> M;
  WeightedGraph<long long int> g(N);
  for (int i = 1; i < N; i++) {
    g[i].emplace_back(i - 1, 0);
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    long long int c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a].emplace_back(b, c);
    g[b].emplace_back(a, c);
  }
  long long int dist = dijkstra(g, 0)[N - 1];
  if (dist == numeric_limits<long long int>::max())
    puts("-1");
  else
    printf("%d\n", dist);
  return 0;
}
