#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
int get_x(char c) {
  if (c == 'A' || c == 'D' || c == 'G') return 0;
  if (c == 'B' || c == 'E' || c == 'H') return 1;
  if (c == 'C' || c == 'F' || c == 'I') return 2;
  return -1;
}
int get_y(char c) {
  if (c == 'A' || c == 'B' || c == 'C') return 0;
  if (c == 'D' || c == 'E' || c == 'F') return 1;
  if (c == 'G' || c == 'H' || c == 'I') return 2;
  return -1;
}
int main(void) {
  int n;
  char s, t, b;
  while (1) {
    cin >> n;
    if (n == 0) break;
    cin >> s >> t >> b;
    int sx, sy, gx, gy, ox, oy;
    sx = get_x(s);
    sy = get_y(s);
    gx = get_x(t);
    gy = get_y(t);
    ox = get_x(b);
    oy = get_y(b);
    vector<vector<int> > prev(3, vector<int>(3, 0)), next;
    prev[sx][sy] = 1;
    for (int lpc = 0; lpc < n; lpc++) {
      next = vector<vector<int> >(3, vector<int>(3, 0));
      for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++) {
          if (i == oy && j == ox) continue;
          bool stop = false;
          for (int k = 0; k < 4; k++) {
            int nx = j + dx[k];
            int ny = i + dy[k];
            if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) {
              stop = true;
              continue;
            } else if (nx == ox && ny == oy) {
              stop = true;
              continue;
            }
            next[i][j] += prev[ny][nx];
          }
          if (stop) {
            next[i][j] += prev[i][j];
          }
        }
      prev = next;
    }
    int sum = 0;
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++) sum += next[i][j];
    double answer = (double)next[gy][gx] / (double)sum;
    cout.setf(ios::fixed);
    cout << answer << endl;
  }
  return 0;
}
