#include <bits/stdc++.h>
using namespace std;
int sum, ans;
int map[3][3];
void move(int x, int y, int n) {
  if (n == 0) {
    sum++;
    if (map[y][x] == 1) ans++;
    return;
  }
  if (x - 1 >= 0 && map[y][x - 1] != -1)
    move(x - 1, y, n - 1);
  else
    move(x, y, n - 1);
  if (y - 1 >= 0 && map[y - 1][x] != -1)
    move(x, y - 1, n - 1);
  else
    move(x, y, n - 1);
  if (x + 1 < 3 && map[y][x + 1] != -1)
    move(x + 1, y, n - 1);
  else
    move(x, y, n - 1);
  if (y + 1 < 3 && map[y + 1][x] != -1)
    move(x, y + 1, n - 1);
  else
    move(x, y, n - 1);
}
int main() {
  int n;
  for (;;) {
    cin >> n;
    if (n == 0) break;
    char s, t, b;
    sum = 0;
    ans = 0;
    cin >> s >> t >> b;
    memset(map, 0, sizeof(map));
    int x, y, tm;
    tm = t - 'A';
    x = tm % 3;
    y = tm / 3;
    map[y][x] = 1;
    tm = b - 'A';
    x = tm % 3;
    y = tm / 3;
    map[y][x] = -1;
    tm = s - 'A';
    move(tm % 3, tm / 3, n);
    cout << (double)ans / sum << endl;
  }
}
