# 行列累乗 O(H^3logN) (H := matrix Aの次元) # 内積 def product(a, b, mod=None, add=lambda x,y:x+y, mul=lambda x,y:x*y): if mod is None: res = 0 for i in map(lambda x:mul(x[0],x[1]), zip(a,b)): res = add(res,i) return res else: res = 0 for i in map(lambda x:mul(x[0],x[1])%mod, zip(a,b)): res = add(res,i)%mod return res # 行列積 def mul_of_matrix(a, b, mod=None, add=lambda x,y:x+y, mul=lambda x,y:x*y): bt = [[b[i][j] for i in range(len(b))] for j in range(len(b[0]))] return [[product(ai, bj, mod, add, mul) for bj in bt] for ai in a] # 行列累乗 def pow_of_matrix(a, n, mod=None, add=lambda x,y:x+y, mul=lambda x,y:x*y, default=1): res = [[default if i == j else 0 for j in range(len(a))] for i in range(len(a))] while n: if n&1: res = mul_of_matrix(res,a,mod,add,mul) a = mul_of_matrix(a,a,mod,add,mul) n >>= 1 return res n,m = map(int, input().split()) mat = pow_of_matrix([[1,1],[1,0]],n-1,m) print(mat[1][0])