//Aideen's Code for P43 {aideen[at]gmail[dot]com}
#include <iostream>
using namespace std;

const int MAX_N = 30 + 3;
const int NMOVES = 6;

int n;
string move[MAX_N];

long long ans[MAX_N][3];
char des[MAX_N][3];

long long doMove(int n, char &col) {
  int bcol = col-'A';

  if (ans[n][bcol] != 0) {
    col =  des[n][bcol];
    return ans[n][bcol];
  }

  if (n == 1) {
    for (int i=0; i<NMOVES; i++)
      if (move[i][0] == col) {
	ans[n][move[i][0]-'A'] = 1;
	des[n][move[i][0]-'A'] = move[i][1];
	col = move[i][1];
	return 1;
      }
  }

  char small = col;
  long long ret = 0;
  ret += doMove(n-1, small);
  while (small != col) {
    col = 'A'+'B'+'C'-col-small;
    ret ++;
    ret += doMove(n-1, small);
  }
  des[n][bcol] = col;
  ans[n][bcol] = ret;
  return ret;
}

int main() {
  cin >> n;
  for (int i=0; i<NMOVES; i++)
    cin >> move[i];

  char col = 'A';
  cout << doMove(n, col) << endl;    
  return 0;
}

