Problem written for 2013 UTCS Programming Contest