extern crate core;

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

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(Clone)]
struct CharSequence {
    chars: Vec<char>
}
impl CharSequence {
    fn new(str: &String) -> CharSequence {
        CharSequence{chars: str.chars().collect()}
    }
    fn replaced(&self, from: &Vec<char>, to: &Vec<char>) -> CharSequence {
        let mut chars = Vec::with_capacity(self.chars.len());
        let mut c = 0;
        while c < self.chars.len() {
            let mut is_match = c + from.len() <= self.chars.len();
            if is_match {
                for j in 0..from.len() {
                    if self.chars[c + j] != from[j] {
                        is_match = false;
                        break
                    }
                }
            }
            if is_match {
                for j in 0 .. to.len() {
                    chars.push(to[j]);
                }
                c += from.len()
            }else {
                chars.push(self.chars[c]);
                c += 1;
            }
        }
        CharSequence{chars: chars}
    }
}
impl Ord for CharSequence {
    fn cmp(&self, other: &Self) -> Ordering {
        for i in 0 .. min(self.chars.len(), other.chars.len()) {
            match self.chars[i].cmp(&other.chars[i]) {
                Ordering::Equal => {}
                e => {return e}
            }
        }
        self.chars.len().cmp(&other.chars.len())
    }
}
impl PartialOrd for CharSequence {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(&other))
    }
}
impl PartialEq for CharSequence {
    fn eq(&self, other: &Self) -> bool {
        self.chars.len() == other.chars.len() && self.cmp(&other) == Ordering::Equal
    }
}
impl Eq for CharSequence{}
struct Tuple<T1, T2>{
    first: T1, second: T2,
}
fn main() {
    loop {
        let_all!(n: usize);
        if n == 0 {
            break
        }
        let mut replacement: Vec<Tuple<Vec<char>, Vec<char>>> = Vec::with_capacity(n);
        for _ in 0 .. n {
            let_all!(a: String, b: String);
            replacement.push(Tuple{first: a.chars().collect(), second:b.chars().collect()});
        }
        let start = CharSequence::new(&read_line!());
        let goal = CharSequence::new(&read_line!());
        let mut queue = VecDeque::new();
        queue.push_back(start);
        let mut is_searching = BTreeSet::new();
        let mut make_count = -1;
        let mut count = 1;
        while !queue.is_empty() && make_count == -1 {
            for _ in 0 .. queue.len() {
                let peek = queue.pop_front().unwrap();
                for tuple in &replacement {
                    let replaced = peek.replaced(&tuple.first, &tuple.second);
                    if replaced == goal {
                        make_count = count;
                        break
                    }else if replaced.chars.len() < goal.chars.len() && is_searching.insert(replaced.clone()){
                        queue.push_back(replaced);
                    }
                }
                if make_count != - 1{
                    break
                }
            }
            count += 1;
        }
        println!("{}", make_count)
    }
}



Measured memory_bytes: 5052