https://barzunov.ru/2015/01/begin9/
Решение задачи Begin9