#include <bits/stdc++.h>
using namespace std;
int n;
char s, t, b;
double d, a;
int dy[4] = {1, 0, -1, 0};
int dx[4] = {0, 1, 0, -1};
char v[3][3] = {{'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}};
int c[3][3] = {{2, 3, 2}, {3, 4, 3}, {2, 3, 2}};
bool can(int y, int x) {
  if (0 <= y && y < 3 && 0 <= x && x < 3) return true;
  return false;
}
int main() {
  while (cin >> n && n) {
    cin >> s >> t >> b;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (v[i][j] != b) continue;
        for (int k = 0; k < 4; k++) {
          int ny = i + dy[k];
          int nx = j + dx[k];
          if (can(ny, nx)) {
            c[ny][nx]--;
          }
        }
      }
    }
    d = 0;
    a = 1;
    queue<pair<pair<int, int>, int> > que;
    bool inQ[10];
    memset(inQ, 0, sizeof(inQ));
    int res[3][3];
    memset(res, 0, sizeof(res));
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (v[i][j] == s) {
          for (int k = 0; k < 4; k++) {
            int ni = i + dy[k];
            int nj = j + dx[k];
            que.push(make_pair(make_pair(ni, nj), 0));
            res[ni][nj] = 1;
          }
          que.push(make_pair(make_pair(i, j), 0));
        }
      }
    }
    n--;
    while (que.size()) {
      pair<int, int> p = que.front().first;
      int cnt = que.front().second;
      que.pop();
      inQ[p.first * 3 + p.second] = false;
      if (cnt == n) {
        break;
      }
      a *= c[p.first][p.second];
      for (int i = 0; i < 4; i++) {
        int ny = p.first + dy[i];
        int nx = p.second + dx[i];
        if (can(ny, nx)) {
          res[ny][nx] += res[p.first][p.second];
          if (!inQ[ny * 3 + nx]) {
            inQ[ny * 3 + nx] = true;
            que.push(make_pair(make_pair(ny, nx), cnt + 1));
          }
        }
      }
    }
    double sum = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        sum += res[i][j];
        if (v[i][j] == t) {
          d = res[i][j];
        }
      }
    }
    cout << fixed;
    cout.precision(20);
    cout << d / sum << endl;
  }
  return 0;
}
