import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); ArrayList> graph = new ArrayList<>(); Path[] pathes = new Path[m * 2]; for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; int c = sc.nextInt(); int d = sc.nextInt(); pathes[i * 2] = new Path(i * 2, b, a, c, d); graph.get(a).add(pathes[i * 2]); pathes[i * 2 + 1] = new Path(i * 2 + 1, a, b, c, d); graph.get(b).add(pathes[i * 2 + 1]); } PriorityQueue queue = new PriorityQueue<>(); queue.add(new Path(-1, 0, -1, 0, 0)); long[] costs1 = new long[n]; Arrays.fill(costs1, Long.MAX_VALUE); int[] idxes = new int[n]; int[] pIdxes = new int[n]; while (queue.size() > 0) { Path p = queue.poll(); if (costs1[p.idx] <= p.value) { continue; } costs1[p.idx] = p.value; idxes[p.idx] = p.last; pIdxes[p.idx] = p.path; for (Path x : graph.get(p.idx)) { queue.add(new Path(x.path, x.idx, p.idx, p.value + x.value, 0)); } } int current = n - 1; while (current > 0) { int p = pIdxes[current]; if (p % 2 == 0) { pathes[p].up(); pathes[p + 1].up(); } else { pathes[p].up(); pathes[p + 1].up(); } current = idxes[current]; } long[] costs2 = new long[n]; Arrays.fill(costs2, Long.MAX_VALUE); queue.add(new Path(n - 1, costs1[n - 1])); while (queue.size() > 0) { Path p = queue.poll(); if (costs2[p.idx] <= p.value) { continue; } costs2[p.idx] = p.value; for (Path x : graph.get(p.idx)) { queue.add(new Path(x.idx, p.value + x.value)); } } System.out.println(costs2[0]); } static class Path implements Comparable { int path; int idx; int last; long value; int sub; public Path(int path, int idx, int last, long value, int sub) { this.path = path; this.idx = idx; this.last = last; this.value = value; this.sub = sub; } public Path(int idx, long value) { this.idx = idx; this.value = value; } public int compareTo(Path another) { if (value == another.value) { return 0; } else if (value < another.value) { return -1; } else { return 1; } } public void up() { value = sub; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }