N, M = map(int, input().split()) def modprd(A_0, A_1): a_0, b_0 = A_0[0]; c_0, d_0 = A_0[1] a_1, b_1 = A_1[0]; c_1, d_1 = A_1[1] ret = [[0, 0], [0, 0]] ret[0][0] = (a_0 * a_1 + b_0 * c_1) % M ret[0][1] = (a_0 * b_1 + b_0 * d_1) % M ret[1][0] = (c_0 * a_1 + d_0 * c_1) % M ret[1][1] = (c_0 * b_1 + d_0 * d_1) % M return ret def modpow(A, N): if N == 0: return [[1, 0], [0, 1]] if N % 2 == 0: return modprd(modpow(A, N // 2), modpow(A, N // 2)) else: return modprd(A, modprd(modpow(A, N // 2), modpow(A, N // 2))) F = [[0, 1], [1, 1]] F = modpow(F, N - 1) print(F[0][1])