# 行列A, Bの積 def matrix_mul(A,B,mod = None): row = len(A) column = len(A[0]) temp = [[0]*column for _ in range(row)] if mod is None: for i in range(row): for j in range(column): for k in range(column): temp[i][j] += A[i][k] * B[k][j] return temp else: for i in range(row): for j in range(column): for k in range(column): temp[i][j] += A[i][k] * B[k][j] % mod return temp # 行列Aのn乗(行列累乗) def matrix_pow(A,n,mod = None): nbit = list(str(bin(n))[2:]) nbit = [int(i) for i in nbit] row = len(A) column = len(A[0]) B = A C = [[0]*column for _ in range(row)] for i in range(row): C[i][i] = 1 if mod is None: for i in range(-1, -len(nbit)-1, -1): if nbit[i] == 1: C = matrix_mul(C, B) B = matrix_mul(B, B) return C else: for i in range(-1, -len(nbit)-1, -1): if nbit[i] == 1: C = matrix_mul(C, B, mod) B = matrix_mul(B, B, mod) return C N, M = map(int, input().split()) A = [[1, 1], [1, 0]] print(matrix_pow(A, 9, M)[0][1])