#include <bits/stdc++.h>
using namespace std;
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];
struct Bunsu {
 public:
  int bunbo;
  int bunsi;
  Bunsu() {}
  Bunsu(int a, int b) : bunbo(a), bunsi(b) {}
  Bunsu operator+=(const Bunsu& b) {
    this->bunbo += b.bunbo;
    this->bunsi += b.bunsi;
    return *this;
  }
};
Bunsu memo[5][5][16];
bool memo_b[5][5][16];
Bunsu calc(State s) {
  Bunsu ret = Bunsu(0, 0);
  if (memo_b[s.p.x][s.p.y][s.battery]) {
    return memo[s.p.x][s.p.y][s.battery];
  }
  if (s.battery == 0) {
    if (field[s.p.x][s.p.y] == BATTERY) {
      ret.bunsi++;
    }
    ret.bunbo++;
    return ret;
  }
  int dx[4] = {0, -1, 1, 0};
  int dy[4] = {-1, 0, 0, 1};
  for (int i = 0; i < 4; i++) {
    Point np = Point(s.p.x + dx[i], s.p.y + dy[i]);
    if (field[np.x][np.y] == STORE) {
      ret += calc(State(s.p, s.battery - 1));
      continue;
    }
    if (field[np.x][np.y] == WALL) {
      ret += calc(State(s.p, s.battery - 1));
      continue;
    }
    ret += calc(State(np, s.battery - 1));
  }
  memo_b[s.p.x][s.p.y][s.battery] = true;
  return memo[s.p.x][s.p.y][s.battery] = ret;
}
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));
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        for (int k = 0; k < 16; k++) {
          memo[i][j][k] = Bunsu(0, 0);
          memo_b[i][j][k] = false;
        }
      }
    }
    for (int i = 0; i < 5; i++) {
      field[0][i] = WALL;
      field[4][i] = WALL;
      field[i][0] = WALL;
      field[4][i] = 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);
    Bunsu ans = calc(nowState);
    printf("%.15lf\n", (double)ans.bunsi / ans.bunbo);
  }
  return 0;
}
