#include <cstring>
#include <iomanip>
#include <iostream>
using namespace std;
int n, b1, b2, b3, dr[4] = { -3, -1, 1, 3 }; char c1, c2, c3; double dp[19][9];
int main() {
	while (cin >> n, n) {
		cin >> c1 >> c2 >> c3;
		b1 = c1 - 65, b2 = c2 - 65, b3 = c3 - 65;
		memset(dp, 0, sizeof(dp));
		dp[0][b1] = 1;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < 9; j++) {
				int r[4] = { 0 };
				for (int k = 0; k < 4; k++) {
					int u = j + dr[k];
					if (u < 0 || u > 8 || abs(j % 3 - u % 3) == 2 || u == b3) continue;
					r[k] = 1;
				}
				for (int k = 0; k < 4; k++) {
					if (r[k]) dp[i][j + dr[k]] += dp[i - 1][j] / 4;
					else dp[i][j] += dp[i - 1][j] / 4;
				}
			}
		}
		cout << fixed << setprecision(15) << dp[n][b2] << endl;
	}
}