#include <bits/stdc++.h>
using namespace std;
char dir[4][4]{"ABC", "DEF", "GHI"};
int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
int main() {
  int n;
  while (cin >> n, n) {
    char s, t, b;
    cin >> s >> t >> b;
    int sum = 0, cnt = 0;
    queue<pair<char, int> > que;
    que.push(pair<char, int>(s, n));
    while (!que.empty()) {
      pair<char, int> p = que.front();
      que.pop();
      if (!p.second) {
        sum++;
        if (p.first == t) cnt++;
        continue;
      }
      int x = (p.first - 'A') / 3, y = (p.first - 'A') % 3;
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3 || dir[nx][ny] == b) {
          nx = x;
          ny = y;
        }
        que.push(pair<char, int>(dir[nx][ny], p.second - 1));
      }
    }
    printf("%.7lf\n", (double)cnt / (double)sum);
  }
}
