#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

int main(){
  int n;
  while(cin >> n, n){
    vector< vector<double> > P(3, vector<double>(3,0));
    char s, t, b;
    cin >> s >> t >> b;
    int S = s - 'A', T = t - 'A', B = b - 'A';
    P[S/3][S%3] = 1;
    const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
    for(int i = 0; i < n; ++i){
      vector< vector<double> > P_(3, vector<double>(3,0));
      for(int j = 0; j < 3; ++j){
        for(int k = 0; k < 3; ++k){
          for(int l = 0; l < 4; ++l){
            int j_ = j + dx[l], k_ = k + dy[l];
            if(j_ < 0 || j_ > 2 || k_ < 0 || k_ > 2 || j_*3 + k_ == B){
              P_[j][k] += P[j][k]*0.25;
            }else{
              P_[j_][k_] += P[j][k]*0.25;
            }
          }
        }
      }
      P = P_;
    }
    printf("%.12f\n", P[T/3][T%3]);
  }
  return 0;
}

