#include <stdio.h>
#include <string.h>

/** Application main entry point. */
int main (
  int     argc,
  char  * argv[ ]
  )
{
  int i, x, y;

  for ( ; ; )
  {
    int map[ 2 ][ 3 ][ 3 ] = { { { 0 } } };
    int mother = 1;
    int sw = 0;
    int n;
    int sx, sy, tx, ty, bx, by;
    char s, t, b;

    scanf ( " %d", &n );
    if ( !n ) break ;

    scanf ( " %c %c %c", &s, &t, &b );
    sx = ( s - 'A' ) % 3; sy = ( s - 'A' ) / 3;
    tx = ( t - 'A' ) % 3; ty = ( t - 'A' ) / 3;
    bx = ( b - 'A' ) % 3; by = ( b - 'A' ) / 3;

    map[ sw ][ sx ][ sy ] = 1;
    for ( i = 0; i < n; ++i )
    {
      mother *= 4;
      sw = !sw;

      memset ( map[ sw ], 0, sizeof ( map[ sw ] ) );

      for ( y = 0; y < 3; ++y )
      for ( x = 0; x < 3; ++x )
      {
        map[ sw ][ ( x < 1 || ( y == by && x - 1 == bx ) ? x : x - 1 ) ][ y ]
          += map[ !sw ][ x ][ y ];
        map[ sw ][ ( x > 1 || ( y == by && x + 1 == bx ) ? x : x + 1 ) ][ y ]
          += map[ !sw ][ x ][ y ];
        map[ sw ][ x ][ ( y < 1 || ( x == bx && y - 1 == by ) ? y : y - 1 ) ]
          += map[ !sw ][ x ][ y ];
        map[ sw ][ x ][ ( y > 1 || ( x == bx && y + 1 == by ) ? y : y + 1 ) ]
          += map[ !sw ][ x ][ y ];
      }
    }

    printf ( "%.6lf\n", (double)map[ sw ][ tx ][ ty ] / (double)mother );
  }

  return ( 0 );
}