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