# frozen_string_literal: true n, m = gets.chomp.split.map(&:to_i) def fib_mod(num, mod) i = 0 b = 1 c = 0 while i < num a = b b = c c = (a + b) % mod i += 1 end c end # ref: https://ja.wikipedia.org/% # wiki/%E3%83%95%E3%82%A3%E3%83%9C%E3%83%8A%E3%83%83%E3%83%81%E6%95%B0 # https://yukicoder.me/submissions/246039 p fib_mod(n - 1, m)