spoj#O2JAM. Jinyuetuan Puzzle

Jinyuetuan Puzzle

Please click here to download a PDF version of the contest problems. The problem is problem J in the PDF.