#include <bits/stdc++.h>
using namespace std;
int s, t, b, n;
double p, p1, nn;
int d[4] = {1, 3, -1, -3};
double df[10][20];
void dfs(int s, int n) {
  if (n == 0) {
    if (s == t) {
      p += pow(0.25, nn);
    }
    return;
  } else {
    if (df[s][n] >= 0) {
      p += df[s][n];
      return;
    } else {
      for (int i = 0; i < 4; i++) {
        if (s + d[i] == b) {
          p1 = p;
          dfs(s, n - 1);
          df[s][n - 1] = p - p1;
        } else if ((s / 3 != (s + d[i]) / 3) || s + d[i] < 0) {
          if ((d[i] == 3 || d[i] == -3) && 0 <= s + d[i] && s + d[i] < 9) {
            p1 = p;
            dfs(s + d[i], n - 1);
            df[s + d[1]][n - 1] = p - p1;
          } else {
            p1 = p;
            dfs(s, n - 1);
            df[s][n - 1] = p - p1;
          }
        } else {
          p1 = p;
          dfs(s + d[i], n - 1);
          df[s + d[i]][n - 1] = p - p1;
        }
      }
    }
  }
  return;
}
int main() {
  while (1) {
    char cs, ct, cb;
    p = 0;
    cin >> n;
    nn = n;
    if (n == 0) break;
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 20; j++) {
        df[i][j] = -1;
      }
    }
    cin >> cs >> ct >> cb;
    s = (int)cs - 'A';
    t = (int)ct - 'A';
    b = (int)cb - 'A';
    dfs(s, n);
    cout << p << endl;
  }
  return 0;
}
