def mat_mul_mod(a, b, MOD): """行列の掛け算 % MOD""" I, J, K = len(a), len(b[0]), len(b) c = [[0] * J for _ in range(I)] for i in range(I) : for j in range(J) : for k in range(K) : c[i][j] += a[i][k] * b[k][j] c[i][j] %= MOD return c def mat_pow(x, n, MOD): """行列累乗 % MOD""" y = [[0] * len(x) for _ in range(len(x))] for i in range(len(x)): y[i][i] = 1 while n > 0: if n & 1: y = mat_mul_mod(x, y, MOD) x = mat_mul_mod(x, x, MOD) n >>= 1 return y N, M = map(int, input().split()) X = [[1, 1], [1, 0]] f1, f2 = 0, 1 F = [[f2], [f1]] Y = mat_pow(X, N - 2, M) Z = mat_mul_mod(Y, F, M) print(Z[0][0])