How to find the longest chain?