Daniel has a bag of candy and N cards. Each card has one positive integer Pi written on it.
While eating his candy, Daniel thought of a game. He can tie together two cards labelled a and b, and each tie forces him to eat min(amodb, bmoda) pieces of candy, where xmody is the remainder of x divided by y.
Daniel wants to tie the cards so that lifting any one card lifts all the others with it. A single card can be tied directly to any number of other cards. Daniel watches his figure, so he does not want to eat much.
Compute the smallest number of candy pieces he must eat to connect all the cards.