let memoize fn = let cache = new System.Collections.Generic.Dictionary<_,_>() (fun x -> match cache.TryGetValue x with | true, v -> v | false, _ -> let v = fn (x) cache.Add(x,v) v) let N,M = let t = stdin.ReadLine().Split() |> Array.map int in t.[0],t.[1] let rec fib = memoize (fun n -> if n <= 1 then 0 elif n = 2 then 1 else (fib (n-1) + fib (n-2)) % M) fib N |> printfn "%i"