/*
AizuOnline A1020
Cleaning Robot
*/
#include <stdio.h>
#include <float.h>
int  n;
char s,t,b;
//                A B C D E F G H I
int   k_t[9][9]={{2,1,0,1,0,0,0,0,0}, //A
                 {1,1,1,0,1,0,0,0,0}, //B
                 {0,1,2,0,0,1,0,0,0}, //C
                 {1,0,0,1,1,0,1,0,0}, //D
                 {0,1,0,1,0,1,0,1,0}, //E
                 {0,0,1,0,1,1,0,0,1}, //F
                 {0,0,0,1,0,0,2,1,0}, //G
                 {0,0,0,0,1,0,1,1,1}, //H
                 {0,0,0,0,0,1,0,1,2}  //I
};

int k_t_a[9][9];
init()
{
  n = 3;s = 'A';t = 'H';b = 'G';
}
unsigned long kakuritu(int bat,char from,char to)
{ unsigned long ret,w;
  char next;
  int  prob;
  

  // printf("%c %c \n",from,to);
  if(bat==0)
    if(from==to)
      return(1L<<31);
    else
      return(0L);

  ret=0L;
  for(next='A';next<='I';next++)
    {  prob= k_t_a[from-'A'][next-'A'];
      if(prob  == 1)  //0.25
	 ret += kakuritu(bat-1,next,to) >> 2;
       if(prob  == 2)  //0.5
	 ret += kakuritu(bat-1,next,to) >> 1;
       if(prob == 3)  // 0.75
	 { w = (kakuritu(bat-1,next,to) >> 1);
	   ret += (w + (w >> 1));
	 }
    }
  return(ret);

}
mod_k_t(char b)
{
  int i,j;
  for(i=0;i<9;i++)
    for(j=0;j<9;j++)
      k_t_a[i][j]=k_t[i][j];
    
  for(i=0;i<9;i++)
    if(k_t_a[i][b-'A'] > 0.0)
      {  k_t_a[i][b-'A']=0.0;
	 k_t_a[i][i] += 1;
      }
  
}

main()
{
  //init();
  while(EOF!=scanf("%d\n",&n) && n!=0)
  { scanf("%c %c %c",&s,&t,&b);
    //printf("%d %c %c %c\n",n,s,t,b);
    mod_k_t(b);
    printf("%f\n",(float) (kakuritu(n,s,t))/65536/32768);
    }
  return(0);
}