Very ugly solution for http://www.spoj.com/problems/HANGOVER/