#include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(ll i=0;i<(n);++i)
using ll = long long;
constexpr ll INF = (1LL<<60);
constexpr ll MOD = (1e9+7);
template<class T>inline bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
inline void dump(){cout<<endl;}
template<class Head,class... Tail>inline void dump(Head&& head, Tail&&... tail){cout<<head<<", ";dump(forward<Tail>(tail)...);}
template<typename T>inline istream &operator>>(istream&input,vector<T>&v){for(auto &elemnt:v)input>>elemnt;return input;}
template<class T>vector<T> make_vector(size_t a){return vector<T>(a);}
template<class T, class... Ts>auto make_vector(size_t a, Ts... ts){return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));}
using T3 = tuple<ll,ll,ll>;
using T4 = tuple<ll,ll,ll,ll>;

template<class T>
class segtree {
private:
  ll n=1;
  T unit;
  std::vector<T> dat;
  std::function<T(T,T)> func;
public:
  segtree(){}
  segtree(std::vector<T>& a,T v,std::function<T(T,T)> f){
    build(a,v,f);
  }
  void build(std::vector<T>& a,T v,std::function<T(T,T)> f){
    ll sz=a.size();
    unit=v;
    func=f;
    while(n<sz)n<<=1LL;
    dat.resize(2*n-1,unit);
    for(int i=0;i<sz;++i)dat[i+n-1]=a[i];
    for(int i=n-2;i>=0;--i){
      dat[i]=func(dat[i*2+1],dat[i*2+2]);
    }
  }
  void update(ll idx,ll val){
    idx+=n-1;
    dat[idx]=val;
    while(idx){
      idx--;idx>>=1LL;
      dat[idx]=func(dat[idx*2+1],dat[idx*2+2]);
    }
  }
  T query(ll a,ll b,ll k=0,ll l=0,ll r=-1){
    if(r<0)r=n;
    if( b<=l || r<=a )return unit;
    if( a<=l && r<=b )return dat[k];
    else{
      auto left=query(a,b,2*k+1,l,(l+r)/2);
      auto right=query(a,b,2*k+2,(l+r)/2,r);
      return func(left,right);
    }
  }
  T operator[](ll i)const{
    return dat[i+n-1];
  }
};
class Union_Find {
private:
  ll n;
  vector<ll> parent;
public:
  Union_Find(ll v){
    n=v;
    parent.resize(n,-1);
  }
  ll root(ll v){
    if(parent[v]<0)return v;
    return parent[v]=root(parent[v]);
  }
  ll size(ll v){
    return -parent[root(v)];
  }
  bool connect(ll a,ll b){
    a=root(a);
    b=root(b);
    if(a==b)return false;
    if(size(a)<size(b)) swap(a,b);
    parent[a]+=parent[b];
    parent[b]=a;
    return true;
  }
  bool same(ll a,ll b){
    return root(a)==root(b);
  }
};

main(){

  ll n,m;
  cin>>n>>m;
  vector<map<ll,ll>> adj(n);
  rep(i,m){
    ll l,r,c;
    cin>>l>>r>>c;
    l--;r--;
    for(int j=l+1;j<=r;++j){
      if(adj[l].find(j)==adj[l].end()){
        adj[l][j]=c;
        adj[j][l]=c;
      }else{
        chmin(adj[l][j],c);
      }
    }
  }

  using pll = pair<ll,ll>;
  vector<ll> dist(n,INF);
  priority_queue<pll,vector<pll>,greater<>> que;
  que.emplace(0,0);
  while(!que.empty()){
    ll cost,cur;
    tie(cost,cur) = que.top();
    que.pop();
    if( chmin(dist[cur],cost) ){
      for(auto nxt:adj[cur]){
        que.emplace(nxt.second+cost,nxt.first);
      }
    }
  }
  cout<<(dist[n-1]==INF?-1:dist[n-1])<<endl;

}


void __main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n,m;
  cin>>n>>m;
  vector<T3> a(m);
  rep(i,m){
    ll l,r,c;
    cin>>l>>r>>c;
    l--;r--;
    a[i]={c,l,r};
  }
  sort(a.begin(),a.end());
  Union_Find uni(n);
  ll ans = 0;
  rep(i,m){
    ll c,l,r;
    tie(c,l,r) = a[i];
    ans += c;
    for(ll i=l+1;i<=r;++i)uni.connect(l,i);
    if(uni.same(0,n-1))break;
  }
  if(uni.same(0,n-1))cout<<(ans)<<endl;
  else cout<<(-1)<<endl;

}


void _main(){

  ll n,m;
  cin>>n>>m;
  vector<T3> a,b,c;
  vector<T4> d;
  rep(i,m){
    ll l,r,c;
    cin>>l>>r>>c;
    l--;r--;
    a.emplace_back(r,c,l);
    d.emplace_back(l-r,l,c,r);
  }
  sort(a.begin(),a.end());
  sort(d.begin(),d.end());

  vector<ll> infv(n,INF);
  segtree<ll> dist(infv,INF,[](auto a,auto b){return min(a,b);});
  dist.update(0,0);
  for(int i=1;i<n;++i){
    T3 key(i,0,0);
    auto itr = lower_bound(a.begin(),a.end(),key);
    if(itr==a.end())continue;
    ll l,r,c;
    tie(r,c,l) = *itr;
    ll d = dist.query(l,r+1);
    dist.update(i,d+c);
  }
  ll ans = dist[n-1];
  cout<<(ans>=1e15?-1:ans)<<endl;


}