#include <stdio.h>
int main(void){
	int d[] = { -5, -1, 1, 5 };
	int np, bp, tp, i, j, k, n;
	char s, t, b;
	
	for(; scanf("%d %c %c %c", &n, &s, &t, &b), n; ){
		double pr[2][25] = {};
		np = ( ( s - 'A' ) / 3 + 1 ) * 5 + ( s - 'A' ) % 3 + 1;
		tp = ( ( t - 'A' ) / 3 + 1 ) * 5 + ( t - 'A' ) % 3 + 1;
		bp = ( ( b - 'A' ) / 3 + 1 ) * 5 + ( b - 'A' ) % 3 + 1;
		pr[n & 1][np] = 1.0;

		for( ; n--; ){
			for( i = 1; i < 4; i++ ){
				for( j = 1; j < 4; j++ ){
					np = i * 5 + j;
					pr[n & 1][np] = 0.0;
					for( k = 0; k < 4; k++ ){
						pr[n & 1][np] += pr[~n & 1][np + d[k]] / 4.0;
					}
				}
			}
			pr[n & 1][bp] = 0.0;
		}
		printf("%.9f\n", pr[0][tp] );
	}
	return 0;
}