loop do
    n = gets.to_i   
    break if n == 0
    rules = (1..n).map { gets.chomp.split }
    start = gets.chomp
    goal = gets.chomp

    queue = [start]
    k = 0 
    ok = false
    catch(:exit) do
        loop do
            next_queue = []
            until queue.empty?
                s = queue.pop
                if s == goal
                    ok = true
                    throw :exit
                end 
                next if s.size >= goal.size
                rules.each do |from, to|
                    if s =~ /#{from}/
                        next_queue << s.gsub(from, to) 
                    end
                end
            end 
            queue = next_queue
            break if queue.empty?
            k += 1
        end 
    end 
    puts ok ? k : -1
end