#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 main() {
  int n, s, t, b;
  while (true) {
    cin >> n;
    if (n == 0) break;
    char cs, ct, cb;
    cin >> cs >> ct >> cb;
    int count = 0, count2 = 0;
    s = cs - 'A';
    t = ct - 'A';
    b = cb - 'A';
    int field[5][5];
    memset(field, EMPTY, sizeof(field));
    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);
    queue<State> q;
    q.push(nowState);
    while (!q.empty()) {
      State s = q.front();
      q.pop();
      if (s.battery == 0) {
        if (field[s.p.x][s.p.y] == BATTERY) {
          count2++;
        }
        count++;
        continue;
      }
      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) {
          q.push(State(s.p, s.battery - 1));
          continue;
        }
        if (field[np.x][np.y] == WALL) {
          q.push(State(s.p, s.battery - 1));
          continue;
        }
        q.push(State(np, s.battery - 1));
      }
    }
    printf("%.15lf\n", (double)count2 / count);
  }
  return 0;
}
