computing distances through main