def mul(X, Y, MOD): if len(X[0]) != len(Y): return False A = len(X) B = len(Y[0]) C = len(X[0]) res = [[0]*B for _ in range(A)] for i in range(A): for j in range(B): for k in range(C): res[i][j] += X[i][k] * Y[k][j] res[i][j] %= MOD return res def rui(X, n, MOD): if n == 0: A = len(X) res = [[0]*A for _ in range(A)] for i in range(A): res[i][i] = 1 return res res = rui(X, n//2, MOD) res = mul(res, res, MOD) if n % 2 == 1: res = mul(res, X, MOD) return res N, M = map(int, input().split()) if N == 1: ans = 0 elif N == 2: ans = 1 else: O = [[1, 0], [0, 0]] T = [[1, 1], [1, 0]] A = rui(T, N-2, M) A = mul(A, O, M) ans = A[0][0] print(ans)