#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
struct Edge {
  int from, to;
  long long cost;
  Edge(int _to, long long _cost) : from(-1), to(_to), cost(_cost) {}
  Edge(int _from, int _to, long long _cost)
      : from(_from), to(_to), cost(_cost) {}
};
template <class T>
vector<T> Dijkstra(const vector<vector<Edge>> &G, int s, T def) {
  using P = pair<T, int>;
  priority_queue<P, vector<P>, greater<P>> pq;
  vector<T> dist((int)G.size(), def);
  pq.push({0, s});
  dist[s] = 0;
  while (!pq.empty()) {
    int cur = pq.top().second;
    pq.pop();
    for (auto &e : G[cur]) {
      if (dist[e.to] > dist[cur] + e.cost) {
        dist[e.to] = dist[cur] + e.cost;
        pq.push({dist[e.to], e.to});
      }
    }
  }
  return dist;
}
void solve() {
  int N, M;
  cin >> N >> M;
  vector<vector<Edge>> G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    long long c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
    if (i) G[i].emplace_back(i - 1, 0LL);
  }
  auto dist = Dijkstra(G, 0, INF);
  if (dist[N - 1] == INF)
    cout << -1 << '\n';
  else
    cout << dist[N - 1] << '\n';
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
