/*
  AOJ 1020
  Title:cleaning robot
  @kankichi573
*/
#include <stdio.h>
#include <string.h>
#include <float.h>
int n;
char s,t,b;

double matrix[9][9];
double myvector[9];
char rinsetu[24][2];
char rinsetu_org[12][2]=
  {"AB","BC","DE","EF","GH","HI",
   "AD","DG","BE","EH","CF","FI"};
void init_rinsetu()
{
  int i,j;
  for(i=0;i<12;i++)
    for(j=0;j<2;j++)
      rinsetu[i][j]=rinsetu_org[i][j];
}
void make_reverse()
{
  int i;
  for(i=0;i<12;i++)
    {
      rinsetu[i+12][0]=rinsetu[i][1];
      rinsetu[i+12][1]=rinsetu[i][0];
    }
}
void dump_rinsetu()
{
  int i;
  for(i=0;i<24;i++)
    printf("%c%c|",rinsetu[i][0],rinsetu[i][1]);
  printf("\n");
}
void delete_souko(char b)
{
  int i,j;
  for(i=0;i<24;i++)
    for(j=0;j<2;j++)
      if(rinsetu[i][j]==b)
	rinsetu[i][j]='X';
}

void make_matrix(char b)
{
  int i,j;
  double s;

  //printf("SOUKO=%c\n",b);
  init_rinsetu();
  make_reverse();
  //dump_rinsetu();
  delete_souko(b);
  //dump_rinsetu();

  for(i=0;i<24;i++)
    if(rinsetu[i][0]!='X' && rinsetu[i][1]!='X')
      matrix[rinsetu[i][0]-'A'][rinsetu[i][1]-'A']=0.25;
  for(i=0;i<9;i++)
    {
      s=0;
      for(j=0;j<9;j++)
	{
	  if(i!=j)
	    s += matrix[i][j];
	}
      matrix[i][i]=1-s;
    }
}
void matrix_vector_product()
{
  int i,j;
  double work[9];
  for(i=0;i<9;i++)
    {
      work[i]=0.0;
      for(j=0;j<9;j++)
	{
	  work[i]+=myvector[j]*matrix[i][j];
	}
    }
  memcpy(myvector,work,9*sizeof(double));
}
int init_vector(char c)
{
  int i;
  for(i=0;i<9;i++)
    myvector[i]=0.0;
  myvector[c-'A']=1.0;
}
 void dump_matrix()
 {
   int i,j;
   for(i=0;i<9;i++)
     {
       for(j=0;j<9;j++)
	 printf("%0.2lf ",matrix[i][j]);
       printf("\n");
     }
 }
void dump_vector()
{
  int i;
  for(i=0;i<9;i++)
    printf("%0.3lf|",myvector[i]);
  printf("\n");
}
main()
{
  int i;

  while(scanf("%d ",&n) && n)
    {
      scanf("%c %c %c",&s,&t,&b);

      //printf("STB=%c %c %c\n",s,t,b);


      make_matrix(b);
      init_vector(s);
      //dump_matrix();
      //printf("--------------------\n");
      //dump_vector();
      for(i=0;i<n;i++)
	{
	  matrix_vector_product();
	  //dump_vector();
	}
    }
  printf("%lf\n",myvector[t-'A']);

  return(0);
}