Finding the least cost path