#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

double dp[3][3][16];
int n, s, t, b;

int toIndex(char l){
    return l - 'A';
}

double rec(int x, int y, int battery){
    if(dp[y][x][battery] > -1e-6){return dp[y][x][battery];}
    if(x == t % 3 && y == t / 3 && battery == n){return 1.0;}
    if(battery >= n){return 0.0;}

    double res = 0.0;
    REP(i, 4){
        int nx = x + dx[i], ny = y + dy[i];
        if(0 <= nx && nx < 3 &&
           0 <= ny && ny < 3 &&
           !(nx == b % 3 && ny == b / 3)){
            res += 0.25 * rec(nx, ny, battery+1);
        }else{
            res += 0.25 * rec(x, y, battery+1);
        }
    }

    return dp[y][x][battery] = res;
}

int main(){
    while(std::cin >> n, n){
        std::fill(dp[0][0], dp[0][0]+3*3*16, -1.0);

        char sl, tl, bl;
        std::cin >> sl >> tl >> bl;

        s = toIndex(sl), t = toIndex(tl), b = toIndex(bl);

        printf("%.7f\n", rec(s%3, s/3, 0));
    }
}