#include <iostream>
#include <stdio.h>
using namespace std;
int date[9][5]={
	{2,1,3,0,0},
	{3,0,2,4,1},
	{2,1,5,2,2},
	{3,0,4,6,3},
	{4,1,3,5,7},
	{3,2,4,8,5},
	{2,3,7,6,6},
	{3,4,6,8,7},
	{2,5,7,8,8}
};
int main(){
	int n;
	while(cin>>n,n){
		long double dp[16][9]={0.0};
		int s,t,b;
		char S,T,B;
		cin>>S>>T>>B;
		s=S-'A';
		t=T-'A';
		b=B-'A';
		dp[0][s]=1.0;
		
		
		for(int i=0;i<15;i++){
			for(int j=0;j<9;j++){
				for(int k=1;k<=4;k++){
					if(date[j][k]!=b){
						dp[i+1][date[j][k]]+=dp[i][j]*0.25;
					}
					else{
						dp[i+1][j]+=dp[i][j]*0.25;
					}
				}
			}
		}
		//cout<<dp[n][t]<<endl;
		printf("%.8Lf\n",dp[n][t]);
	}
	return 0;
}
