import numpy as np def main(n, m): #n, m = map(int, input().split()) x = np.array([[0, 1], [1, 1]]) y = np.array([[1, 0], [0, 1]]) s = bin(n)[:1:-1] for i in s: if i == '1': y = np.dot(y, x) y %= m x = np.dot(x, x) x %= m z = np.dot(y, [[0], [1]]) return z[0][0] n, m = map(int, input().split()) print(main(n-1, m))