Calculating distances mod N You have been collecting data from a Advanced Collecting Device Controller&trade; for a long time. You check the logs, and to your horror you discover that something has gone terribly wrong: the data only contains the last bits of the numbers! 

Luckily, you know the starting value and that the value never changes fast. That means you can recover the rest by just finding the distance from the start.

Challenge

You will write a program or a function to calculate the amount a value has changed, given a modulus N and a list of the intermediate values modulo N.

The change between every pair of numbers is always less than N/2, so there will only be one valid answer for each test case.

You will be given as input an integer N > 2 and a list of values, in a format of your choice. Input may be given via STDIN or command line or function arguments.

You will output a single integer, the amount the original value has changed. Output may be printed to STDOUT or returned.

Rules


Your program must work for any distance and modulus less than 2^20.
You may assume that:


N is at least 3.
The list has at least 2 values.
All the values in the list are at least 0 and less than N.
All changes in the numbers are less than N/2.

Anything else is an invalid input, and your program may do whatever it wants.
Standard loopholes, any non-standard libraries and built-in functions for this exact purpose are forbidden. 
This is code-golf, so the shortest program in bytes wins.


Example test cases

Input:

3
0 1 2 2 0 1 0 2 1 2 0 1 2 1 1


Output:

4


Explanation (with example value):

Value mod 3: 0 1 2 2 0 1 0 2 1 2 0 1 2 1 1
Value:       0 1 2 2 3 4 3 2 1 2 3 4 5 4 4


Input:

10
5 2 8 9 5


Output:

-10


Explanation (with example value):

Value mod 10:  5  2  8  9  5
Value:        15 12  8  9  5


Invalid inputs:

2
0 0 0 0 0


(too small modulus)

6
2 5 4 2


(too large change between 2 and 5)
Python 2, 53 bytes

lambda n,l:sum((b-a+n/2)%n-n/2for a,b in zip(l,l[1:]))


Super straight forward answer. I wonder if there is a shorter way.