Problem: We have N points numbered 1 to N arranged in a line in this order.

Takahashi decides to make an undirected graph, using these points as the vertices. In the beginning, the graph has no edge. Takahashi will do M operations to add edges in this graph. The i-th operation is as follows:

* The operation uses integers L_i and R_i between 1 and N (inclusive), and a positive integer C_i. For every pair of integers (s, t) such that L_i \leq s < t \leq R_i, add an edge of length C_i between Vertex s and Vertex t.



The integers L_1, ..., L_M, R_1, ..., R_M, C_1, ..., C_M are all given as input.

Takahashi wants to solve the shortest path problem in the final graph obtained. Find the length of the shortest path from Vertex 1 to Vertex N in the final graph.

Constraints

* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq L_i < R_i \leq N
* 1 \leq C_i \leq 10^9

Input

Input is given from Standard Input in the following format:


N M
L_1 R_1 C_1
:
L_M R_M C_M


Output

Print the length of the shortest path from Vertex 1 to Vertex N in the final graph. If there is no shortest path, print `-1` instead.

Examples

Input

4 3
1 3 2
2 4 3
1 4 6


Output

5


Input

4 2
1 2 1
3 4 2


Output

-1


Input

10 7
1 5 18
3 4 8
1 3 5
4 7 10
5 9 8
6 10 5
8 10 3


Output

28

Solution (in JAVA):

Solution:
/**
 * Created at 21:34 on 2019-11-09
 */

import java.io.*;
import java.util.*;

public class Main {

  static FastScanner sc = new FastScanner();
  static Output out = new Output(System.out);

  static final int[] dx = {0, 1, 0, -1};
  static final int[] dy = {-1, 0, 1, 0};

  static final long MOD = (long) (1e9 + 7);
  static final long INF = Long.MAX_VALUE / 2;

  static final int e5 = (int) 1e5;

  public static class Solver {
    public Solver() {

      int N = sc.nextInt();
      int M = sc.nextInt();

      long[] initial = new long[N];
      Arrays.fill(initial, INF);
      initial[0] = 0;
      SegmentTree seg = new SegmentTree(initial);

      Range[] ranges = new Range[M];
      for (int i=0; i<M; i++) {
        int l = sc.nextInt()-1;
        int r = sc.nextInt()-1;
        long c = sc.nextLong();
        ranges[i] = new Range(l, r, c);
      }

      Arrays.sort(ranges, (o1, o2) -> {
        if (o1.l == o2.l) {
          return o2.r - o1.r;
        } else {
          return o1.l - o2.l;
        }
      });

      for (int i=0; i<M; i++) {
        int l = ranges[i].l;
        int r = ranges[i].r;
        long c = ranges[i].c;
        long min = seg.query(l, r+1).val;
        seg.put(l, r+1, min+c);
      }

      long ans = seg.query(N-1, N).val;
      out.println(ans == INF ? -1 : ans);

    }

    public class Range {
      int l, r;
      long c;
      Range(int l, int r, long c) {
        this.l = l;
        this.r = r;
        this.c = c;
      }
    }

    public static class SegmentTree {

      static final long INF = Long.MAX_VALUE / 2;

      Node[] tree;
      int N = 1; //元配列の要素数以上で最小の2の冪乗
      int size; //木のサイズ
      Node initialNode;

      public static class Node {
        static long initialVal = INF;
        static long initialLazy = INF;
        boolean isLazy = false;
        long val;
        long lazy;

        public Node() {
          val = initialVal;
          lazy = initialLazy;
        }
        public Node(long val) {
          this.val = val;
          lazy = initialLazy;
        }

        public Node operation(Node nl, Node nr) {
          val = Math.min(nl.val, nr.val);
          return this;
        }
        public void prop(Node par) {
          if (!par.isLazy) return;
          lazy = Math.min(lazy, par.lazy); //changed
          isLazy = true;
        }
        public void prop(long x, int l, int r) {
          lazy = Math.min(lazy, x); //changed
          isLazy = true;
        }
        public void update() {
          if (!isLazy) return;
          val = Math.min(lazy, val); //changed
          lazy = initialLazy;
          isLazy = false;
        }
      }

      public void update(int k, long x) {
        k += N-1;
        tree[k] = new Node(x);

        while(k != 0) {
          k = parent(k);
          tree[k].operation(tree[childL(k)], tree[childR(k)]);
        }
      }

      private void prop(int k) {
        if (childL(k) < size) { //葉じゃなければ
          tree[childL(k)].prop(tree[k]);
          tree[childR(k)].prop(tree[k]);
        }
        tree[k].update();
      }

      public void put(int a, int b, long x) {
        put(a, b, 0, 0, N, x);
      }

      private void put(int a, int b, int k, int l, int r, long x) {
        prop(k);

        if (r <= a || b <= l) return; //区間外
        if (a <= l && r <= b) { //区間に完全に含まれる
          tree[k].prop(x, l, r);
          prop(k);
        } else { //一部区間外
          put(a, b, childL(k), l, (l + r) / 2, x);
          put(a, b, childR(k), (l + r) / 2, r, x);
          tree[k].operation(tree[childL(k)], tree[childR(k)]);
        }
      }

      public SegmentTree(long[] A) {
        initialNode = new Node();

        //元配列の要素数が2の冪乗でない場合,2の冪乗サイズに拡大して初期値をつめておく
        while (N < A.length) N *= 2;

        size = N*2-1;
        tree = new Node[size];

        for (int i=0; i<size; i++) {
          tree[i] = new Node();
        }

        for (int i=0; i<A.length; i++) {
          update(i, A[i]);
        }

      }

      public Node query(int a, int b) {
        return query(a, b, 0, 0, N);
      }

      private Node query(int a, int b, int k, int l, int r) {
        prop(k);

        if (r <= a || b <= l) return initialNode;
        if (a <= l && r <= b) return tree[k];

        return new Node().operation(query(a, b, childL(k), l, (l+r)/2), query(a, b, childR(k), (l+r)/2, r));
      }

      private int parent(int k) {
        return k % 2 == 0 ? (k-2)/2 : (k-1)/2;
      }

      private int childL(int k) {
        return 2*k+1;
      }

      private int childR(int k) {
        return 2*k+2;
      }
    }

  }

  public static void main(String[] args) {
    new Solver();
    out.flush();
  }

  static class FastScanner {
    private InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;


    public void load() {
      try {
        in = new FileInputStream(next());
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    private boolean hasNextByte() {
      if (ptr < buflen) {
        return true;
      } else {
        ptr = 0;
        try {
          buflen = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (buflen <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    public boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    public long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    public int nextInt() {
      return (int) nextLong();
    }

    public int[] nextIntArray(int N, boolean oneBased) {
      if (oneBased) {
        int[] array = new int[N + 1];
        for (int i = 1; i <= N; i++) {
          array[i] = sc.nextInt();
        }
        return array;
      } else {
        int[] array = new int[N];
        for (int i = 0; i < N; i++) {
          array[i] = sc.nextInt();
        }
        return array;
      }
    }

    public long[] nextLongArray(int N, boolean oneBased) {
      if (oneBased) {
        long[] array = new long[N + 1];
        for (int i = 1; i <= N; i++) {
          array[i] = sc.nextLong();
        }
        return array;
      } else {
        long[] array = new long[N];
        for (int i = 0; i < N; i++) {
          array[i] = sc.nextLong();
        }
        return array;
      }
    }
  }

  static class Output extends PrintWriter {

    private long startTime;

    public Output(PrintStream ps) {
      super(ps);
    }

    public void print(int[] a, String separator) {
      for (int i = 0; i < a.length; i++) {
        if (i == 0) print(a[i]);
        else print(separator + a[i]);
      }
      println();
    }

    public void print(long[] a, String separator) {
      for (int i = 0; i < a.length; i++) {
        if (i == 0) print(a[i]);
        else print(separator + a[i]);
      }
      println();
    }

    public void print(String[] a, String separator) {
      for (int i = 0; i < a.length; i++) {
        if (i == 0) print(a[i]);
        else print(separator + a[i]);
      }
      println();
    }

    public void print(ArrayList a, String separator) {
      for (int i = 0; i < a.size(); i++) {
        if (i == 0) print(a.get(i));
        else print(separator + a.get(i));
      }
      println();
    }

    public void start() {
      startTime = System.currentTimeMillis();
    }

    public void time(String s) {
      long time = System.currentTimeMillis() - startTime;
      println(s + "(" + time + ")");
    }

  }

}