#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int n;
int field[3][3];
int goal_i,di[4] = {1,-1,0,0};
int goal_j,dj[4] = {0,0,1,-1};
double ans;

typedef struct{
	int i,j,n;
}Info;

int getI(char x){
	return (x - 'A') / 3;
}

int getJ(char x){
	return (x - 'A') % 3;
}

void solve(queue<Info> q,int now,int before[3][3]){
	int sum[3][3];

	memset(sum,0,sizeof(sum));

	while(1){
		Info tmp = q.front();
		if(tmp.n >= now) break;
		q.pop();

		for(int k=0;k<4;k++){
			Info next;
			int ni = tmp.i + di[k];
			int nj = tmp.j + dj[k];

			if(ni<0 || ni>=3 || nj<0 || nj>=3 || field[ni][nj]==2){
				next.i = tmp.i;
				next.j = tmp.j;
			}
			else{
				next.i = ni;
				next.j = nj;
			}
			next.n = tmp.n + 1;

			if(sum[next.i][next.j] == 0){
				q.push(next);
			}
			sum[next.i][next.j] += before[tmp.i][tmp.j];
		}
	}

	if(now < n){
		solve(q,now+1,sum);
	}
	else{
		ans = sum[goal_i][goal_j] * pow(0.25,n);
	}
}

int main(void){
	char s,t,b;
	int sum[3][3];

	while(1){
		cin>>n;
		if(!n) break;

		cin>>s>>t>>b;

		memset(field,0,sizeof(field));
		memset(sum,0,sizeof(sum));

		goal_i = getI(t);
		goal_j = getJ(t);
		field[goal_i][goal_j] = 1;
		field[getI(b)][getJ(b)] = 2;
		sum[getI(s)][getJ(s)] = 1;
		ans = 0;
		Info tmp = {getI(s),getJ(s),0};
		queue<Info> q;
		q.push(tmp);

		solve(q,1,sum);
		printf("%.8lf\n",ans);
	}

  return 0;
}