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, Default)]
    struct BitSet {
        state: i32
    }
    impl BitSet {
        fn added(&self, position: usize) -> BitSet {
            BitSet{state: self.state | (1 << position as i32)}
        }
        fn removed(&self, position: usize) -> BitSet {
            BitSet{state: self.state & !(1 << position as i32)}
        }
        fn is_added(&self, position: usize) -> bool {
            self.state & (1 << position as i32) != 0
        }
        fn is_empty(&self) -> bool {
            self.state == 0
        }
        fn empty() -> BitSet {
            BitSet{state: 0}
        }
    }
    impl IntoIterator for BitSet {
        type Item = usize;
        type IntoIter = BitSetIterator;
        fn into_iter(self) -> Self::IntoIter {
            BitSetIterator{state: self.state, position: 0}
        }
    }
    struct BitSetIterator {
        state: i32, position: i32
    }
    impl Iterator for BitSetIterator {
        type Item = usize;
        fn next(&mut self) -> Option<Self::Item> {
            if self.state == 0 {
                None
            }else if self.state % 2 == 0 {
                self.state /= 2;
                self.position += 1;
                self.next()
            }else {
                let position = self.position as usize;
                self.state /= 2;
                self.position += 1;
                Some(position)
            }
        }
    }
    fn main() {
        let_all!(n: usize, m: usize, k: usize);
        let mut dark_room = read_line!(' '; usize);
        let mut rooms = vec![n; n];
        for i in 0 .. dark_room.len() {
            dark_room[i] -= 1;
            rooms[dark_room[i]] = i;
        }
        let mut path = Vec::with_capacity(n);
        for _ in 0 .. n {
            let mut p: Vec<usize> = read_line!(' '; usize);
            for mut r in &mut p {
                *r -= 1;
            }
            path.push(p);
        }
        let mut queue = VecDeque::new();
        queue.push_back(BitSet{state: (1 << m as i32) - 1});
        let mut count = 0;
        let mut is_added = vec![false; 1 << m];
        while !queue.is_empty() {
            count += 1;
            for _ in 0 .. queue.len() {
                if let Some(set) = queue.pop_front() {
                    for i in 0 .. k {
                        let mut next = BitSet::empty();
                        for c in set {
                            if rooms[path[dark_room[c]][i]] != n {
                                next = next.added(rooms[path[dark_room[c]][i]]);
                            }
                        }
                        if next.is_empty() {
                            println!("{}", count);
                            return
                        }
                        if !is_added[next.state as usize] {
                            is_added[next.state as usize] = true;
                            queue.push_back(next);
                        }
                    }
                }
            }
        }
    }


Measured memory_bytes: 5052