Working solution. Though problem2 has no solution