#include <iostream>
#include <cstring>
#include "stdio.h"

using namespace std;

int dx[] = {-1,0,1,0};
int dy[] = {0,-1,0,1};

int main(){
	int n;
	double prob[2][3][3];
	bool junk[3][3];
	while(cin >> n, n){
		char s, t, b; cin >> s >> t >> b;
		double ans = 0.0;
		for(int i=0;i<2;i++)
			for(int j=0;j<3;j++)
				for(int k=0;k<3;k++) prob[i][j][k] = 0.0;
		prob[0][(s-'A')/3][(s-'A')%3] = 1.0;
		int gx = (t-'A')/3, gy = (t-'A')%3;
		memset(junk, false, sizeof(junk));
		junk[(b-'A')/3][(b-'A')%3] = true;
		for(int i=0;i<n;i++){
			for(int x=0;x<3;x++)
				for(int y=0;y<3;y++) prob[1-(i%2)][x][y] = 0.0;
			for(int x=0;x<3;x++){
				for(int y=0;y<3;y++){
					for(int d=0;d<4;d++){
						int nx = x+dx[d], ny = y+dy[d];
						if(nx<0||3<=nx||ny<0||3<=ny||junk[nx][ny]) {
							nx = x, ny = y;
						}
						prob[1-(i%2)][nx][ny] += prob[i%2][x][y]*0.25;
					}
				}
			}
		}
		printf("%.8lf\n", prob[n%2][gx][gy]);
	}
}