def mul(A, B): if len(A[0]) != len(B): return -1 R = len(A) C = len(B[0]) K = len(B) res = [[0]*C for _ in range(R)] for i in range(R): for j in range(C): res[i][j] = sum(A[i][k]*B[k][j] for k in range(K)) res[i][j] %= M return res def power(A, x): if x == 0: N = len(A) res = [[0]*N for _ in range(N)] for i in range(N): res[i][i] = 1 return res B = power(A, x//2) res = mul(B, B) if x % 2 == 1: res = mul(res, A) return res N, M = map(int, input().split()) if N == 1: ans = 0 elif N == 2: ans = 1 else: A = [[1, 1], [1, 0]] A = power(A, N-2) ans = A[0][0] print(ans)