Solution by Ildar Shaimordanov