n, m = gets.split.map(&:to_i) x =m -1 y = 0 for i in 1 .. n x, y = [y, x + y] y %= m end puts x