import sys sys.setrecursionlimit(10**8) def mat_dot_mod(mat1, mat2, mod): if len(set(map(len, mat1))) != 1 or len(set(map(len, mat2))) != 1: raise ValueError("wrong matrix") if len(mat1[0]) != len(mat2): raise ValueError("matrix size dose not match") return [[sum(v1 * v2 for v1, v2 in zip(r1, r2)) % mod for r2 in zip(*mat2)] for r1 in mat1] def mat_pow_mod(mat,p,mod): c = mat len_mat = len(mat) ans = [[1 if i == j else 0 for j in range(len(mat))]for i in range(len(mat))] for i in range(len(bin(p))-2): if 1 << i & p: ans = mat_dot_mod(ans,c,mod) c = mat_dot_mod(c,c,mod) return ans N, M = map(int, input().split()) mat = [[1, 1], [1, 0]] mt = mat_pow_mod(mat,N-2, M) print(mt[0][0]*1 + mt[0][1]*0)