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

public class hanoi_petr implements Runnable {
    final String TASK_ID = "hanoi";
    final String IN_FILE = TASK_ID + ".in";
    final String OUT_FILE = TASK_ID + ".out";

    public static void main(String[] args) {
        Locale.setDefault(Locale.US);
        new Thread(new hanoi_petr()).start();
    }

    public void run() {
        try {
            readInput();
            solve();
            writeOutput();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(57);
        }
    }

    String[] moves;
    int n;
    long res;
//    Set<List<Integer>> all;

    private void solve() {
        int z = stupid(3);
        res = 1;
        if (z == 17) {
            for (int i = 1; i < n; ++i)
                res = res * 3 + 2;
        } else if (z == 9) {
            for (int i = 1; i < n; ++i)
                res = res * 3;
        } else if (z == 7) {
            for (int i = 1; i < n; ++i)
                res = res * 2 + 1;
        }
//        all = new HashSet<List<Integer>>();
//        rec(0);
    }

/*    private void rec(int at) {
        if (at >= 5) {
            doIt();
        }
        for (int i = at; i < 6; ++i) {
            String t = moves[i];
            moves[i] = moves[at];
            moves[at] = t;
            rec(at + 1);
            t = moves[i];
            moves[i] = moves[at];
            moves[at] = t;
        }
    }

    private void doIt() {
        List<Integer> l = new ArrayList<Integer>();
        for (n = 1; n <= 5; ++n) {
            l.add(stupid());
        }
        all.add(l);
    }*/

    private int stupid(int n) {
        List<Stack<Integer>> peg = new ArrayList<Stack<Integer>>();
        for (int i = 0; i < 3; ++i)
            peg.add(new Stack<Integer>());
        for (int i = n; i >= 1; --i)
            peg.get(0).add(i);
        int time = 0;
        int last = -1;
        while (peg.get(0).size() > 0 || (peg.get(1).size() > 0 && peg.get(2).size() > 0)) {
            for (String m : moves) {
                int i1 = m.charAt(0) - 'A';
                int i2 = m.charAt(1) - 'A';
                if (peg.get(i1).size() == 0)
                    continue;
                int cur = peg.get(i1).peek();
                if (cur == last)
                    continue;
                if (peg.get(i2).size() == 0 || peg.get(i2).peek() > cur) {
                    peg.get(i2).add(cur);
                    peg.get(i1).pop();
                    last = cur;
                    ++time;
                    break;
                }
            }
        }
        return time;
    }

    private void writeOutput() throws FileNotFoundException {
        PrintWriter writer = new PrintWriter(OUT_FILE);

        writer.println(res);

        writer.close();
    }

    private void readInput() throws IOException {
        reader = new BufferedReader(new FileReader(IN_FILE));
        tokenizer = null;

        n = nextInt();
        moves = new String[6];
        for (int i = 0; i < 6; ++i)
            moves[i] = nextToken();
    }

    StringTokenizer tokenizer;
    BufferedReader reader;

    String nextToken() throws IOException {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

}
