Here's your approach:
Read (number of houses) and (number of tasks).
Create a vector to store the house numbers you need to visit.
Initialize your current position to and total time to .
Loop through each task in the vector.
For each target house, compute the clockwise distance using the formula from the previous unit.
Add this distance to your total time.
Update your current position to the target house.
Output the total time.