/*
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
float k_t[9][9]={{0.50,0.25,0.00,0.25,0.00,0.00,0.00,0.00,0.00}, //A
                 {0.25,0.25,0.25,0.00,0.25,0.00,0.00,0.00,0.00}, //B
                 {0.00,0.25,0.50,0.00,0.00,0.25,0.00,0.00,0.00}, //C
                 {0.25,0.00,0.00,0.25,0.25,0.00,0.25,0.00,0.00}, //D
                 {0.00,0.25,0.00,0.25,0.00,0.25,0.00,0.25,0.00}, //E
                 {0.00,0.00,0.25,0.00,0.25,0.25,0.00,0.00,0.25}, //F
                 {0.00,0.00,0.00,0.25,0.00,0.00,0.50,0.25,0.00}, //G
                 {0.00,0.00,0.00,0.00,0.25,0.00,0.25,0.25,0.25}, //H
                 {0.00,0.00,0.00,0.25,0.00,0.25,0.00,0.25,0.25} //I
};

float k_t_a[9][9];
init()
{
  n = 3;s = 'A';t = 'H';b = 'G';
}
double kakuritu(int bat,char from,char to)
{ double ret;
  char next;
  // printf("%c %c \n",from,to);
  if(bat==0)
    if(from==to)
      return(1.0);
    else
      return(0.0);

  ret=0.0;
  for(next='A';next<='I';next++)
    if( k_t_a[from-'A'][next-'A'] > 0.0)
      ret += k_t_a[from-'A'][next-'A']*kakuritu(bat-1,next,to);
  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] += 0.25;
      }
  
}

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",kakuritu(n,s,t));
    }
  return(0);
}