Calculation of min. distance