Finding whether Clockwise or Anti - clockwise path take minimum flip .
if ( valClockwise <= valAnticlockwise ) { if ( ! isOpposite ) document . write ( " " + valClockwise + " " ) ; else document . write ( " " + valAnticlockwise + " " ) ; } else { if ( ! isOpposite ) document . write ( " " + valAnticlockwise + " " ) ; else document . write ( " " + valClockwise + " " ) ; } }