#include <bits/stdc++.h>
using namespace std;
char s, t, b;
long long p[3][3];
char m[3][3];
long long correct = 0.0;
int n;
bool c(int i, int j) { return ((0 <= i && i < 3 && 0 <= j && j < 3)); }
void walk(int step, int i, int j) {
  p[i][j] += pow(4, step);
  if (step == n) {
    if (i == t / 3 && j == t % 3) correct += p[i][j];
    return;
  }
  if (m[i][j] == 'b') return;
  if (c(i + 1, j))
    walk(step + 1, i + 1, j);
  else
    walk(step + 1, i, j);
  if (c(i, j - 1))
    walk(step + 1, i, j - 1);
  else
    walk(step + 1, i, j);
  if (c(i, j + 1))
    walk(step + 1, i, j + 1);
  else
    walk(step + 1, i, j);
  if (c(i - 1, j))
    walk(step + 1, i - 1, j);
  else
    walk(step + 1, i, j);
}
int main(void) {
  while (cin >> n, n) {
    cin >> s >> t >> b;
    correct = 0.0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        p[i][j] = 0;
        m[i][j] = '.';
      }
    }
    s -= 'A';
    t -= 'A';
    b -= 'A';
    m[b / 3][b % 3] = 'b';
    walk(0, s / 3, s % 3);
    if (correct == 0)
      printf("%.8lf\n", (double)correct);
    else
      printf("%.8lf\n", 1.0 / (double)correct);
  }
  return 0;
}
