Problem: Cold winter evenings in Tomsk are very boring — nobody wants be on the streets at such a time. Residents of Tomsk while away the time sitting in warm apartments, inventing a lot of different games. One of such games is 'Colored Jenga'.

This game requires wooden blocks of three colors: red, green and blue. A tower of n levels is made from them. Each level consists of three wooden blocks. The blocks in each level can be of arbitrary colors, but they are always located close and parallel to each other. An example of such a tower is shown in the figure.

<image>

The game is played by exactly one person. Every minute a player throws a special dice which has six sides. Two sides of the dice are green, two are blue, one is red and one is black. The dice shows each side equiprobably.

If the dice shows red, green or blue, the player must take any block of this color out of the tower at this minute so that the tower doesn't fall. If this is not possible, the player waits until the end of the minute, without touching the tower. He also has to wait until the end of the minute without touching the tower if the dice shows the black side. It is not allowed to take blocks from the top level of the tower (whether it is completed or not).

Once a player got a block out, he must put it on the top of the tower so as to form a new level or finish the upper level consisting of previously placed blocks. The newly constructed levels should have all the same properties as the initial levels. If the upper level is not completed, starting the new level is prohibited.

For the tower not to fall, in each of the levels except for the top, there should be at least one block. Moreover, if at some of these levels there is exactly one block left and this block is not the middle block, the tower falls.

The game ends at the moment when there is no block in the tower that you can take out so that the tower doesn't fall.

Here is a wonderful game invented by the residents of the city of Tomsk. I wonder for how many minutes can the game last if the player acts optimally well? If a player acts optimally well, then at any moment he tries to choose the block he takes out so as to minimize the expected number of the game duration.

Your task is to write a program that determines the expected number of the desired amount of minutes.

Input

The first line of the input contains the only integer n (2 ≤ n ≤ 6) — the number of levels in the tower.

Then n lines follow, describing the levels of the tower from the bottom to the top (the first line is the top of the tower). Each level is described by three characters, the first and the third of them set the border blocks of the level and the second one is the middle block. The character that describes the block has one of the following values 'R' (a red block), 'G' (a green block) and 'B' (a blue block).

Output

In the only line of the output print the sought mathematical expectation value. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.

Examples

Input

6
RGB
GRG
BBB
GGR
BRG
BRB


Output

17.119213696601992

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
const int maxn = 18;
const int maxm = 110;
const int maxmask = (1 << maxn) - 1;
const int infi = 1e9 + 7;
const long long infl = 1e18;
const double eps = 1e-6;
const int mod = 1e9 + 7;
int n, m, i, j, k;
vector<pair<int, int> > per[30];
string sost[30] = {"",    "R",   "B",   "G",   "RRR", "RRB", "RRG", "RBB",
                   "RBG", "RGG", "BRR", "BRB", "BRG", "BBB", "BBG", "BGG",
                   "GRR", "GRB", "GRG", "GBB", "GBG", "GGG"};
long long st[30];
bool comp(char a, char b) {
  if (a == 'R') {
    return 1;
  }
  if (b == 'R') {
    return 0;
  }
  if (a == 'B') {
    return 1;
  }
  if (b == 'B') {
    return 0;
  }
  return 0;
}
void srt(string &a) {
  swap(a[0], a[1]);
  if (comp(a[2], a[1])) {
    swap(a[1], a[2]);
  }
  return;
}
vector<int> per1[50][4];
string sosth[50] = {"",    "RRR", "RRB", "RRG", "RBB", "RBG", "RGG", "BRR",
                    "BRB", "BRG", "BBB", "BBG", "BGG", "GRR", "GRB", "GRG",
                    "GBB", "GBG", "GGG", "_R_", "_B_", "_G_", "_RR", "_RB",
                    "_RG", "_BB", "_BG", "_GG", "R__", "RR_", "RB_", "RG_",
                    "B__", "BR_", "BB_", "BG_", "G__", "GR_", "GB_", "GG_"};
map<pair<int, long long>, long double> q;
void pereb(pair<int, long long> a) {
  if (q.count(a)) {
    return;
  }
  if (a.second == 0) {
    q.insert(make_pair(a, 0));
    return;
  }
  long double res[4];
  res[1] = res[2] = res[3] = infi + 1;
  for (int i = 1; i <= 21 && st[i] <= a.second; ++i) {
    if ((a.second / st[i]) % (st[i + 1] / st[i])) {
      for (int j = 0; j < per[i].size(); ++j) {
        long long to = a.second - st[i];
        if (per[i][j].first) {
          to += st[per[i][j].first];
        }
        if (a.first <= 18) {
          to += st[a.first + 3];
        }
        for (int k = 0; k < per1[a.first][per[i][j].second].size(); ++k) {
          int p1 = per1[a.first][per[i][j].second][k];
          pereb(make_pair(p1, to));
          res[per[i][j].second] =
              min(res[per[i][j].second], q.find(make_pair(p1, to))->second);
        }
      }
    }
  }
  long double b = 5.0 / 6.0;
  long double res1 = 1;
  if (res[1] >= (long double)infi) {
    b -= (long double)1.0 / (long double)6.0;
  } else {
    res1 += res[1] / (long double)6.0;
  }
  if (res[2] >= (long double)infi) {
    b -= (long double)1.0 / (long double)3.0;
  } else {
    res1 += res[2] / (long double)3.0;
  }
  if (res[3] >= (long double)infi) {
    b -= (long double)1.0 / (long double)3.0;
  } else {
    res1 += res[3] / (long double)3.0;
  }
  if (b == 0) {
    q.insert(make_pair(a, 0));
    return;
  }
  res1 /= b;
  q.insert(make_pair(a, res1));
  return;
}
int main() {
  char b[4] = {' ', 'R', 'B', 'G'};
  per[1].push_back(make_pair(0, 1));
  per[2].push_back(make_pair(0, 2));
  per[3].push_back(make_pair(0, 3));
  for (i = 4; i <= 21; ++i) {
    for (j = 1; j <= 3; ++j) {
      if (b[j] == sost[i][0]) {
        per[i].push_back(make_pair(0, j));
        break;
      }
    }
    for (j = 1; j <= 3; ++j) {
      if (b[j] == sost[i][1]) {
        for (k = 1; k <= 3; ++k) {
          if (b[k] == sost[i][2]) {
            per[i].push_back(make_pair(j, k));
            if (j != k) {
              per[i].push_back(make_pair(k, j));
            }
          }
        }
      }
    }
  }
  for (i = 1; i <= 18; ++i) {
    per1[i][1].push_back(28);
    per1[i][1].push_back(19);
    per1[i][2].push_back(20);
    per1[i][2].push_back(32);
    per1[i][3].push_back(36);
    per1[i][3].push_back(21);
  }
  for (i = 19; i <= 39; ++i) {
    for (j = 1; j <= 3; ++j) {
      if (sosth[i][0] == '_') {
        string ar = sosth[i];
        ar[0] = b[j];
        for (k = 1; k <= 39; ++k) {
          if (ar == sosth[k]) {
            per1[i][j].push_back(k);
            break;
          }
        }
      }
      if (sosth[i][1] == '_') {
        string ar = sosth[i];
        ar[1] = b[j];
        for (k = 1; k <= 39; ++k) {
          if (ar == sosth[k]) {
            per1[i][j].push_back(k);
            break;
          }
        }
      }
      if (sosth[i][2] == '_' && sosth[i][1] != '_') {
        string ar = sosth[i];
        ar[2] = b[j];
        if (comp(ar[2], ar[1])) {
          swap(ar[1], ar[2]);
        }
        for (k = 1; k <= 39; ++k) {
          if (ar == sosth[k]) {
            per1[i][j].push_back(k);
            break;
          }
        }
      }
    }
  }
  string ar;
  long long g = 729;
  st[1] = 1;
  st[2] = 9;
  st[3] = 81;
  for (i = 4; i <= 22; ++i) {
    st[i] = g;
    g *= 6LL;
  }
  int a;
  long long arz = 0;
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> ar;
    srt(ar);
    for (j = 4; j <= 21; ++j) {
      if (ar == sost[j]) {
        a = j;
        break;
      }
    }
    if (i != n - 1) {
      arz += st[a];
    }
  }
  pereb(make_pair(a - 3, arz));
  cout.precision(9);
  cout << fixed;
  cout << q.find(make_pair(a - 3, arz))->second;
  return 0;
}