Create a working minimum-distance algorithm