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]: http://discuss.codechef.com/questions/19034/cookfood-editorial

//