public class Euler321 {
    static class PellState {
        long x, y;

        PellState(long x, long y) {
            this.x = x;
            this.y = y;
        }
    }

    public static String solve() {
        int terms = 40;
        PellState a = new PellState(5, 2);
        PellState b = new PellState(11, 4);

        long sum = 0;
        int taken = 0;
        while (taken < terms) {
            long na = a.y - 1;
            long nb = b.y - 1;
            if (na < nb) {
                sum += na;
                long nx = 3 * a.x + 8 * a.y;
                long ny = a.x + 3 * a.y;
                a.x = nx;
                a.y = ny;
            } else {
                sum += nb;
                long nx = 3 * b.x + 8 * b.y;
                long ny = b.x + 3 * b.y;
                b.x = nx;
                b.y = ny;
            }
            taken++;
        }
        return String.valueOf(sum);
    }

    public static void main(String[] args) {
        System.out.println(solve());
    }
}
