I guess this can be done using dynamic programming.
Suppose the input is n.
then the answer has to be min(1+dp[n-1], 1 + dp[max(a,b)]) where dp[i] is the answer to n =i. You will have to write the base case for some of the initial cases. This cannot be performed for prime inputs though. For when n is prime, you cannot perform dp[max(a,b)]. So, the answer will have to be 1+ dp[n-1].