calculate and print optimal distance and path