#include <iostream>
#include <cstdio>
#define rep(i,n) for(int i=0;i<(n);i++)
#define EPS (10e-9)

using namespace std;

double dp[16][9];
const string room[3] = { "ABC", "DEF", "GHI" };
const int dir[][2] = { {-1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };

int main()
{
	int n;
	while (cin >> n && n ){
		char s, t, b;
		cin >> s >> t >> b;

		rep (i, 16 ) rep (j, 9 ) dp[i][j] = 0.0;

		dp[0][s-'A'] = 1.0;
		rep (i, n )
			for (int j = 0; j < 9; j++ ){
				if (dp[i][j] > EPS ){
					int cr = j/3, cc = j%3;
					char C = room[cr][cc];
					rep (k, 4 ){
						int nr = cr + dir[k][0];
						int nc = cc + dir[k][1];
						if (nr < 0 || nc < 0 || nr >= 3 || nc >= 3 || room[nr][nc] == b ){
							dp[i+1][C-'A'] += 1./4.*dp[i][C-'A'];
						}else{
							char D = room[nr][nc];
							dp[i+1][D-'A'] += 1./4.*dp[i][C-'A'];
						} // end if
					} // end rep
				} // end if
			} // end for

		printf ("%.9lf\n", dp[n][t-'A'] );
	} // end loop
		
	return 0;
}