n, m = map(int, input().split()) b = bin(n)[2:] b = b[::-1] x = [[1, 1], [1, 0]] def matpow(mat1, mat2): t00 = mat1[0][0] * mat2[0][0] + mat1[0][1] * mat2[1][0] t01 = mat1[0][0] * mat2[0][1] + mat1[0][1] * mat2[1][1] t10 = mat1[1][0] * mat2[0][0] + mat1[1][1] * mat2[1][0] t11 = mat1[1][0] * mat2[0][1] + mat1[1][1] * mat2[1][1] return [[t00, t01], [t10, t11]] def divmat(mat, M): return [[mat[0][0]%M, mat[0][1]%M], [mat[1][0]%M, mat[1][1]%M]] result = [[1, 0], [0, 1]] for i in range(0, len(b)): if b[i] == "1": temp = x for j in range(i): temp = matpow(temp, temp) result = matpow(result, temp) result = divmat(result, m) print(result[1][1])