// Aizu 1020: Cleaning Robot
// 2017.9.17 bal4u@uu

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

typedef struct { int r, c, k; } Q;
Q q[500]; int top, end;
char map[3][3], mk[3][3][17];
long long f[9];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

int main()
{
	int n, s, t, b, i, r, c, k, r2, c2;
	char b1[5], b2[5], b3[5];
	long long ans;

	while (scanf("%d", &n) && n) {
		memset(f, 0, sizeof(f)), memset(map, 0, sizeof(map));
		memset(mk, 0, sizeof(mk));
		scanf("%s%s%s", b1, b2, b3), s = *b1-'A', t = *b2-'A', b = *b3-'A';
		map[b/3][b%3] = 1;
		q[0].r = s/3, q[0].c = s%3, q[0].k = n, top = 0, end = 1;
		while (top < end) {
			r = q[top].r, c = q[top].c, k = q[top++].k;
			if (k == 0) { f[r*3+c]++; continue; }
			for (i = 0; i < 4; i++) {
				r2 = r + mv[i][0], c2 = c + mv[i][1];
				if (r2 < 0 || r2 >= 3 || c2 < 0 || c2 >= 3 || map[r][c]) r2 = r, c2 = c;
				if (mk[r2][c2][k]) { f[3*r+c] += (1ULL << 2*(k-1)); continue; }
				mk[r2][c2][k] = 1;
				q[end].r = r2, q[end].c = c2, q[end++].k = k-1;
			}
		}
		for (ans = 0, i = 0; i < 9; i++) ans += f[i];
		printf("%.8lf\n", (double)f[t] / (double)ans);
	}
	return 0;
}