First distance function