Terrible code for submission to AizuOnlineJudge