# encoding: utf-8                                                            

$map = {
  :A => [1, 1], :B => [1, 2], :C => [1, 3],
  :D => [2, 1], :E => [2, 2], :F => [2, 3],
  :G => [3, 1], :H => [3, 2], :I => [3, 3]
}
 
def solve(n, s, b, j)
  s, b, j = s.to_sym, b.to_sym, j.to_sym
  dis = ($map[s][0] - $map[b][0]).abs + ($map[s][1] - $map[b][1]).abs
  if dis > n
    0.0
  else
    0.25 ** dis
  end
end

while n = gets
  n = n.to_i  
  break if n == 0
  s, b, j = gets.rstrip.split(/\s/)
  puts ("%.8f" % solve(n, s, b, j))
end