#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		vector< vector<double> > dp(n+1, vector<double>(9, 0.0));
		char s, t, b, c;
		cin >> s >> t >> b; 
		//cout << s << ' ' << t << ' ' << b << endl;
		dp[0][s-'A'] = 1.0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 9; j++) {
				if (j == 0) {
					if (b == 'A') {
					} else if (b == 'B') {
						dp[i+1][0] += dp[i][0]*0.75;
						dp[i+1][3] += dp[i][0]*0.25;
					} else if (b == 'D') {
						dp[i+1][0] += dp[i][0]*0.75;
						dp[i+1][1] += dp[i][0]*0.25;
					} else {
						dp[i+1][0] += dp[i][0]*0.5;
						dp[i+1][1] += dp[i][0]*0.25;
						dp[i+1][3] += dp[i][0]*0.25;
					}
				} else if (j == 1) {
					if (b == 'B') {
					} else if (b == 'A') {
						dp[i+1][1] += dp[i][1]*0.5;
						dp[i+1][2] += dp[i][1]*0.25;
						dp[i+1][4] += dp[i][1]*0.25;
					} else if (b == 'C') {
						dp[i+1][1] += dp[i][1]*0.5;
						dp[i+1][0] += dp[i][1]*0.25;
						dp[i+1][4] += dp[i][1]*0.25;
					} else if (b == 'E') {
						dp[i+1][1] += dp[i][1]*0.5;
						dp[i+1][0] += dp[i][1]*0.25;
						dp[i+1][2] += dp[i][1]*0.25;
					} else {
						dp[i+1][1] += dp[i][1]*0.25;
						dp[i+1][0] += dp[i][1]*0.25;
						dp[i+1][2] += dp[i][1]*0.25;
						dp[i+1][4] += dp[i][1]*0.25;
					}
				} else if (j == 2) {
					if (b == 'C') {
					} else if (b == 'B') {
						dp[i+1][2] += dp[i][2]*0.75;
						dp[i+1][5] += dp[i][2]*0.25;
					} else if (b == 'F') {
						dp[i+1][2] += dp[i][2]*0.75;
						dp[i+1][1] += dp[i][2]*0.25;
					} else {
						dp[i+1][2] += dp[i][2]*0.5;
						dp[i+1][1] += dp[i][2]*0.25;
						dp[i+1][5] += dp[i][2]*0.25;
					}
				} else if (j == 3) {
					if (b == 'D') {
					} else if (b == 'A') {
						dp[i+1][3] += dp[i][3]*0.5;
						dp[i+1][4] += dp[i][3]*0.25;
						dp[i+1][6] += dp[i][3]*0.25;
					} else if (b == 'E') {
						dp[i+1][3] += dp[i][3]*0.5;
						dp[i+1][0] += dp[i][3]*0.25;
						dp[i+1][6] += dp[i][3]*0.25;
					} else if (b == 'G') {
						dp[i+1][3] += dp[i][3]*0.5;
						dp[i+1][0] += dp[i][3]*0.25;
						dp[i+1][4] += dp[i][3]*0.25;
					} else {
						dp[i+1][3] += dp[i][3]*0.25;
						dp[i+1][0] += dp[i][3]*0.25;
						dp[i+1][4] += dp[i][3]*0.25;
						dp[i+1][6] += dp[i][3]*0.25;
					}
				} else if (j == 4) {
					if (b == 'E') {
					} else if (b == 'B') {
						dp[i+1][4] += dp[i][4]*0.25;
						dp[i+1][3] += dp[i][4]*0.25;
						dp[i+1][5] += dp[i][4]*0.25;
						dp[i+1][7] += dp[i][4]*0.25;
					} else if (b == 'D') {
						dp[i+1][4] += dp[i][4]*0.25;
						dp[i+1][1] += dp[i][4]*0.25;
						dp[i+1][5] += dp[i][4]*0.25;
						dp[i+1][7] += dp[i][4]*0.25;
					} else if (b == 'F') {
						dp[i+1][4] += dp[i][4]*0.25;
						dp[i+1][1] += dp[i][4]*0.25;
						dp[i+1][3] += dp[i][4]*0.25;
						dp[i+1][7] += dp[i][4]*0.25;
					} else if (b == 'H') {
						dp[i+1][4] += dp[i][4]*0.25;
						dp[i+1][1] += dp[i][4]*0.25;
						dp[i+1][3] += dp[i][4]*0.25;
						dp[i+1][5] += dp[i][4]*0.25;
					} else {
						dp[i+1][1] += dp[i][4]*0.25;
						dp[i+1][3] += dp[i][4]*0.25;
						dp[i+1][5] += dp[i][4]*0.25;
						dp[i+1][7] += dp[i][4]*0.25;
					}
				} else if (j == 5) {
					if (b == 'F') {
					} else if (b == 'C') {
						dp[i+1][5] += dp[i][5]*0.5;
						dp[i+1][4] += dp[i][5]*0.25;
						dp[i+1][8] += dp[i][5]*0.25;
					} else if (b == 'E') {
						dp[i+1][5] += dp[i][5]*0.5;
						dp[i+1][2] += dp[i][5]*0.25;
						dp[i+1][8] += dp[i][5]*0.25;
					} else if (b == 'I') {
						dp[i+1][5] += dp[i][5]*0.5;
						dp[i+1][2] += dp[i][5]*0.25;
						dp[i+1][4] += dp[i][5]*0.25;
					} else {
						dp[i+1][5] += dp[i][5]*0.25;
						dp[i+1][2] += dp[i][5]*0.25;
						dp[i+1][4] += dp[i][5]*0.25;
						dp[i+1][8] += dp[i][5]*0.25;
					}
				} else if (j == 6) {
					if (b == 'G') {
					} else if (b == 'D') {
						dp[i+1][6] += dp[i][6]*0.75;
						dp[i+1][7] += dp[i][6]*0.25;
					} else if (b == 'H') {
						dp[i+1][6] += dp[i][6]*0.75;
						dp[i+1][3] += dp[i][6]*0.25;
					} else {
						dp[i+1][6] += dp[i][6]*0.5;
						dp[i+1][3] += dp[i][6]*0.25;
						dp[i+1][7] += dp[i][6]*0.25;
					}
				} else if (j == 7) {
					if (b == 'H') {
					} else if (b == 'E') {
						dp[i+1][7] += dp[i][7]*0.5;
						dp[i+1][6] += dp[i][7]*0.25;
						dp[i+1][8] += dp[i][7]*0.25;
					} else if (b == 'G') {
						dp[i+1][7] += dp[i][7]*0.5;
						dp[i+1][4] += dp[i][7]*0.25;
						dp[i+1][8] += dp[i][7]*0.25;
					} else if (b == 'I') {
						dp[i+1][7] += dp[i][7]*0.5;
						dp[i+1][4] += dp[i][7]*0.25;
						dp[i+1][6] += dp[i][7]*0.25;
					} else {
						dp[i+1][7] += dp[i][7]*0.25;
						dp[i+1][4] += dp[i][7]*0.25;
						dp[i+1][6] += dp[i][7]*0.25;
						dp[i+1][8] += dp[i][7]*0.25;
					}
				} else {
					if (b == 'I') {
					} else if (b == 'F') {
						dp[i+1][8] += dp[i][8]*0.75;
						dp[i+1][7] += dp[i][8]*0.25;
					} else if (b == 'H') {
						dp[i+1][8] += dp[i][8]*0.75;
						dp[i+1][5] += dp[i][8]*0.25;
					} else {
						dp[i+1][8] += dp[i][8]*0.5;
						dp[i+1][5] += dp[i][8]*0.25;
						dp[i+1][7] += dp[i][8]*0.25;
					}
				}
			}
		}
		printf("%.10f\n", dp[n][t-'A']);
	}
	return 0;
}

Measured memory_bytes: 1240