/*--------------------------------------------------------------*/
/* ACM ICPC 2007-2008                                           */
/* Northeastern European Regional Contest                       */
/* St Petersburg - Barnaul - Tashkent - Batumi, November 28, 200*/
/*--------------------------------------------------------------*/
/* Problem H. Hanoi Towers                                      */
/*--------------------------------------------------------------*/
/* Solution                                                     */
/*                                                              */
/* Author                Roman Satyukov                         */
/*--------------------------------------------------------------*/
 
import java.io.*;
import java.util.*;

public class hanoi_rs implements Runnable {
	private BufferedReader in;
	private PrintWriter out;
	
	private long[][] ans;
	private int[][] pos;
	
	private void solve() throws IOException {
		int n = Integer.parseInt(in.readLine());
		StringTokenizer st = new StringTokenizer(in.readLine());
		int[][] ops = new int[6][2];
		for (int i = 0; i < 6; i++) {
			String cur = st.nextToken();
			ops[i][0] = cur.charAt(0) - 'A';
			ops[i][1] = cur.charAt(1) - 'A';
		}
		
		ans = new long[n + 1][3];
		pos = new int[n + 1][3];
		for (int i = 0; i < 3; i++) {
			ans[1][i] = 1;
			for (int op = 0; op < 6; op++) {
				if (ops[op][0] == i) {
					pos[1][i] = ops[op][1];
					break;
				}
			}
		}
		
		for (int size = 2; size <= n; size++) {
			for (int i = 0; i < 3; i++) {
				ans[size][i] = 0;
				
				// Step 1
				ans[size][i] += ans[size - 1][i];
				int j = pos[size - 1][i];
				
				// Step 2
				ans[size][i]++;
				int k = 3 - i - j;
				
				if (pos[size - 1][j] == k) {
					ans[size][i] += ans[size - 1][j];
					pos[size][i] = k;
					continue;
				}
				
				ans[size][i] += ans[size - 1][j];
				ans[size][i]++;
				ans[size][i] += ans[size - 1][i];
				pos[size][i] = pos[size - 1][i];
			}
		}
		out.println(ans[n][0]);
	}
	
	public static void main(String[] args) {
		new Thread(new hanoi_rs()).start();
	}

	public void run() {
		String problem = getClass().getName().split("_")[0];
		try {
			in = new BufferedReader(new FileReader(new File(problem + ".in")));
			out = new PrintWriter(new File(problem + ".out"));
			solve();
			in.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}
