import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] fees = new int[n * n]; for (int i = 0; i < m; i++) { int idx = (sc.nextInt() - 1) * n + sc.nextInt() - 1; fees[idx] = sc.nextInt(); } long[][] costs = new long[2][n * n]; Arrays.fill(costs[0], Long.MAX_VALUE); Arrays.fill(costs[1], Long.MAX_VALUE); PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(0, 0, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.type][p.idx] <= p.value + fees[p.idx]) { continue; } costs[p.type][p.idx] = p.value + fees[p.idx]; if (p.idx / n > 0) { queue.add(new Path(p.idx - n, p.type, p.value + fees[p.idx] + 1)); } if (p.idx / n < n - 1) { queue.add(new Path(p.idx + n, p.type, p.value + fees[p.idx] + 1)); } if (p.idx % n > 0) { queue.add(new Path(p.idx - 1, p.type, p.value + fees[p.idx] + 1)); } if (p.idx % n < n - 1) { queue.add(new Path(p.idx + 1, p.type, p.value + fees[p.idx] + 1)); } if (p.type == 1) { continue; } if (costs[1][p.idx] <= p.value) { continue; } costs[1][p.idx] = p.value; if (p.idx / n > 0) { queue.add(new Path(p.idx - n, 1, p.value + 1)); } if (p.idx / n < n - 1) { queue.add(new Path(p.idx + n, 1, p.value + 1)); } if (p.idx % n > 0) { queue.add(new Path(p.idx - 1, 1, p.value + 1)); } if (p.idx % n < n - 1) { queue.add(new Path(p.idx + 1, 1, p.value + 1)); } } System.out.println(costs[1][n * n - 1]); } static class Path implements Comparable { int idx; int type; long value; public Path(int idx, int type, long value) { this.idx = idx; this.type = type; this.value = value; } public int compareTo(Path another) { if (value == another.value) { return 0; } else if (value < another.value) { return -1; } else { return 1; } } } }