precompute distances