There is no solution to that in the form stated. In order to find a solution, you would need to define a measurement that you could apply to the polyfit() output for various K, to allow you to choose which output was appropriate for your situation.
You will not be able to get a polynomial that fits those points exactly: you are going to encounter round-off error in all the calculations.
When you use a K less than length(X)-1 then polyfit() will do fitting to find the coefficients with minimum total error at the points specified. It is plausible that you might find a K less than length(X)-1 for which the total error is "good enough" for your purposes. (Keep in mind that even with K=length(X)-1 there is going to be error.) But we do not know what your error tolerance is.