Write distances to files