#include <stdio.h>
#include <string.h>

int main()
{
	int room[16][9];
	int n;
	int s, t, b;
	char buf[16];

	while (scanf("%d", &n), n){
		scanf(" %[^\n]", buf);
		s = buf[0] - 'A';
		t = buf[2] - 'A';
		b = buf[4] - 'A';

		memset(room, 0, sizeof(room));
		room[0][s] = 1;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < 9; j++){
				if (room[i][j]){
					if ((j % 3) + 1 <= 2 && j + 1 != b) {
						room[i + 1][j + 1] += room[i][j];
					}
					else {
						room[i + 1][j] += room[i][j];
					}
					if ((j % 3) - 1 >= 0 && j - 1 != b) {
						room[i + 1][j - 1] += room[i][j];
					}
					else {
						room[i + 1][j] += room[i][j];
					}

					if (j / 3 + 1 <= 2 && j + 3 != b){
						room[i + 1][j + 3] += room[i][j];
					}
					else {
						room[i + 1][j] += room[i][j];
					}

					if (j / 3 - 1 >= 0 && j - 3 != b){
						room[i + 1][j - 3] += room[i][j];
					}
					else {
						room[i + 1][j] += room[i][j];
					}
				}
			}
		}

		int total_pattern = 0;
		for (int i = 0; i < 9; i++){
			total_pattern += room[n][i];
		}

		printf("%.8lf\n", (double)room[n][t] / total_pattern);
	}

	return (0);
}