#!/usr/bin/python2 # -*- coding: utf-8 -*- # † n, mod = map(int, raw_input().split()) assert n == 1 if n > 100: p = pow(2, n, mod-1) + mod - 1 else: p = 2 ** n def matmul(A, B): p, q = len(A[0]), len(B) if p != q: raise ValueError('len(A[0])={} len(B)={}'.format(p, q)) rows, cols, times = len(A), len(B[0]), len(A[0]) res = [[0] * cols for _ in xrange(rows)] for r in xrange(rows): for c in xrange(cols): res[r][c] = sum(A[r][i]*B[i][c] % mod for i in xrange(times)) % mod return res def matpow(A, n): m = len(A) res = [[int(i==j) for j in xrange(m)] for i in xrange(m)] while n > 0: if n & 1: res = matmul(res, A) A = matmul(A, A) n >>= 1 return res A = [[2, 3], [1, 2]] B = matpow(A, 2**n) C = [[1], [0]] D = matmul(B, C) res = 2 * D[0][0] res %= mod print res