#include <cstdio>
#include <cstring>
char a[5][5] ={{0,0,0,0,0},
	       {0,'A','B','C',0},
	       {0,'D','E','F',0},
	       {0,'G','H','I',0},
	       {0,0,0,0,0}};
char t,b;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
double dp[22][5][5];
double solve(int e,int x,int y){
  if(!e) return (a[x][y] == t)?1:0;
  if(dp[e][x][y] >= 0) return dp[e][x][y];

  double g = 0;
  int c = 0;
  for(int i = 0; i < 4; i++){
    int nx = x + dx[i];
    int ny = y + dy[i];
    c++;
    if(a[nx][ny]){
      if(a[nx][ny] == b){
	g += solve(e-1,x,y);
      }else{
	g += solve(e-1,nx,ny);
      }
    }else{
      g += solve(e-1,x,y);
    }
  }
  //printf("%f\n",g);
  return dp[e][x][y] = g / c;
}
int main(void){
  while(1){
    int n;
    scanf("%d",&n); if(!n) break;
    memset(dp,-1,sizeof(dp));
    char s;
    scanf(" %c %c %c",&s,&t,&b);
    s -= 'A';
    printf("%.10f\n",solve(n,s/3+1,s%3+1));
  }
}