#include <iostream>
#include <iomanip>
using namespace std;

const int NMAX = 15;

int index(char c){
	return (int)c - 'A';
}

double dp[NMAX+1][3][3];
int dx[] = {
	1, 0, -1, 0
};
int dy[] = {
	0, 1, 0 , -1
};

int main(){
	int n;
	char s, t, b;
	while(cin >> n){
		if(n == 0) break;
		cin >> s >> t >> b;
		int sx, sy;
		int tx, ty;
		int bx, by;
		sx = index(s) % 3;
		sy = index(s) / 3;
		tx = index(t) % 3;
		ty = index(t) / 3;
		bx = index(b) % 3;
		by = index(b) / 3;
		for(int i = 0; i <= NMAX; i++){
			for(int j = 0; j < 3; j++){
				for(int k = 0; k < 3; k++){
					dp[i][j][k] = 0;
				}
			}
		}
		dp[0][ty][tx] = 1.0;
		for(int i = 1; i <= n; i++){
			for(int j = 0; j < 3; j++){
				for(int k = 0; k < 3; k++){
					if(j != by || k != bx){
						for(int l = 0; l < 4; l++)
							if(j + dy[l] >= 0 && j + dy[l] < 3 && k + dx[l] >= 0
								&& k + dx[l] < 3 && (j + dy[l] != by || k + dx[l] != bx))
							{
								dp[i][j][k] += dp[i-1][j+dy[l]][k+dx[l]] / 4;
							}else{
								dp[i][j][k] += dp[i-1][j][k] / 4;
							}
					}
				}
			}
		}
		cout << fixed << setprecision(7) << dp[n][sy][sx] << endl;;
	}
	return 0;
}