Add pairwise distance function