#include <stdio.h>

int dp[16][3 + 2][3 + 2];

int main(void)
{
	int n;
	char s[2], e[2], t[2];
	int start;
	int end;
	int barn;
	int mot;
	
	while (1){
		scanf("%d", &n);
		if (n == 0) break;
		
		scanf("%s%s%s", s, e, t);
		
		for (int i = 0; i < 5; i++){
			for (int j = 0; j < 5; j++){
				for (int k = 0; k < 16; k++){
					if (i == 0 || i == 4 || j == 0 || j == 4){
						dp[k][i][j] = -1;
					}
					else {
						dp[k][i][j] = 0;
					}
				}
			}
		}
		
		start = s[0] - 'A';
		barn = t[0] - 'A';
		dp[0][(start % 3) + 1][(start / 3) + 1] = 1;
		
		for (int i = 0; i < n + 1; i++){
			dp[i][(barn % 3) + 1][(barn / 3) + 1] = -1;
		}
		
		for (int i = 0; i < n; i++){
			for (int y = 1; y <= 3; y++){
				for (int x = 1; x <= 3; x++){
					if (dp[i][x][y] == -1) continue;
					if (dp[i][x + 1][y] == -1){
						dp[i + 1][x][y] += dp[i][x][y];
					}
					else {
						dp[i + 1][x + 1][y] += dp[i][x][y];
					}
					if (dp[i][x - 1][y] == -1){
						dp[i + 1][x][y] += dp[i][x][y];
					}
					else {
						dp[i + 1][x - 1][y] += dp[i][x][y];
					}
					if (dp[i][x][y + 1] == -1){
						dp[i + 1][x][y] += dp[i][x][y];
					}
					else {
						dp[i + 1][x][y + 1] += dp[i][x][y];
					}
					if (dp[i][x][y - 1] == -1){
						dp[i + 1][x][y] += dp[i][x][y];
					}
					else {
						dp[i + 1][x][y - 1] += dp[i][x][y];
					}
				}
			}
		}
		
		mot = 0;
		for (int y = 1; y <= 3; y++){
			for (int x = 1; x <= 3; x++){
				if (dp[n][x][y] != -1){
					mot += dp[n][x][y];
				}
			}
		}
		end = e[0] - 'A';
		printf("%lf\n", double((double)(dp[n][(end % 3) + 1][(end / 3) + 1]) / (double)mot));
	}
	
	return (0);
}