macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};
use std::collections::HashMap;

fn main() {
    input!{
        n: usize,
        m: usize,
        v: [(usize1, usize1, usize); m]
    }
    let mut g: Vec<Vec<(usize, usize)>> = Vec::new();
    let mut map = HashMap::new();
    map.insert((0, 0), 0);
    map.insert((n - 1, 0), 1);
    g.push(Vec::new());
    g.push(Vec::new());
    for &(a, b, c) in &v {
        let mut ac;
        if let Some(&t) = map.get(&(a, c)) {
            ac = t;
        } else {
            ac = g.len();
            map.insert((a, c), ac);
            g.push(Vec::new());
        }
        let mut a0;
        if let Some(&t) = map.get(&(a, 0)) {
            a0 = t;
        } else {
            a0 = g.len();
            map.insert((a, 0), a0);
            g.push(Vec::new());
        }
        let mut bc;
        if let Some(&t) = map.get(&(b, c)) {
            bc = t;
        } else {
            bc = g.len();
            map.insert((b, c), bc);
            g.push(Vec::new());
        }
        let mut b0;
        if let Some(&t) = map.get(&(b, 0)) {
            b0 = t;
        } else {
            b0 = g.len();
            map.insert((b, 0), b0);
            g.push(Vec::new());
        }
        g[ac].push((bc, 0));
        g[ac].push((a0, 0));
        g[a0].push((ac, 1));
        g[bc].push((ac, 0));
        g[bc].push((b0, 0));
        g[b0].push((bc, 1));
    }
    if let Some(ans) = dijkstra(0, 1, &g) {
        println!("{}", ans);
    } else {
        println!("-1");
    }
}

use std::collections::BinaryHeap;
use std::cmp::Ordering;

#[derive(Debug, PartialEq, Eq)]
struct State(usize, usize);

impl Ord for State {
    fn cmp(&self, other: &Self) -> Ordering {
        other.1.cmp(&self.1)
    }
}

impl PartialOrd for State {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

fn dijkstra(s: usize, t: usize, g: &Vec<Vec<(usize, usize)>>) -> Option<usize> {
    let n = g.len();
    let mut pq = BinaryHeap::new();
    let mut dist = vec![usize::max_value(); n];
    pq.push(State(s, 0));
    dist[s] = 0;
    while let Some(st) = pq.pop() {
        if st.0 == t {
            return Some(st.1);
        }
        for &(q, c) in &g[st.0] {
            let d = st.1+ c;
            if dist[q] > d {
                dist[q] = d;
                pq.push(State(q, d));
            }
        }
    }
    None
}


Measured memory_bytes: 104700