extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign, Range};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet, HashMap};
use std::fmt::Display;

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}

macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}

macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
#[derive(Copy, Clone)]
struct Edge{to: usize, cost: i32}
struct ValueWithKey<V, K> {
    value: V, key: K
}
impl <V, K: Ord> Ord for ValueWithKey<V, K> {
    fn cmp(&self, other: &Self) -> Ordering {
        other.key.cmp(&self.key)
    }
}
impl <V, K: PartialOrd> PartialOrd for ValueWithKey<V, K> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.key.partial_cmp(&self.key)
    }
}
impl <V, K: PartialEq> PartialEq for ValueWithKey<V, K> {
    fn eq(&self, other: &Self) -> bool {
        other.key.eq(&self.key)
    }
}
impl <V, K: Eq> Eq for ValueWithKey<V, K>{}
impl <V: Copy, K: Copy> Copy for ValueWithKey<V, K> {}
impl <V: Clone, K: Clone> Clone for ValueWithKey<V, K> {
    fn clone(&self) -> Self {
        ValueWithKey{value: self.value.clone(), key: self.key.clone()}
    }
}
fn main() {
    loop {
        let_all!(n: usize, m: usize);
        if n == 0 {
            break
        }
        let mut nodes: Vec<Vec<Edge>> = vec![Vec::new(); n];
        for _ in 0 .. m {
            let_all!(a: usize, b: usize, c: i32);
            nodes[a - 1].push(Edge{to: b - 1, cost: c});
            nodes[b - 1].push(Edge{to: a - 1, cost: c});
        }
        let mut min_cost = vec![vec![std::i32::MAX;2];n];
        let mut queue = BinaryHeap::new();
        min_cost[0][0] = 0;
        queue.push(ValueWithKey{value: (0, 0), key: 0});
        while let Some(ValueWithKey{value: (current, p), key: cost}) = queue.pop() {
            if current == n - 1 {
                println!("{}", cost);
                break
            }
            if min_cost[current][p] == cost {
                for &e in &nodes[current] {
                    if min_cost[e.to][p] > cost + e.cost {
                        min_cost[e.to][p] = cost + e.cost;
                        queue.push(ValueWithKey{value: (e.to, p), key: cost + e.cost});
                    }
                    if p == 0 {
                        for &e2 in &nodes[e.to] {
                            if min_cost[e2.to][1] > cost {
                                min_cost[e2.to][1] = cost;
                                queue.push(ValueWithKey{value: (e2.to, 1), key: cost});
                            }
                        }
                    }
                }
            }
        }
    }
}


Measured memory_bytes: 5060