Tightly wrap MIN defenition in parens for safety.
Better way of computing distance between IDs needed for sorting