calculating distance and saving routes