Source: https://www.cemc.uwaterloo.ca/contests/past_contests/2014/2014CayleySolution-f.pdf