#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;
//参考http://topcoder.g.hatena.ne.jp/isa_rentacs/20111114/1321278656 
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};

double dp[16][9];
int dir[4] = {1,-3,-1,3};
int main()
{
  int n;
  while(cin >>n&&n){
    FILL(dp,0);
    char tmp;
    int s,t,b,x,y;
    cin >> tmp; s = (int)(tmp - 'A');
    cin >> tmp; t = (int)(tmp - 'A');
    cin >> tmp; b = (int)(tmp - 'A');

    dp[0][s] = 1;
    for(int i = 0;i < n;i++){
      for(int j = 0;j < 9;j++){
	if(dp[i][j] ==0)
	  continue;
	x = j%3;
	y = j/3;
	for(int k = 0;k < 4;k++){
	  if(0 <= x+dx[k] && x+dx[k] < 3 &&
	     0 <= y+dy[k] && y+dy[k] < 3 &&
	     j+dir[k] != b){
	    dp[i+1][j+dir[k]] += (dp[i][j]*0.25);
	  }
	  else{
	    dp[i+1][j] +=(dp[i][j]*0.25);
	  }
	}
      }
    }
    printf("%.8f\n",dp[n][t]);
  }
  return 0;
}