import numpy as np N, M = map(int, input().split()) def calc(n, m): n -= 1 a = np.array([1, 1, 1, 0]).reshape(2, 2) ans = np.array([1, 0, 0, 1]).reshape(2, 2) while n != 0: if n & 1 == 1: ans = np.dot(ans, a) ans %= m n >>= 1 a = np.dot(a, a) a %= m ans = np.dot(ans, np.array([1, 0]).reshape(2, 1)) return ans[1][0] % m print(calc(N, M))