Code for 2014 MCM's first problem by Javascript