#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
int n, m;
int nod[2][N], C, s, t;
vector<pair<int, int> > g[1300000 + 10];
void build(int id, int l, int r, int rt) {
  nod[id][rt] = ++C;
  if (l == r) {
    if (id == 0 && l == 1) s = nod[id][rt];
    if (id == 1 && l == n) t = nod[id][rt];
    if (id == 1) {
      g[nod[id][rt]].push_back(make_pair(nod[0][rt], 0));
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(id, l, mid, rt << 1);
  build(id, mid + 1, r, rt << 1 | 1);
  if (id == 0) {
    g[nod[id][rt << 1]].push_back(make_pair(nod[id][rt], 0));
    g[nod[id][rt << 1 | 1]].push_back(make_pair(nod[id][rt], 0));
  } else {
    g[nod[id][rt]].push_back(make_pair(nod[id][rt << 1], 0));
    g[nod[id][rt]].push_back(make_pair(nod[id][rt << 1 | 1], 0));
  }
}
void split(int id, int to, int l, int r, int rt, int L, int R) {
  if (L <= l && r <= R) {
    if (id == 0) {
      g[nod[id][rt]].push_back(make_pair(to, 0));
    } else {
      g[to].push_back(make_pair(nod[id][rt], 0));
    }
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) split(id, to, l, mid, rt << 1, L, R);
  if (R > mid) split(id, to, mid + 1, r, rt << 1 | 1, L, R);
}
struct Nod {
  long long dis;
  int u;
  bool operator<(const Nod& o) const { return dis > o.dis; }
};
long long dis[1300000 + 10];
void dijkstra() {
  priority_queue<Nod> q;
  q.push((Nod){0, s});
  memset(dis, -1, sizeof(dis));
  dis[s] = 0;
  while (q.size()) {
    Nod tmp = q.top();
    q.pop();
    int u = tmp.u;
    if (dis[u] < tmp.dis) continue;
    for (auto e : g[u]) {
      int v = e.first;
      if (dis[v] == -1 || dis[v] > dis[u] + e.second) {
        dis[v] = dis[u] + e.second;
        q.push((Nod){dis[v], v});
      }
    }
  }
  printf("%lld\n", dis[t]);
}
int main() {
  scanf("%d%d", &n, &m);
  build(0, 1, n, 1);
  build(1, 1, n, 1);
  for (int i = 1; i <= m; i++) {
    int l, r, c;
    scanf("%d%d%d", &l, &r, &c);
    {
      int x = ++C, y = ++C;
      split(0, x, 1, n, 1, l, r);
      g[x].push_back(make_pair(y, c));
      split(1, y, 1, n, 1, l, r);
    }
    {
      int x = ++C, y = ++C;
      split(0, y, 1, n, 1, l, r);
      g[y].push_back(make_pair(x, c));
      split(1, x, 1, n, 1, l, r);
    }
  }
  dijkstra();
}
