#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory> 
#include <string> 
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>

#define REP(i,n) for(i=0; i < (n); i++)
#define REPONE(i, n) for(i=1; i <= (n); i++)
#define LOOP(n) for(int loopCount=1; loopCount <= (n); loopCount++)
#define ITER(c) __typeof((c).begin())
#define EACH(c,it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define SZ(c) ((int) (c).size())
#define ALL(c) c.begin(), c.end()
#define SUM(c) accumulate(ALL(c), 0)
#define EXIST(c,v) (find(ALL(c), (v)) != (c).end())
#define PB push_back
#define MP make_pair
#define fs first
#define sc second

using namespace std;
static const double EPS = 1e-9;
static const double PI = 3.141592653589793238462643383279;
typedef long long ll;

pair<int, int> toP(char c){
    int x = c - 'A';
    return MP(x/3, x%3);
}

bool isOut(int x, int y){
    return !(0<=x && x<=2 && 0<=y && y<=2);
}

int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};

int main(){
    double dp[20][3][3];
    int i, j, k, l, n, x, y;
    char c1, c2, c3;
    pair<int, int> st, tar, obs;

    while(scanf("%d\n",&n)){
        if(!n) break;
        scanf("%c %c %c\n",&c1, &c2, &c3);
        st = toP(c1);
        tar = toP(c2);
        obs = toP(c3);
        REP(k,20)REP(i,3)REP(j,3) dp[k][i][j] = 0.0;
        dp[0][st.fs][st.sc] = 1.0;

        REP(k,n)REP(i,3)REP(j,3){
            REP(l,4){
                x = i + dx[l];
                y = j + dy[l];
                if(isOut(x,y) || MP(x,y)==obs){
                    dp[k+1][i][j] += 0.25*dp[k][i][j];
                }
                else{
                    dp[k+1][x][y] += 0.25*dp[k][i][j];
                }
            }
        }
        printf("%.10f\n", dp[n][tar.fs][tar.sc]);
    }

    return 0;
}