#[macro_use]
mod input {

use std;
use std::io;

const SPLIT_DELIMITER: char = ' ';

#[macro_export]
#[allow(unused_macros)]
macro_rules! input {
    ( $($x:expr ),*) => {
        {
            let temp_str = input_line_str();
            let mut split_result_iter = temp_str.split_whitespace();
                $(
                let buf_split_result = split_result_iter.next();
                let buf_split_result = buf_split_result.unwrap();
                    ($x) = buf_split_result.parse().unwrap();
                )*
        }
    };
}

#[allow(dead_code)]
pub fn input_line_str() -> String {
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

#[allow(dead_code)]
pub fn p<T>(t: T)
where
    T: std::fmt::Display,
{
    println!("{}", t);
}

#[allow(dead_code)]
pub fn input_vector2d<T>(line: usize) -> Vec<Vec<T>>
where
    T: std::str::FromStr,
{
    let mut v: Vec<Vec<T>> = Vec::new();

    for _ in 0..line {
        let vec_line = input_vector();
        v.push(vec_line);
    }
    v
}

#[allow(dead_code)]
pub fn input_vector<T>() -> Vec<T>
where
    T: std::str::FromStr,
{
    let mut v: Vec<T> = Vec::new();

    let s = input_line_str();
    let split_result = s.split(SPLIT_DELIMITER);
    for z in split_result {
        let buf = match z.parse() {
            Ok(r) => r,
            Err(_) => panic!("Parse Error"),
        };
        v.push(buf);
    }
    v
}

#[allow(dead_code)]
pub fn str2vec(s: &str) -> Vec<char> {
    let mut v: Vec<char> = Vec::new();
    for c in s.chars() {
        v.push(c);
    }
    v
}

}

#[allow(unused_imports)]
use input::*;

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

#[derive(Copy, Clone, Eq, PartialEq)]
struct State {
    cost: usize,
    node: (usize, usize),
}

impl Ord for State {
    fn cmp(&self, other: &State) -> Ordering {
        if self.cost > other.cost {
            Ordering::Less
        } else if self.cost == other.cost {
            Ordering::Equal
        } else {
            Ordering::Greater
        }
    }
}
impl PartialOrd for State {
    fn partial_cmp(&self, other: &State) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

const INF: usize = 999_999_999_999;

fn main() {
    let (n, m): (usize, usize);
    input!(n, m);
    let mut graph = vec![vec![Vec::new(); 3]; n];
    let mut dist = vec![vec![INF; 3]; n];
    let mut used = vec![vec![false; 3]; n];
    let mut heap = BinaryHeap::new();
    for _ in 0..m {
        let (u, v): (usize, usize);
        input!(u, v);
        graph[u - 1][0].push((v - 1, 1));
        graph[u - 1][1].push((v - 1, 2));
        graph[u - 1][2].push((v - 1, 0));
    }
    let (s, t): (usize, usize);
    input!(s, t);
    dist[s - 1][0] = 0;
    for edge in &graph[s - 1][0] {
        heap.push(State {
            cost: 1,
            node: *edge,
        });
        used[edge.0][edge.1] = true;
    }
    while !heap.is_empty() {
        let state = heap.pop().unwrap();
        dist[state.node.0][state.node.1] = state.cost;
        for edge in &graph[state.node.0][state.node.1] {
            if dist[edge.0][edge.1] == INF && !used[edge.0][edge.1] {
                used[edge.0][edge.1] = true;
                heap.push(State {
                    cost: state.cost + 1,
                    node: *edge,
                });
            }
        }
    }
    if dist[t - 1][0] == INF {
        println!("-1");
    } else {
        println!("{}", dist[t - 1][0] / 3);
    }
}

Measured memory_bytes: 47356