how to crack CHAIN - Strange Food Chain spoj question

//