Solution by Pavel Jurca
