Math formula derivation help?? (COOKFOOD)

Can someone explain the math with an example on how (K-2)^2 + K-1 choices are derived??

This question is tagged as simple math but I’m not fully able to wrap the math part though i understand it in bits.
COOKFOOD Editorial

[link text][1]
[1]: COOKFOOD - Editorial - editorial - CodeChef Discuss