#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};
signed main() {
  cout << fixed << setprecision(8);
  for (int n; cin >> n && n;) {
    char s, t, b;
    cin >> s >> t >> b;
    double f[3][3] = {};
    double g[3][3] = {};
    pii S, T, B;
    S = pii((s - 'A') / 3, (s - 'A') % 3);
    T = pii((t - 'A') / 3, (t - 'A') % 3);
    B = pii((b - 'A') / 3, (b - 'A') % 3);
    f[S.first][S.second] = 1;
    while (n--) {
      for (int i = (0); i < (3); i++)
        for (int j = (0); j < (3); j++) g[i][j] = 0;
      swap(f, g);
      for (int a = (0); a < (3); a++)
        for (int b = (0); b < (3); b++) {
          int cnt = 0;
          f[a][b] += (g[a][b] / 4) * (4 - cnt);
        }
    }
    cout << f[T.first][T.second] << endl;
  }
  return 0;
}
