General (SPOJ - CHAIN)

Problem:

Please, someone, explain how to solve this problem, and don’t redirect me to Quora link
I couldn’t get it from there.