#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;
		int cr = -1, cc = -1;

		rep (i, 16 ) rep (j, 9 ) dp[i][j] = 0.0;
		rep (i, 3 ) rep (j, 3 ){
			if (s == room[i][j] ){
				cr = i, cc = j;
				break;
			} // end if
		} // end rep

		dp[0][s-'A'] = 1.0;
		rep (i, n ){
			for (int j = 0; j < 9; j++ ){
				if (dp[i][j] > EPS ){
					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{
							dp[i+1][room[nr][nc]-'A'] += 1./4.*dp[i][C-'A'];
						} // end if
					} // end rep
				} // end if
			} // end for
		} // end rep
		printf ("%.9lf\n", dp[n][t-'A'] );
	} // end loop
		
	return 0;
}