Assign each cycle node an index from to where is cycle length. Start from any cycle node and assign indices in order. Distance from cycle node to cycle node is .
This works because the cycle wraps around. The modulo handles the wraparound. Storing indices makes distance computation a single modulo operation instead of simulation. This is much faster for large cycles.