added a safety check to ensure that distance matrix cache always has at least 2 elements