import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 2); int n = Integer.parseInt(first[0]); int m = Integer.parseInt(first[1]); HashMap places = new HashMap<>(); for (int i = 0; i < m; i++) { String[] line = br.readLine().split(" ", 3); places.put((Integer.parseInt(line[0]) - 1) * n + Integer.parseInt(line[1]) - 1, Integer.parseInt(line[2])); } PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(0, 0, 0)); long[][] costs = new long[2][n * n]; Arrays.fill(costs[0], Long.MAX_VALUE); Arrays.fill(costs[1], Long.MAX_VALUE); while (queue.size() > 0) { Path p = queue.poll(); int add = places.getOrDefault(p.idx, 0); if (costs[p.level][p.idx] <= p.value + add) { continue; } costs[p.level][p.idx] = p.value + add; if (p.idx % n > 0) { queue.add(new Path(p.idx - 1, p.level, p.value + 1 + add)); } if (p.idx % n < n - 1) { queue.add(new Path(p.idx + 1, p.level, p.value + 1 + add)); } if (p.idx / n > 0) { queue.add(new Path(p.idx - n, p.level, p.value + 1 + add)); } if (p.idx / n < n - 1) { queue.add(new Path(p.idx + n, p.level, p.value + 1 + add)); } if (p.level > 0) { 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 - 1, 1, p.value + 1)); } if (p.idx % n < n - 1) { queue.add(new Path(p.idx + 1, 1, p.value + 1)); } 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)); } } System.out.println(costs[1][n * n - 1]); } static class Path implements Comparable { int idx; int level; long value; public Path(int idx, int level, long value) { this.idx = idx; this.level = level; this.value = value; } public int compareTo(Path another) { if (value == another.value) { return 0; } else if (value < another.value) { return -1; } else { return 1; } } } }