#include <bits/stdc++.h>
using namespace std;
string M[3] = {"ABC", "DEF", "GHI"};
vector<vector<bool> > V;
char s, t, b;
int g;
int cnt;
char get(const int x, const int y) { return M[y][x]; }
void visit(const int x, const int y) { V[y][x] = true; }
bool is_visited(const int x, const int y) { return V[y][x]; }
void search(int &x, int &y, const char c) {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (get(i, j) == c) {
        x = i;
        y = j;
        return;
      }
    }
  }
}
void bf(const int x, const int y, const int n) {
  if (n == 0) {
    if (get(x, y) == t) g++;
    cnt++;
    return;
  }
  const int dx[4] = {0, 0, 1, -1};
  const int dy[4] = {1, -1, 0, 0};
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) {
      bf(x, y, n - 1);
      continue;
    }
    if (get(nx, ny) == b) {
      bf(x, y, n - 1);
      continue;
    }
    bf(nx, ny, n - 1);
  }
}
int main(void) {
  int n;
  while (cin >> n && n != 0) {
    cin >> s >> t >> b;
    int x, y;
    cnt = 0;
    g = 0;
    search(x, y, s);
    V = vector<vector<bool> >(3, vector<bool>(3, false));
    bf(x, y, n);
    double probability;
    if (cnt != 0) probability = (double)g / (double)cnt;
    cout.setf(ios::fixed);
    cout.precision(8);
    cout << probability << endl;
  }
  return 0;
}
