index = "ABCDEFGHI".index
dd = ((-1, 0), (0, -1), (1, 0), (0, 1))
while 1:
    N = int(input())
    if N == 0:
        break
    s, t, b = map(index, input().split())
    S = [[0]*3 for i in range(3)]
    sy, sx = divmod(s, 3)
    ty, tx = divmod(t, 3)
    by, bx = divmod(b, 3)
    T = [[0]*3 for i in range(3)]
    S[sy][sx] = 1
    for i in range(N):
        for y in range(3):
            for x in range(3):
                if x == bx and y == by:
                    T[y][x] = 0
                    continue
                r = 0
                for dx, dy in dd:
                    nx = x + dx; ny = y + dy
                    if not 0 <= nx < 3 or not 0 <= ny < 3 or (bx == nx and by == ny):
                        nx = x; ny = y
                    r += S[ny][nx]
                T[y][x] = r
        S, T = T, S
    su = sum(sum(e) for e in S)
    print("%.15f" % (S[ty][tx] / su))

