#include <bits/stdc++.h>
using namespace std;
int n, dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};
char s, b, j;
map<pair<int, char>, double> M;
char code(int y, int x) { return 'A' + y * 3 + x; }
pair<int, int> dec(char c) {
  return pair<int, int>((c - 'A') / 3, (c - 'A') % 3);
}
double f(int n, char c) {
  if (n == 0) return c == b;
  if (M.find(pair<int, int>(n, c)) != M.end()) return M[pair<int, int>(n, c)];
  double& res = M[pair<int, int>(n, c)] = 0;
  pair<int, int> p = dec(c);
  int y = p.first, x = p.second;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (ny < 0 || 2 < ny || nx < 0 || 2 < nx || code(ny, nx) == j)
      res += f(n - 1, c) / 4;
    else
      res += f(n - 1, code(ny, nx)) / 4;
  }
  return res;
}
int main() {
  while (cin >> n && n) {
    cin >> s >> b >> j;
    cout << f(n, s) << endl;
  }
}
