def matrix_multiply(A, B, mod): rows_A = len(A) cols_A = len(A[0]) cols_B = len(B[0]) C = [[0] * cols_B for _ in range(rows_A)] for i in range(rows_A): for j in range(cols_B): for k in range(cols_A): C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod return C def matrix_power(A, n, mod): rows = len(A) result = [[1 if i == j else 0 for j in range(rows)] for i in range(rows)] while n > 0: if n % 2 == 1: result = matrix_multiply(result, A, mod) A = matrix_multiply(A, A, mod) n //= 2 return result def fibonacci(n, mod): if n <= 1: return n base_matrix = [[1, 1], [1, 0]] powered_matrix = matrix_power(base_matrix, n - 1, mod) return powered_matrix[0][0] N, MOD = map(int, input().split()) result = fibonacci(N-1, MOD) print(result)