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::BinaryHeap;

fn main() {
    input!{
        n: usize,
        m: usize,
        es: [(usize1, usize1); m],
        s: usize1,
        t: usize1
    }
    let mut g: Vec<Vec<usize>> = vec![vec![]; n];
    for &(u, v) in &es {
        g[u].push(v);
    }
    if let Some(ans) = dijkstra(s, t, &g) {
        println!("{}", ans);
    } else {
        println!("-1");
    }
}

use std::cmp::Ordering;

#[derive(Debug, PartialEq, Eq)]
struct DijkstraState {
    v: usize,
    w: usize
}

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

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

fn dijkstra(s: usize, t: usize, g: &Vec<Vec<usize>>) -> Option<usize> {
    let n = g.len();
    let mut prev = vec![vec![usize::max_value(); 3]; n];
    let mut pq = BinaryHeap::new();
    pq.push(DijkstraState{v: s, w: 0});
    prev[s][0] = 0;
    while let Some(s) = pq.pop() {
        if prev[s.v][s.w % 3] < s.w {
            continue;
        }
        if s.v == t && s.w % 3 == 0 {
            return Some(s.w / 3);
        }
        for &v in &g[s.v] {
            let d = s.w + 1;
            if prev[v][d % 3] > d {
                prev[v][d % 3] = d;
                pq.push(DijkstraState{v: v, w: d});
            }
        }
    }
    None
}


Measured memory_bytes: 20732