require 'set'
def solve(str1, str2, words)
  dp = 10.times.map{ Set.new }
  dp[0] << str1
  (1..9).each do |count|
    dp[count - 1].each do |w|
      words.each do |s1, s2|
        if w.include?(s1)
          word = w.gsub(s1, s2)
          return count if word == str2
          dp[count] << word if word.length <= 10
        end
      end
    end
    return -1 if dp[count].all?{ |w| w.length > 10 }
  end
  -1
end

while n = gets.to_i
  break if n == 0
  words = n.times.map{ gets.split }
  str1 = gets.chomp
  str2 = gets.chomp
  puts solve(str1, str2, words)
end