Solution by Arpad Toth
