#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e15;
const int MAXN = 2e5 + 5;
const int MAXT = MAXN * 4;
const int MAXM = MAXN * 20 * 20;
struct tnode {
  int l, r, cl, cr;
} t[MAXN];
int n, m, M, rt;
int edge, head[MAXT * 2], nex[MAXM], tail[MAXM];
long long w[MAXM], dist[MAXT * 2];
bool use[MAXT * 2];
struct rec {
  int u;
  long long dist;
  bool operator<(const rec& b) const { return dist > b.dist; }
};
priority_queue<rec> q;
vector<int> lis;
void add(int u, int v, long long W) {
  edge++, nex[edge] = head[u], head[u] = edge, tail[edge] = v, w[edge] = W;
}
int newnode(int l, int r, int cl, int cr) {
  t[++M] = (tnode){l, r, cl, cr};
  return M;
}
int build(int l, int r) {
  if (l == r) return newnode(l, r, 0, 0);
  int mid = (l + r) >> 1;
  int id = newnode(l, r, 0, 0);
  int cl = build(l, mid), cr = build(mid + 1, r);
  t[id].cl = cl;
  if (cl) add(2 * cl, 2 * id, 0), add(2 * id + 1, 2 * cl + 1, 0);
  t[id].cr = cr;
  if (cr) add(2 * cr, 2 * id, 0), add(2 * id + 1, 2 * cr + 1, 0);
  return id;
}
void qry(int a, int b, int id) {
  int l = t[id].l, r = t[id].r;
  int cl = t[id].cl, cr = t[id].cr;
  if (l > b || r < a) return;
  if (a <= l && r <= b) {
    lis.push_back(id);
    return;
  }
  qry(a, b, cl);
  qry(a, b, cr);
}
int main() {
  scanf("%d%d", &n, &m);
  rt = build(1, n);
  for (int i = 1; i <= m; i++) {
    int l, r;
    long long x;
    scanf("%d%d%lld", &l, &r, &x);
    lis.clear();
    qry(l, r, rt);
    for (int i = 0; i < lis.size(); i++) {
      int u = lis[i];
      for (int j = 0; j < lis.size(); j++) {
        int v = lis[j];
        add(2 * u, 2 * v + 1, x);
        if (u != v)
          add(2 * v + 1, 2 * u, x);
        else
          add(2 * v + 1, 2 * u, 0);
      }
    }
  }
  for (int i = 1; i <= 2 * M + 3; i++) dist[i] = inf;
  lis.clear();
  qry(1, 1, rt);
  q.push((rec){2 * lis[0], 0});
  dist[2 * lis[0]] = 0;
  while (!q.empty()) {
    rec u = q.top();
    q.pop();
    if (use[u.u]) continue;
    use[u.u] = 1;
    for (int e = head[u.u]; e; e = nex[e]) {
      int v = tail[e];
      if (dist[v] > dist[u.u] + w[e]) {
        dist[v] = dist[u.u] + w[e];
        q.push((rec){v, dist[v]});
      }
    }
  }
  lis.clear();
  qry(n, n, rt);
  long long ans = dist[2 * lis[0] + 1];
  printf("%lld\n", ans == inf ? 1ll : ans);
  return 0;
}
