#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int dx[]{ 1,-1,0,0 }, dy[]{ 0,0,1,-1 };
int dp[3][3][16];
int main() {
	int n;
	while (cin >> n, n) {
		char s, t, b; cin >> s >> t >> b;
		memset(dp, 0, sizeof(dp));
		dp[(s - 'A') / 3][(s - 'A') % 3][0] = 1;
		rep(k, n) {
			rep(i, 3) {
				rep(j, 3) {
					if (!dp[i][j][k])continue;
					rep(l, 4) {
						int nx = i + dx[l], ny = j + dy[l];
						if (nx < 0 || ny < 0 || nx >= 3 || ny >= 3 || 'A' + nx * 3 + ny == b)
							dp[i][j][k + 1] += dp[i][j][k];
						else dp[nx][ny][k + 1] += dp[i][j][k];
					}
				}
			}
		}
		int sum = 0;
		rep(i, 3)rep(j, 3)sum += dp[i][j][n];
		printf("%.7lf\n", (double)dp[(t - 'A') / 3][(t - 'A') % 3][n] / (double)sum);
	};
}