#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

typedef long double ld;

const int EMPTY   = 0;
const int BATTERY = 1;
const int STORE   = 2;
const int WALL    = 3;

struct Point {
public:
  int x, y;
  Point() { }
  Point(int x, int y): x(x), y(y) { }
};

struct State {
public:
  Point p;
  int battery;
  State() { };
  State(Point p, int battery): p(p), battery(battery) { }
};

Point int2point(int n) {
  return Point(1 + n % 3, 1 + n / 3);
}

int field[5][5];

ld dp[16][5][5];
int main() {
  int n, s, t, b;
  while (true) {
    cin >> n;
    if (n == 0) break;
    char cs, ct, cb;
    cin >> cs >> ct >> cb;
    s = cs - 'A';
    t = ct - 'A';
    b = cb - 'A';
    
    memset(field, EMPTY, sizeof(field));
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < 5; i++) {
      field[0][i] = WALL;
      field[4][i] = WALL;
      field[i][0] = WALL;
      field[i][4] = WALL;
    }
    Point batteryPoint = int2point(t);
    field[batteryPoint.x][batteryPoint.y] = BATTERY;
    Point storePoint = int2point(b);
    field[storePoint.x][storePoint.y] = STORE;
    State nowState(int2point(s), n);

    dp[0][nowState.p.x][nowState.p.y] = 1.0f;
    int dx[] ={1,0,-1,0},dy[] ={0,1,0,-1};
    for(int i = 0; i < nowState.battery; i++){
      for(int j = 1; j <= 3; j++){
        for(int k = 1; k <= 3; k++){
          for(int l = 0;l < 4; l++){
            int nx = j + dx[l], ny = k + dy[l];
            if(field[nx][ny] == STORE || field[nx][ny] == WALL) {
              nx = j, ny = k;
            }
            dp[i+1][nx][ny] += dp[i][j][k] / 4.0;
          }
        }
      }
    }
    printf("%.15llf\n", dp[nowState.battery][batteryPoint.x][batteryPoint.y]);
  }
  return 0;
}