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): for k in range(K): res[i][j] += A[i][k] * B[k][j] res[i][j] %= MOD 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, MOD = map(int, input().split()) F = [[1, 1], [1, 0]] A = [[1], [0]] F = power(F, N-2) A = mul(F, A) print(A[0][0])