#include <bits/stdc++.h>
double p(int n, char s, char t, char b) {
  if (!n)
    return s == t ? 1 : 0;
  else {
    double result = 0;
    if (s >= 'D' && s - b != 3)
      result += p(n - 1, s - 3, t, b) / 4;
    else
      result += p(n - 1, s, t, b) / 4;
    if (s <= 'F' && b - s != 3)
      result += p(n - 1, s + 3, t, b) / 4;
    else
      result += p(n - 1, s, t, b) / 4;
    if ((s - 'A') % 3 && s - b != 1)
      result += p(n - 1, s - 1, t, b) / 4;
    else
      result += p(n - 1, s, t, b) / 4;
    if ((s - 'A') % 3 != 2 && b - s != 1)
      result += p(n - 1, s + 1, t, b) / 4;
    else
      result += p(n - 1, s, t, b) / 4;
    return result;
  }
}
int main() {
  int n;
  char s, t, b;
  while (std::cin >> n, n) {
    std::cin >> s >> t >> b;
    double result = p(n, s, t, b);
    std::cout << result << std::endl;
  }
}
