#include <bits/stdc++.h>
using namespace std;

int dp[16][5][5];
bool grid[5][5];
const int px[] = {1, 1, 1, 2, 2, 2, 3, 3, 3};
const int py[] = {1, 2, 3, 1, 2, 3, 1, 2, 3};
const int dx[] = {-1, 0, 1, 0, 0};
const int dy[] = {0, -1, 0, 1, 0};

int main()
{
	int n;
	while (scanf("%d", &n), n){
		memset(dp, 0, sizeof(dp));
		memset(grid, true, sizeof(grid));
		
		char s, t, b;
		scanf(" %c %c %c", &s, &t, &b);
		s -= 'A'; t -= 'A'; b -= 'A';
		
		for (int i = 0; i < 9; i++){
			grid[px[i]][py[i]] = (b == i);
		}
		
		dp[0][px[s]][py[s]] = 1;
		for (int i = 0; i < n; i++){
			for (int x = 1; x <= 3; x++){
				for (int y = 1; y <= 3; y++){
					bool f = false;
					for (int dir = 0; dir < 4; dir++){
						int tx = x + dx[dir];
						int ty = y + dy[dir];
						if (grid[tx][ty]){
							//if (f) continue;
							dp[i + 1][x][y] += dp[i][x][y];
							//f = true;
						}
						else dp[i + 1][tx][ty] += dp[i][x][y];
					}
				}
			}
		}
		
		int sum = 0;
		int num = 0;
		for (int i = 0; i < 9; i++){
			sum += dp[n][px[i]][py[i]];
			num += dp[n][px[i]][py[i]] * (i == t);
		}
		
		double res = (double)num / sum;
		//printf("%d %d\n", num, sum);
		
		printf("%.8f\n", res);
	}
	
	return 0;
}