Part of 2013 Federal Competition For Advanced Students, Part 2
Source: Austrian Federal Competition 2013, part 2, problem 1