Compute the total time needed to visit a sequence of houses on a circular ring road.
Xenia and Ringroad
A ring road has houses numbered from $1n in clockwise order. Xenia starts at house \1$ and needs to visit a given sequence of houses in order.
Moving from one house to the next adjacent house takes one unit of time. Because the road is circular, after house comes house $1$.
Your task is to determine the total time required for Xenia to complete the entire sequence of visits.
Task
Given the number of houses and the list of houses to visit, simulate the movement along the ring road and sum the distances traveled for each step.
Input Format
- The first line contains two integers and — the number of houses on the ring road and the number of houses to visit.
- The second line contains integers — the sequence of houses Xenia must visit in order.
Output Format
- Print one integer: the total time needed to visit all houses in the given order.
Constraints
- Xenia starts at house $1$
Example 1
Input
4 3 3 2 3
Output
6
Explanation
Start at 1.
- Go to 3: 2 steps.
- Go to 2: 3 steps (3 -> 4 -> 1 -> 2).
- Go to 3: 1 step. Total = 6.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.