N, MOD = map(int, input().split()) def mul(A, B): R1 = len(A) C1 = len(A[0]) C2 = len(B[0]) res = [[0]*C2 for _ in range(R1)] for i in range(R1): for j in range(C2): for k in range(C1): 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 res = power(A, x//2) res = mul(res, res) if x % 2 == 1: res = mul(res, A) return res F = [[1, 1], [1, 0]] A = [[1], [0]] F = power(F, N-2) A = mul(F, A) print(A[0][0])