Образцы решений: http://www.reshebnik.ru/solutions/5/2 и http://www.reshebnik.ru/solutions/5/4