#include <iostream>
#include <string.h>


using namespace std;

int sum,ans;
int map[3][3];
int dp[3][3][15];
int move(int x,int y,int n)
{
  int a = 0;
  //  cout<<x<<" "<<y<<" "<<n<<" "<<endl;
  if(dp[x][y][n]!=-1)
    return dp[x][y][n];
  if(n==0)
    {
      if(map[y][x]==1)
	return 1;
      else return 0;
    }


  if(x-1>=0 && map[y][x-1]!=-1)
     a+= move(x-1,y,n-1);
  else
    a+=  move(x,y,n-1);

  if(y-1>=0 && map[y-1][x]!=-1)
     a+= move(x,y-1,n-1);
  else
    a+=  move(x,y,n-1);

  if(x+1<3 && map[y][x+1]!=-1)
    a+=  move(x+1,y,n-1);
  else
    a+=  move(x,y,n-1);

  if(y+1<3 && map[y+1][x]!=-1)
    a+=  move(x,y+1,n-1);
  else
    a+=  move(x,y,n-1); 

  dp[x][y][n]=a;

  return a;
}

int main()
{
  int n;

  for(;;)
    {
      cin>>n;
      if(n==0)
	break;
      char s,t,b;
      sum=0;
      ans=0;
      cin>>s>>t>>b;
      memset(map,0,sizeof(map));
      memset(dp,-1,sizeof(dp));
      int x,y,tm;
      tm=t-'A';
      x=tm%3;
      y=tm/3;
      map[y][x]=1;
      tm=b-'A';
      x=tm%3;
      y=tm/3;
      map[y][x]=-1;

      tm=s-'A';
      ans = move(tm%3,tm/3,n);
      sum=4;
      for(int g=1;g<n;g++)
	sum *=4;
      cout<<(double)ans/sum<<endl;  
    }
}