Modified distanceBFS.