import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.PriorityQueue; public class Main { static int n, n2; static int[] c; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); n = Integer.parseInt(sa[0]); int m = Integer.parseInt(sa[1]); n2 = n * n; c = new int[n2]; for (int i = 0; i < m; i++) { sa = br.readLine().split(" "); int h = Integer.parseInt(sa[0]) - 1; int w = Integer.parseInt(sa[1]) - 1; c[h * n + w] = Integer.parseInt(sa[2]); } br.close(); long[] d = new long[n2]; Arrays.fill(d, Long.MAX_VALUE); d[0] = 0; PriorityQueue que = new PriorityQueue(); Node first = new Node(0, 0); que.add(first); dijkstra(d, que); long[] d2 = new long[n2]; for (int i = 0; i < n2; i++) { d2[i] = d[i] - c[i]; } que = new PriorityQueue(); for (int i = 1; i < n2 - 1; i++) { que.add(new Node(i, d2[i])); } dijkstra(d2, que); System.out.println(d2[n2 - 1]); } static void dijkstra(long[] d, PriorityQueue que) { int[] dx = {1, -1, n, -n}; while (!que.isEmpty()) { Node cur = que.poll(); if (cur.d > d[cur.v]) { continue; } for (int dd : dx) { int next = cur.v + dd; if (next < 0 || n2 <= next) { continue; } if (next / n != cur.v / n && (dd == -1 || dd == 1)) { continue; } long alt = d[cur.v] + 1 + c[next]; if (alt < d[next]) { d[next] = alt; que.add(new Node(next, alt)); } } } } static class Node implements Comparable { int v; long d; public Node(int v, long d) { this.v = v; this.d = d; } public int compareTo(Node o) { return Long.compare(d, o.d); } } }