#include <bits/stdc++.h>
using namespace std;
const int MAXN = 123456;
vector<pair<int, int> > G[MAXN];
long long dis[MAXN];
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    int L, R, C;
    scanf("%d %d %d", &L, &R, &C);
    G[L].push_back(make_pair(R, C));
  }
  set<int> st;
  st.insert(1);
  for (int L = 1; L <= N; L++) {
    for (auto j : G[L]) {
      int R = j.first, C = j.second;
      auto it = st.lower_bound(L);
      if (it == st.end()) break;
      long long cur = dis[*it] + C;
      for (auto k = st.lower_bound(L); k != st.upper_bound(R);) {
        if (*k <= R && cur <= dis[*k])
          k = st.erase(k);
        else
          k++;
      }
      it = st.lower_bound(R);
      if (it == st.end() || dis[*it] > cur) {
        st.insert(R);
        dis[R] = cur;
      }
    }
  }
  if (st.find(N) == st.end())
    return !printf("-1\n");
  else
    printf("%lld\n", dis[N]);
  return 0;
}
