Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.

Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number a_{i} and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.


-----Input-----

The first line contains two integers n and m (2 ≤ n ≤ 10^5, 1 ≤ m ≤ 10^5). The second line contains m integers a_1, a_2, ..., a_{m} (1 ≤ a_{i} ≤ n). Note that Xenia can have multiple consecutive tasks in one house.


-----Output-----

Print a single integer — the time Xenia needs to complete all tasks.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.


-----Examples-----
Input
4 3
3 2 3

Output
6

Input
4 3
2 3 3

Output
2



-----Note-----

In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.

Source: codeforces | Difficulty: EASY
Tags: Implementation


Solutions:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def main():
	(n, m) = LI()
	a = LI()
	r = 0
	for i in range(m - 1):
		if a[i] > a[i + 1]:
			r += n
	return r + a[-1] - 1
print(main())


(n, m) = map(int, input().split())
s = [int(x) for x in input().split()]
time = 0
for i in range(m):
	if i == 0:
		time = s[i] - 1
	elif s[i] > s[i - 1]:
		time += s[i] - s[i - 1]
	elif s[i] < s[i - 1]:
		time += n - s[i - 1] + s[i]
print(time)


(n, m) = input().split()
n = int(n)
m = int(m)
line2 = [int(i) for i in input().split()]
x = 0
for i in range(len(line2)):
	if i > 0:
		if line2[i] < line2[i - 1]:
			x += 1
y = x * n + line2[-1] - 1
print(y)


(n, m) = input().split()
n = int(n)
m = int(m)
r = 0
time = 0
task = [int(i) for i in input().split()]
for i in range(m - 1):
	if task[i + 1] < task[i]:
		r += 1
time = r * n + task[-1] - 1
print(time)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
c = l[0] - 1
for i in range(1, m):
	if l[i] >= l[i - 1]:
		c += l[i] - l[i - 1]
	else:
		c += n + l[i] - l[i - 1]
print(c)


nk = [int(i) for i in input().split()]
n = nk[0]
k = nk[1]
number = [int(i) for i in input().split()]
step = 0
sta = 1
for i in range(k):
	final = number.pop(0)
	if sta <= final:
		step += final - sta
		sta = final
	else:
		step += n - sta + final
		sta = final
print(step)


while 1:
	try:
		(n, m) = map(int, input().split())
		a = list(map(int, input().split()))
		tt = a[0] - 1
		for i in range(1, len(a)):
			if a[i] < a[i - 1]:
				tt += n - a[i - 1] + a[i]
			else:
				tt += a[i] - a[i - 1]
		print(tt)
	except EOFError:
		break


n = list(map(int, input().split()))
lst = list(map(int, input().split()))[:n[1]]
i = 1
c = 0
while True:
	if len(lst) == 0:
		break
	elif i < lst[0]:
		val = lst[0] - i
		c += val
		i = lst[0]
	elif i > lst[0]:
		val = n[0] - (i - lst[0])
		c += val
		i = lst[0]
	elif i == lst[0]:
		vals = lst[0]
		lst.remove(vals)
	elif i == n[0] and len(lst) != 0:
		i = 1
		c += 1
print(c)


a = input().split()
b = input().split()
start = 1
total = 0
for i in b:
	if start > int(i):
		total += int(i) + int(a[0]) - start
	else:
		total += int(i) - start
	start = int(i)
print(total)


(n, m) = [int(x) for x in input().split()]
L = [int(x) for x in input().split()]
num = L[0] - 1
for i in range(1, m):
	if L[i] == L[i - 1]:
		continue
	elif L[i] > L[i - 1]:
		num = num + L[i] - L[i - 1]
	elif L[i] < L[i - 1]:
		num = num + n - L[i - 1] + L[i]
print(num)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
(t, p) = (0, 1)
for i in range(m):
	if a[i] >= p:
		t += a[i] - p
	else:
		t += n + a[i] - p
	p = a[i]
print(t)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 1)
time = 0
for i in range(m):
	if a[i] <= a[i + 1]:
		time += a[i + 1] - a[i]
	else:
		time += n - a[i] + a[i + 1]
print(time)


(n, m) = map(int, input().split())
summ = 0
prev = 1
for el in map(int, input().split()):
	summ += el - prev if el >= prev else n - prev + el
	prev = el
print(summ)


p = input
i = int
n = i(p().split()[0])
m = [1] + list(map(i, p().split()))
print(sum(([n - a + b, b - a][b >= a] for (a, b) in zip(m, m[1:]))))


import sys
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
answer = 0
current = 0
for i in range(m):
	a[i] -= 1
	if a[i] != current:
		steps = a[i] - current
		if steps < 0:
			steps = n + steps
		current = a[i]
		answer += steps
print(answer)


(a, b) = map(int, input().split())
l = list(map(int, input().split()))
ans = l[0] - 1
for i in range(b - 1):
	if l[i + 1] - l[i] < 0:
		ans += a - l[i] + 1
		ans += l[i + 1] - 1
	else:
		ans += l[i + 1] - l[i]
print(ans)


(n, m) = (int(i) for i in input().split())
tasks = [int(i) for i in input().split()]
now = 1
time = 0
for i in tasks:
	time += (i - now + n) % n
	now = i
print(time)


def ringroad(n, m, s):
	Sum = s[0]
	for i in range(0, len(s) - 1):
		if s[i] <= s[i + 1]:
			Sum = Sum + s[i + 1] - s[i]
		else:
			Sum = Sum + n - s[i] + s[i + 1]
	return Sum - 1
(n, m) = input().split()
x = input().split()
s = []
for i in range(0, int(m)):
	s.append(int(x[i]))
print(ringroad(int(n), int(m), s))


(n, t) = map(int, input().strip().split()[:2])
l = list(map(int, input().strip().split()[:t]))
c = 1
s = 0
for x in range(len(l)):
	if l[x] - c < 0:
		s += n - abs(l[x] - c)
	else:
		s += l[x] - c
	c = l[x]
print(s)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def in_int():
	return int(input())

def in_list():
	return list(map(int, input().split()))

def in_str():
	s = input()
	return list(s[:len(s) - 1])

def in_ints():
	return map(int, input().split())
mod = 7 + 10 ** 9
(n, m) = in_ints()
a = in_list()
start = 1
ans = 0
for xx in a:
	if xx >= start:
		ans += xx - start
	else:
		ans += n - start + xx
	start = xx
print(ans)


from sys import stdin, stdout
import math
import random
import time

def sumSquare(a):
	s = 0
	for i in range(len(a)):
		s += a[i] * a[i]
	return s

def C():
	n = int(input())
	a = list(map(int, stdin.readline().split()))
	while True:
		for i in range(n):
			op = False
			for j in range(i + 1, n):
				if a[i] | a[j] > max(a[i], a[j]):
					op = True
					if a[i] > a[j]:
						temp_ai = a[i]
						temp_aj = a[j]
						a[i] = temp_ai | temp_aj
						a[j] = temp_ai & temp_aj
					else:
						temp_ai = a[i]
						temp_aj = a[j]
						a[i] = temp_ai & temp_aj
						a[j] = temp_ai | temp_aj
		if not op:
			break
	print(sumSquare(a))

def main():
	(n, m) = list(map(int, stdin.readline().split()))
	t = list(map(int, stdin.readline().split()))
	time = 0
	tasks = [1]
	tasks.extend(t)
	for i in range(1, len(tasks)):
		diff = tasks[i] - tasks[i - 1]
		if diff >= 0:
			time += diff
		else:
			time += n - tasks[i - 1] + tasks[i]
	print(time)
main()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
prev = 1
s = 0
for k in a:
	if prev == k:
		continue
	elif prev < k:
		s += k - prev
	else:
		s += n - prev + k
	prev = k
print(s)


def resolve(n, m, array):
	soma = 0
	casa_atual = 1
	for i in array:
		if i > casa_atual:
			soma += i - casa_atual
		elif i < casa_atual:
			soma += n - (casa_atual - i)
		casa_atual = i
	return soma
(n, m) = [int(i) for i in input().split()]
array = [int(i) for i in input().split()]
print(resolve(n, m, array))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
c = 0
for i in range(len(a)):
	if i == 0:
		c = c + a[0] - 1
	elif a[i] >= a[i - 1]:
		c = c + a[i] - a[i - 1]
	else:
		c = c + n - a[i - 1] + a[i]
print(c)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
(t, c) = (0, 0)
for i in range(m):
	va = a[i] - 1
	if c <= va:
		t += va - c
	else:
		t += va + n - c
	c = va
print(t)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
sum = l[0] - 1
for i in range(1, m):
	if l[i] < l[i - 1]:
		sum += n - l[i - 1]
		sum += l[i]
	elif l[i] > l[i - 1]:
		sum += l[i] - l[i - 1]
	elif l[i] == l[i - 1]:
		sum += 0
print(sum)


(n, m) = map(int, input().split())
prev = 1
total = 0
for v in map(int, input().split()):
	total += (v - prev) % n
	prev = v
print(total)


(n, m) = map(int, input().split())
t = 0
l = [int(x) for x in input().split()]
num = len(l)
for i in range(num):
	if i == 0:
		t += l[i] - 1
	elif l[i] >= l[i - 1]:
		t += l[i] - l[i - 1]
	else:
		t += n - l[i - 1] + l[i]
print(t)


(x, y) = map(int, input().split())
a = list(map(int, input().split()))
suma = 0
for i in range(len(a)):
	if i != len(a) - 1:
		if a[i] < a[i + 1]:
			suma += a[i + 1] - a[i]
		elif a[i] > a[i + 1]:
			suma += x - (a[i] - a[i + 1])
print(suma + a[0] - 1)


(n, m) = map(int, input().split())
task = list(map(int, input().split()))
tim = 0
j = 0
prev = 1
while j < m:
	if task[j] >= prev:
		tim += task[j] - prev
		prev = task[j]
	else:
		tim += n - prev
		tim += task[j]
		prev = task[j]
	j += 1
print(tim)


from sys import stdin
(n, m) = [int(c) for c in stdin.readline().split()]
d = [int(c) for c in stdin.readline().split()]
t = 1
count = 0
l = len(d)
for x in d:
	if t == x:
		continue
	elif t < x:
		count = count + x - t
	else:
		count = count + n - t + x
	t = x
print(count)


l = [int(x) for x in input().split()]
n = l[0]
m = l[1]
s = [int(x) for x in input().split()]
a = s[0] - 1
for i in range(1, m):
	if s[i] >= s[i - 1]:
		a += s[i] - s[i - 1]
	else:
		a += n - s[i - 1] + s[i]
print(a)


(n, m) = map(int, input().split())
arr = []
arr = list(map(int, input().split()))
curr = 1
ans = 0
for i in arr:
	if curr > i:
		ans += n - curr + i
	else:
		ans += i - curr
	curr = i
print(ans)


a = [int(i) for i in input().split()]
n = [int(i) for i in input().split()]
summary = 0
for i in range(a[1] - 1):
	if n[i + 1] < n[i]:
		summary += a[0]
print(summary + n[a[1] - 1] - 1)


(n, m) = map(int, input().split())
a = map(int, input().split())
ans = 0
prev = 1
for ai in a:
	if ai >= prev:
		ans += ai - prev
	else:
		ans += n + ai - prev
	prev = ai
print(ans)


def main():
	(n, m) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = 0
	a = 1
	for i in range(m):
		b = arr[i]
		if a <= b:
			ans += b - a
		else:
			ans += n - a + b
		a = b
	print(ans)
main()


(n, m) = input().split(' ')
(n, m) = (int(n), int(m))
a = [1] + [int(i) for i in input().split(' ')]
mov = 0
i = 1
while i < len(a):
	if a[i] < a[i - 1]:
		mov += n - a[i - 1] + (a[i] - 1) + 1
	else:
		mov += a[i] - a[i - 1]
	i += 1
print(mov)


nm = input()
info = nm.split(' ')
n = int(info[0])
m = int(info[1])
h = input()
home = h.split(' ')
i = 0
while i < len(home):
	home[i] = int(home[i])
	i += 1
num = home[0] - 1
i = 1
while i < m:
	if home[i] > home[i - 1]:
		num += home[i] - home[i - 1]
	elif home[i] < home[i - 1]:
		num += n - home[i - 1] + home[i]
	else:
		pass
	i += 1
print(num)


(n, m) = map(int, input().split())
lst = list(map(int, input().split()))
count = lst[0] - 1
for i in range(1, len(lst)):
	if lst[i] == lst[i - 1]:
		continue
	elif lst[i] > lst[i - 1]:
		count += lst[i] - lst[i - 1]
	else:
		count += n - lst[i - 1] + lst[i]
print(count)


class CodeforcesTask339BSolution:

	def __init__(self):
		self.result = ''
		self.n_m = []
		self.tasks = []

	def read_input(self):
		self.n_m = [int(x) for x in input().split(' ')]
		self.tasks = [int(x) for x in input().split(' ')]

	def process_task(self):
		time_needed = 0
		position = 1
		for task in self.tasks:
			if task > position:
				time_needed += task - position
				position = task
			elif task < position:
				time_needed += task + self.n_m[0] - position
				position = task
		self.result = str(time_needed)

	def get_result(self):
		return self.result
Solution = CodeforcesTask339BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


(n, m) = input().split()
h = input().split()
h = ['1'] + h
ans = 0
for i in range(int(m)):
	t = int(h[i + 1]) - int(h[i])
	ans += t
	if t < 0:
		ans += int(n)
print(ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
time = l[0] - 1
for i in range(1, m):
	time += (n - l[i - 1] + l[i]) % n
print(time)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
ans = a[0] - 1
for i in range(1, m):
	if a[i] < a[i - 1]:
		ans += n - a[i - 1] + a[i]
	elif a[i] > a[i - 1]:
		ans += a[i] - a[i - 1]
print(ans)


(n, _) = map(int, input().split())
tasks = list(map(int, input().split()))
cur_house = 1
cur_time = 0
for task in tasks:
	cur_time += (task - cur_house) % n
	cur_house = task
print(cur_time)


(m, n) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
c = 0
st = 1
for i in a:
	if i >= st:
		c += i - st
	else:
		c += m - st + i
	st = i
print(c)


(n, m) = [int(i) for i in input().split()]
numb = [0] * m
a = input().split()
for i in range(m):
	numb[i] = int(a[i])
time = numb[0] - 1
for i in range(m - 1):
	if numb[i + 1] >= numb[i]:
		time += numb[i + 1] - numb[i]
	else:
		time += n - numb[i] + numb[i + 1]
print(time)


(n, m) = [int(x) for x in input().split()]
tasks = [int(x) for x in input().split()]
s = 0
position = 1
for t in tasks:
	if t >= position:
		s += t - position
	else:
		s += t - position + n
	position = t
print(s)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
import heapq
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
input = sys.stdin.readline
(n, m) = map(int, input().split())
ar = [1] + list(map(int, input().split()))
answer = 0
for i in range(m):
	if ar[i] <= ar[i + 1]:
		answer += ar[i + 1] - ar[i]
	else:
		answer += n - (ar[i] - ar[i + 1])
print(answer)


x = list(map(int, input().split()))
a = list(map(int, input().split()))
start = 1
t = 0
for i in range(x[1]):
	if a[i] == start:
		pass
	elif a[i] > start:
		t += a[i] - start
		start = a[i]
	elif a[i] < start:
		t += x[0] - (start - a[i])
		start = a[i]
print(t)


(n, m) = map(int, input().split())
q = 0
ar = [1] + list(map(int, input().split()))
for ind in range(1, len(ar)):
	if ar[ind] > ar[ind - 1]:
		q += ar[ind] - ar[ind - 1]
	elif ar[ind] < ar[ind - 1]:
		q += n - ar[ind - 1] + ar[ind]
	else:
		q += 0
print(q)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
res = 0
cur = 1
for i in a:
	if i < cur:
		res += n + i - cur
	else:
		res += i - cur
	cur = i
print(res)


(a, b) = map(int, input().split())
lst = list(map(int, input().split()))
count = lst[0] - 1
for i in range(1, b):
	if lst[i] == lst[i - 1]:
		pass
	elif lst[i] > lst[i - 1]:
		count += lst[i] - lst[i - 1]
	else:
		count += lst[i] + a - lst[i - 1]
print(count)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
(n, m) = (a[0], a[1])
c = 1
t = 0
for i in b:
	if c > i:
		t += n + i - c
		c = i
	elif c < i:
		t += i - c
		c = i
print(t)


a = input().split()
n = int(a[0])
m = int(a[1])
start = 1
t = 0
task = [int(x) for x in input().split()]
for i in task:
	if i >= start:
		t += i - start
	else:
		t += n + i - start
	start = i
print(t)


def main():
	(n, m) = [int(i) for i in input().split()]
	arr = [int(i) for i in input().split()]
	count = 0
	last = 1
	for i in range(len(arr)):
		if arr[i] >= last:
			count += arr[i] - last
		else:
			count += n - last + arr[i]
		last = arr[i]
	print(count)
	return 0
main()


(n, m) = list(map(int, input().split()))
t = list(map(int, input().split()))
temp = 1
ans = 0
for i in t:
	if i >= temp:
		ans += i - temp
	else:
		ans += i + n - temp
	temp = i
print(ans)


(n, m) = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
position = 1
time = 0
for i in x:
	if i >= position:
		time += i - position
		position = i
	else:
		time += i + n - position
		position = i
print(time)


f = lambda : map(int, input().split())
(n, m) = f()
lst = list(f())
cnt = lst[0] - 1
for i in range(1, m):
	if lst[i] < lst[i - 1]:
		cnt += n - lst[i - 1] + 1 + (lst[i] - 1)
	elif lst[i] > lst[i - 1]:
		cnt += lst[i] - lst[i - 1]
print(cnt)


(x, y) = map(int, input().split())
lst = list(map(int, input().split()))
p = 1
c = 0
for i in lst:
	if p < i:
		c += i - p
		p = i
	elif p > i:
		c += i
		c += x - p
		p = i
print(c)


from sys import stdin, stdout
A = [int(a) for a in stdin.read().split()]
(a, b) = (A[0], A[1])
A = A[2:]
c = A[0]
f = c - 1
d = 1
while d < b:
	e = A[d]
	if e >= c:
		f = f + e - c
		c = e
	else:
		f = f + a - c + e
		c = e
	d = d + 1
stdout.write(str(f))


def solve():
	cost = 0
	house = 1
	for i in range(len(tasks)):
		if tasks[i] >= house:
			cost += tasks[i] - house
			house = tasks[i]
		else:
			cost += n - house + tasks[i]
			house = tasks[i]
	return cost
(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
print(solve())


count = 0
(n, m) = list(map(int, input().split()))
w = list(map(int, input().split()))
count += w[0] - 1
for i in range(m - 1):
	if w[i] < w[i + 1]:
		count += w[i + 1] - w[i]
	if w[i] > w[i + 1]:
		count += n - w[i] + w[i + 1]
print(count)


li = [int(i) for i in input().split()]
li1 = [int(i) for i in input().split()]
c = 0
for i in range(li[1]):
	if i == 0:
		c += li1[i]
	elif li1[i] < li1[i - 1]:
		c += li[0] - li1[i - 1]
		c += li1[i]
	else:
		c += li1[i] - li1[i - 1]
print(c - 1)


l = [int(x) for x in input().split()]
n = l[0]
m = l[1]
l = [int(x) for x in input().split()]
x = 1
ans = 0
for i in l:
	if i >= x:
		ans += i - x
	else:
		ans += n - (x - i)
	x = i
print(ans)


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
(n, m) = rinput()
a = list(map(int, input().split()))
sum = a[0] - 1
for i in range(m - 1):
	if a[i] > a[i + 1]:
		s1 = a[i + 1] + n - a[i]
		sum += s1
	else:
		s1 = a[i + 1] - a[i]
		sum += s1
print(sum)


(n, m) = map(int, input().split())
time = 0
place = 1
for house in map(int, input().split()):
	if house >= place:
		time += house - place
	else:
		time += n - place + house
	place = house
print(time)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
s = 0
for i in range(0, m - 1):
	if l[i] > l[i + 1]:
		s = s + 1
s = s * n + l[-1] - 1
print(s)


data = input().split(' ')
n = int(data[0])
m = int(data[1])
current = 1
time = 0
tasks = input().split(' ')
tasks = [int(x) for x in tasks]
for task in tasks:
	if task > current:
		time += task - current
		current = task
	elif task < current:
		time += task - 1 + (n + 1 - current)
		current = task
print(time)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
for i in range(k):
	if i == 0:
		ans = ans + l[i] - 1
	elif l[i] > l[i - 1]:
		ans = ans + (l[i] - l[i - 1])
	elif l[i] < l[i - 1]:
		ans = ans + (n - l[i - 1]) + l[i]
print(ans)


(n, m) = map(int, input().split())
tasks = list((int(x) for x in input().split()))
t = tasks[0] - 1
for i in range(m - 1):
	if tasks[i] > tasks[i + 1]:
		t += tasks[i + 1] + n - tasks[i]
	else:
		t += tasks[i + 1] - tasks[i]
print(t)


(n, m) = [int(a) for a in input().split()]
tasks = [1] + [int(a) for a in input().split()]
count = 0
for x in range(1, m + 1):
	if tasks[x] >= tasks[x - 1]:
		count += tasks[x] - tasks[x - 1]
	else:
		count += n - tasks[x - 1] + tasks[x]
print(count)


def modified_difference(x, y, n):
	if y >= x:
		return y - x
	else:
		return n - x + y
(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
time = 0
time += modified_difference(1, l1[0], n)
for i in range(1, m, 1):
	time += modified_difference(l1[i - 1], l1[i], n)
print(time)


from sys import stdin

def take():
	(n, m) = map(int, stdin.readline().split())
	li = map(int, stdin.readline().split())
	count = 0
	curr = 1
	for val in li:
		if val >= curr:
			count += val - curr
		else:
			count += n - curr + val
		curr = val
	print(count)
take()


__author__ = 'Esfandiar'
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
res = 0
ind = 1
for i in range(m):
	if ind > a[i]:
		res += n - ind + a[i]
	else:
		res += a[i] - ind
	ind = a[i]
print(res)


(n, m) = [int(x) for x in input().split()]
task = [int(x) for x in input().split()]
current = task[0]
times = task[0] - 1
for i in range(1, m):
	if task[i] >= task[i - 1]:
		times += task[i] - task[i - 1]
	else:
		times += task[i] + n - task[i - 1]
print(times)


(n, m) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
p = 1
time = 0
for i in range(0, m):
	if a[i] >= p:
		time += a[i] - p
	elif a[i] <= p:
		time += n - p + a[i]
	p = a[i]
print(time)


a = input()
list_a = list(map(int, a.split()))
n = list_a[0]
m = list_a[1]
b = input()
list_b = list(map(int, b.split()))
i = 0
time = 0
y = 1
while i < len(list_b):
	x = list_b[i]
	if y > x:
		time += n - y + 1
		y = 1
		continue
	else:
		time += x - y
		y = x
		i += 1
print(time)


nm = list(map(int, input().split(' ')))
n = nm[0]
m = nm[1]
hno = list(map(int, input().split()))
ch = 1
t = 0
for i in range(m):
	dh = hno[i]
	if dh < ch:
		t += n - ch + 1 + (dh - 1)
	else:
		t += dh - ch
	ch = hno[i]
print(t)


(n, m) = map(int, input().split(' '))
ai = [s for s in map(int, input().split(' '))]
x = 1
sum_time = 0
for i in ai:
	if x > i:
		sum_time += n - (x - i)
		x = i
	else:
		sum_time += i - x
		x = i
print(sum_time)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = 0
for i in range(1, m):
	if a[i] < a[i - 1]:
		b += n - a[i - 1] + a[i]
	else:
		b += a[i] - a[i - 1]
print(a[0] - 1 + b)


(n, m) = map(int, input().split())
list_of_tasks = list(map(int, input().split()))
counter = 0
start = 1
for i in list_of_tasks:
	if i >= start:
		counter += i - start
	else:
		counter += n - start + i
	start = i
print(counter)


inp = input()
inp = inp.split()
houses = int(inp[0])
n = int(inp[1])
tasks = input()
tasks = tasks.split()
tasks2 = []
for i in tasks:
	tasks2.append(int(i))
time = tasks2[0] - 1
for i in range(1, len(tasks2)):
	if tasks2[i] == tasks2[i - 1]:
		continue
	elif tasks2[i] - tasks2[i - 1] < 0:
		time += houses + tasks2[i] - tasks2[i - 1]
	else:
		time += tasks2[i] - tasks2[i - 1]
print(time)


(n, m) = list(map(int, input().strip().split(' ')))
av = list(map(int, input().strip().split(' ')))
ans = av[0] - 1
curr = av[0]
for i in range(m - 1):
	if av[i] > av[i + 1]:
		ans += n - av[i]
		ans += av[i + 1]
	else:
		ans += av[i + 1] - av[i]
print(ans)


s = input().split()
n = int(s[0])
m = int(s[1])
p = 0
o = [int(i) for i in input().split()]
o.insert(0, 1)
for i in range(m):
	if o[i] <= o[i + 1]:
		p = p + o[i + 1] - o[i]
	else:
		p = n - o[i] + o[i + 1] + p
print(p)


(n, m) = input().split()
line = input()
piece = line.split()
t1 = 1
s = 0
for i in range(int(m)):
	t2 = int(piece[i])
	k = t2 - t1
	if k < 0:
		k = k + int(n)
	else:
		k = k
	s = s + k
	t1 = t2
print(s)


(n, m) = map(int, input().split())
(*t,) = map(int, input().split())
im = 0
for i in range(m):
	if i == 0:
		im = t[i]
	elif t[i] >= t[i - 1]:
		im += t[i] - t[i - 1]
	else:
		im += n + t[i] - t[i - 1]
print(im - 1)


(a, n) = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
q = 0
w = 1
for i in x:
	if i == w:
		continue
	if i < w:
		q = q + i + a - w
	else:
		q = q + i - w
	w = i
print(q)


(n, m) = map(int, input().split())
arr = [int(i) for i in input().split()]
p = 1
ans = 0
for i in range(m):
	if arr[i] < p:
		ans += n - (p - arr[i])
		p = arr[i]
	elif arr[i] > p:
		ans += arr[i] - p
		p = arr[i]
print(ans)


(m, n) = map(int, input().split())
A = [1] + [int(i) for i in input().split()]
t = 0
for i in range(n):
	t += (A[i + 1] - A[i]) % m
print(t)


(n, m) = map(int, input().split(' '))
lst = list(map(int, input().split(' ')))
k = 1
ans = 0
for i in range(len(lst)):
	if lst[i] > k:
		ans = ans + abs(k - lst[i])
		k = lst[i]
	elif lst[i] < k:
		ans = ans + (n - abs(k - lst[i]))
		k = lst[i]
print(ans)


(n, m) = [int(x) for x in input().split()]
x = [int(x) for x in input().split()]
t = x[0] - 1
for i in range(1, m):
	if x[i] >= x[i - 1]:
		t = t + x[i] - x[i - 1]
	else:
		t = t + n + x[i] - x[i - 1]
print(t)


(n, m) = map(int, input().split())
M = [int(i) for i in input().split()]
(ring, result, addition) = (0, 0, M[-1] - 1)
for i in range(len(M) - 1):
	if M[i] > M[i + 1]:
		ring += 1
result += ring * n + addition
print(result)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
j = 1
sum = 0
for i in range(m):
	if j <= a[i]:
		sum += a[i] - j
		j = a[i]
	else:
		sum += n - j + a[i]
		j = a[i]
print(sum)


(n, m) = map(int, input().split())
(a, p, t) = ([1] + [int(x) for x in input().split()], 1, 0)
for (i, j) in zip(a, a[1:]):
	if i != j:
		t += j - p if j - p >= 0 else n - p + j
		p = j
print(t)


(h, n) = map(int, input().split())
a = list(map(int, input().split()))
x = 0
a += [0]
for i in range(n):
	if a[i + 1] < a[i]:
		if i < n - 1:
			x += h
		else:
			x += a[i]
print(x - 1)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
total = 0
cur = 1
for i in range(len(a)):
	if cur < a[i]:
		total += a[i] - cur
		cur += a[i] - cur
	elif cur > a[i]:
		total += n - cur + a[i]
		cur = a[i]
print(total)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
ret = 0
cur = 1
for i in a:
	ret += i - cur
	if i < cur:
		ret += n
	cur = i
print(ret)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = 0
for i in range(0, m - 1):
	q = a[i] - a[i + 1]
	if q > 0:
		s = s + n - q
	else:
		s = s - q
s = s + a[0] - 1
print(s)


def count_delta(a, b, max_num):
	if b < a:
		return max_num - a + b
	return b - a
min_time = 0
(n, m) = map(int, input().split(' '))
tasks = list(map(int, input().split(' ')))
for i in range(0, m - 1):
	min_time += count_delta(tasks[i], tasks[i + 1], n)
print(min_time + (tasks[0] - 1))


def solve(n, m, b):
	b = [1] + b
	s = 0
	for i in range(m):
		s += (b[i + 1] - b[i] + n) % n
	return s

def test():
	assert solve(4, 3, [3, 2, 3]) == 6
	assert solve(4, 3, [2, 3, 3]) == 2
	print('test passes')
tmp = input()
(n, m) = map(int, tmp.split())
tmp = input()
b = list(map(int, tmp.split()))
print(solve(n, m, b))


(n, m) = map(int, input().split())
y = list(map(int, input().split()))
c = y[0] - 1
for i in range(m - 1):
	if y[i + 1] > y[i]:
		c += y[i + 1] - y[i]
	if y[i + 1] < y[i]:
		c += n - y[i] + y[i + 1]
print(c)


(n, m) = input().split()
(n, m) = [int(n), int(m)]
a = [int(x) for x in input().split()]
total = 0
previous = 1
for task in range(m):
	if a[task] >= previous:
		total += a[task] - previous
		previous = a[task]
	else:
		total += n - (previous - a[task])
		previous = a[task]
print(total)


(a, b) = map(int, input().split())
in1 = list(map(int, input().split()))
(prev, count) = (1, 0)
for i in range(0, b):
	curr = in1[i]
	if curr >= prev:
		count += curr - prev
	else:
		count += a - prev + curr
	prev = curr
print(count)


u = 0
(n, m) = map(int, input().split())
mas = list(map(int, input().split()))
for i in range(0, m - 1):
	if mas[i] > mas[i + 1]:
		u += 1
print(mas[m - 1] + u * n - 1)


def d(x, y, n):
	return (x + n - y, x - y)[x >= y]
n = int(input().split()[0])
a = list(map(int, input().split()))
r = sum((d(x, y, n) for (x, y) in zip(a, [1] + a)))
print(r)


(n, m) = (int(x) for x in input().split())
a = list((int(x) for x in input().split()))
ans = 0
prev = 0
for i in a:
	if i >= prev:
		ans += i - prev
	else:
		ans += n + i - prev
	prev = i
print(ans - 1)


f = lambda : map(int, input().split())
(n, m) = f()
ms = list(f())
res = ms[0] - 1
for i in range(1, m):
	if ms[i - 1] < ms[i]:
		res += ms[i] - ms[i - 1]
	elif ms[i - 1] > ms[i]:
		res += n - ms[i - 1] + ms[i]
print(res)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
a = 1
ans = 0
for i in range(len(arr)):
	if a <= arr[i]:
		ans += arr[i] - a
	else:
		ans += n - a + arr[i]
	a = arr[i]
print(ans)


(a, b) = map(int, input().split())
tarefas = [int(x) for x in input().split()]
(casa_atual, resp) = (1, 0)
for tarefa in tarefas:
	if tarefa > casa_atual:
		resp = resp + (tarefa - casa_atual)
		casa_atual = tarefa
	elif tarefa < casa_atual:
		resp = resp + (a - casa_atual + tarefa)
		casa_atual = tarefa
print(resp)


str = input().split()
(n, m) = (int(str[0]), int(str[1]))
affairs = [int(x) for x in input().split()]
now = 1
time = 0
for affair in affairs:
	if affair >= now:
		time += affair - now
	else:
		time += n - now + affair
	now = affair
print(time)


(n, m) = input().split()
m = int(m)
n = int(n)
pos = 0
count = 0
x = [int(x) for x in input().split()]
for i in x:
	if i > pos:
		count = count + (i - pos)
		pos = i
	elif i < pos:
		count = count + i + (n - pos)
		pos = i
print(count - 1)


(house, jobCount) = input().split()
job = input().split()
job = job[:int(jobCount)]
time = 0
pos = 1
for x in job:
	x = int(x)
	if x >= pos:
		time += x - pos
	else:
		time += int(house) - (pos - x)
	pos = x
print(time)


(n, m) = map(int, input().rstrip().split(' '))
arr = list(map(int, input().rstrip().split(' ')))
cur = 1
cost = 0
for i in range(m):
	if arr[i] > cur:
		cost += arr[i] - cur
	elif cur > arr[i]:
		cost += n - cur + arr[i]
	cur = arr[i]
print(cost)


(n, v) = map(int, input().split())
k = list(map(int, input().split()))
c = k[0] - 1
for i in range(len(k) - 1):
	if k[i + 1] == k[i]:
		c += 0
	elif k[i + 1] < k[i] and k[i + 1] != 1:
		c += n - k[i] + 1 + k[i + 1] - 1
	elif k[i + 1] < k[i] and k[i + 1] == 1:
		c += n - k[i] + 1
	elif k[i + 1] > k[i]:
		c += k[i + 1] - k[i]
print(c)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
fre = 1
circle = 0
for cur in a:
	if cur < fre:
		circle += 1
	fre = cur
print(circle * n + a[-1] - 1)


class Code:

	def __init__(self):
		(self.n, self.m) = list(map(int, input().split()))
		self.arr = list(map(int, input().split()))

	def process(self):
		(start, idx, jump) = (1, 0, 0)
		while idx < self.m:
			if self.arr[idx] >= start:
				jump += self.arr[idx] - start
			else:
				jump += self.n - start + self.arr[idx]
			start = self.arr[idx]
			idx += 1
		print(jump)
code = Code()
code.process()


def main():
	(n, m) = [int(i) for i in input().split(' ')]
	task_houses = [int(i) for i in input().split(' ')]
	ttime = 0
	start_house = 1
	ttime = task_houses[0] - start_house
	for i in range(1, m):
		t = task_houses[i] - task_houses[i - 1]
		if t < 0:
			t += n
		ttime += t
	print(ttime)
main()


(n, t) = list(map(int, input().split()))
ringroad = list(map(int, input().split()))
time = ringroad[0] - 1
for i in range(1, len(ringroad)):
	if ringroad[i] > ringroad[i - 1]:
		time += ringroad[i] - ringroad[i - 1]
	elif ringroad[i] == ringroad[i - 1]:
		continue
	else:
		time += n - ringroad[i - 1] + ringroad[i]
print(time)


vals = [int(x) for x in input().split()]
n = vals[0]
m = vals[1]
tasks = [int(x) for x in input().split()]
currentHouse = 1
time = 0
for task in tasks:
	if currentHouse <= task:
		time += task - currentHouse
	else:
		time += n - currentHouse + task
	currentHouse = task
print(time)


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
ans = 0
curr = 1
for i in range(m):
	if a[i] >= curr:
		ans += a[i] - curr
	else:
		ans += n - curr + a[i]
	curr = a[i]
print(ans)


import sys

def main():
	(houses, todo) = [int(x) for x in sys.stdin.readline().strip().split()]
	pos = 1
	time = 0
	for x in sys.stdin.readline().strip().split():
		goto = int(x)
		if goto >= pos:
			time += goto - pos
		else:
			time += houses - pos + goto
		pos = goto
	print(time)
main()


def main():
	(n, m) = map(int, input().split(' '))
	list1 = list(map(int, input().split(' ')))
	c = 1
	sum1 = []
	for i in list1:
		if i - c >= 0:
			sum1.append(i - c)
			c = i
		elif i - c < 0:
			sum1.append(n + i - c)
			c = i
	print(sum(sum1))
main()


(n, m) = map(int, input().split())
A = input().split()
b = 1
t = 0
for i in A:
	a = int(i)
	if a >= b:
		t += a - b
	else:
		t += a - b + n
	b = a
print(t)


def tm(n, m, v):
	t = 0
	v.insert(0, 1)
	for x in range(m):
		(s, f) = (v[x], v[x + 1])
		if f < s:
			f += n
		t += f - s
	return t
(n, m) = [int(x) for x in input().split()]
v = [int(x) for x in input().split()]
print(tm(n, m, v))


(n, m) = map(int, input().split())
tasks = [int(v) for v in input().split()]
(pos, total) = (0, 0)
for cur in tasks:
	if cur < pos:
		total += n - pos + cur
	elif cur > pos:
		total += cur - pos
	pos = cur
print(total - 1)


s = k = 0
(n, m) = map(int, input().split())
trouble = [int(x) for x in input().split()]
k = trouble[m - 1]
for i in range(m - 1):
	if trouble[i] > trouble[i + 1]:
		s += 1
print(n * s + k - 1)


import math
a = input()
b = input()
arr1 = []
arr2 = []

def get_the_list(count, inn, arr):
	for i in range(0, len(inn)):
		if inn[i] == ' ':
			arr.append(int(inn[count:i]))
			count = i + 1
		elif i == len(inn) - 1:
			arr.append(int(inn[count:len(inn) + 1]))
	return arr
data1 = get_the_list(0, a, arr1)
data2 = get_the_list(0, b, arr2)
round = 0
count = 1
for i in range(0, len(data2)):
	if i == 0:
		round += data2[i] - 1
	elif data2[i] < data2[i - 1]:
		round = math.ceil(round / data1[0]) * data1[0] - 1 + data2[i]
	else:
		round += data2[i] - data2[i - 1]
print(round)


def main():
	from sys import stdin, stdout
	(n, m) = map(int, stdin.readline().split())
	tup = tuple(map(int, stdin.readline().split()))
	x = 1
	w = 0
	for i in range(m):
		if tup[i] >= x:
			w += tup[i] - x
			x = tup[i]
		else:
			w += n - x + tup[i]
			x = tup[i]
	stdout.write(str(w))
main()


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
t = 0
pos = 1
for task in tasks:
	if task < pos:
		t += n - pos + task
	else:
		t += task - pos
	pos = task
print(t)


def solve():
	(n, m) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	count = arr[0] - 1
	for i in range(1, len(arr)):
		if arr[i] < arr[i - 1]:
			count += n - arr[i - 1] + arr[i]
		else:
			count += arr[i] - arr[i - 1]
	print(count)
solve()


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
curr = 1
s = 0
for i in range(m):
	s += (arr[i] - curr) % n
	curr = arr[i]
print(s)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
count = a[0] - 1
curr = a[0]
for i in range(1, m):
	if a[i] > curr:
		count += a[i] - curr
		curr = a[i]
	elif a[i] == curr:
		continue
	elif a[i] < curr:
		count += n - curr + 1 + a[i] - 1
		curr = a[i]
print(count)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
t1 = a[0] - 1
ans = t1
for i in range(1, len(a)):
	t2 = 0
	if a[i] < a[i - 1]:
		t2 = n - a[i - 1] + a[i]
	if a[i] > a[i - 1]:
		t2 = a[i] - a[i - 1]
	ans = ans + t2
print(ans)


M = lambda : map(int, input().split())
(r, w) = M()
s = M()
(p, ans) = (1, 0)
for i in s:
	if p <= i:
		ans += i - p
	else:
		ans += r - p + i
	p = i
print(ans)


leisita = str(input()).split()
task = str(input()).split()
new_task = [int(x) for x in task]
m = int(leisita[1])
n = int(leisita[0])
i = 0
j = 0
while i < m - 1:
	if new_task[i] <= new_task[i + 1]:
		i = i + 1
	else:
		i = i + 1
		j = j + 1
print(str(j * n + new_task[-1] - 1))


(houses, things) = map(int, input().split())
house = list(map(int, input().split()))
time = 0
current = 1
for i in range(things):
	if house[i] >= current:
		time += house[i] - current
		current = house[i]
	else:
		time += houses - current
		current = 1
		time += 1
		time += house[i] - current
		current = house[i]
print(time)


line = input().split(' ')
n = int(line[0])
m = int(line[1])
line = input().split(' ')
tasks = [int(task) for task in line]
ti = 0
hn = 1
time = 0
while True:
	if tasks[ti] < hn:
		time += n - (hn - tasks[ti])
		hn = tasks[ti]
		ti += 1
	elif tasks[ti] > hn:
		time += tasks[ti] - hn
		hn = tasks[ti]
		ti += 1
	else:
		ti += 1
	if ti < len(tasks) - 2 and tasks[ti] == tasks[ti + 1]:
		ti += 1
	if ti == len(tasks):
		break
print(time)


def xenia_and_ringroad(n, m, a):
	h = 1
	r = 0
	for i in range(m):
		if a[i] > h:
			r += a[i] - h
		elif a[i] < h:
			r += n - (h - a[i])
		h = a[i]
	return r
f = lambda : list(map(int, input().split()))
(n, m) = f()
b = f()
print(xenia_and_ringroad(n, m, b))


(x, y) = map(int, input().split())
arr = [int(arr) for arr in input().split()]
count = arr[0] - 1
for i in range(0, y - 1):
	if arr[i + 1] < arr[i]:
		count += x - arr[i] + arr[i + 1]
	elif arr[i + 1] > arr[i]:
		count += arr[i + 1] - arr[i]
print(count)


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
k = A[0] - 1
for i in range(1, m):
	if A[i] > A[i - 1]:
		k += A[i] - A[i - 1]
	if A[i] < A[i - 1]:
		k += n + A[i] - A[i - 1]
print(k)


mn = input().split()
n = int(mn[0])
m = int(mn[1])
Q = list(map(int, input().split()))
s = Q[0] - 1
for i in range(1, m):
	s += (Q[i] - Q[i - 1]) % n
print(s)


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
s = 1
t = A[0]
for x in A:
	if x < t:
		s += 1
	t = x
print(n * (s - 1) + A[m - 1] - 1)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
for i in range(len(a)):
	if i == 0:
		time = a[i] - 1
		continue
	if a[i] < a[i - 1]:
		time += n - a[i - 1] + a[i]
	else:
		time += a[i] - a[i - 1]
print(time)


(l, n) = map(int, input().split())
lis = [int(x) for x in input().split()]
l -= 1
for i in range(len(lis)):
	lis[i] -= 1
count = lis[0]
for i in range(len(lis) - 1):
	if lis[i] == lis[i + 1]:
		pass
	elif lis[i + 1] > lis[i]:
		count += lis[i + 1] - lis[i]
	else:
		count += l - lis[i] + lis[i + 1] + 1
print(count)


s = input().split()
a = int(s[0])
b = int(s[1])
sl = input().split()
point = 1
res = 0
for i in range(b):
	sl[i] = int(sl[i])
for i in range(b):
	if sl[i] >= point:
		res += sl[i] - point
		point = int(sl[i])
	else:
		res += a + sl[i] - point
		point = int(sl[i])
print(res)


(n, m) = [int(x) for x in input().split()]
aList = [int(x) for x in input().split()]
cnt = aList[0] - 1
for i in range(m - 1):
	if aList[i + 1] >= aList[i]:
		cnt += aList[i + 1] - aList[i]
	else:
		cnt += n + aList[i + 1] - aList[i]
print(cnt)


[n, m] = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
time = a[0]
i = 0
while i < m - 1:
	if a[i] > a[i + 1]:
		time += n - a[i] + a[i + 1]
	else:
		time += a[i + 1] - a[i]
	i += 1
print(time - 1)


n_m = list(map(int, input().split()))
tasks = list(map(int, input().split()))
time = tasks[0] - 1
for i in range(1, len(tasks)):
	if tasks[i] >= tasks[i - 1]:
		time += tasks[i] - tasks[i - 1]
	else:
		time += n_m[0] - tasks[i - 1] + tasks[i]
print(time)


(n, m) = list(map(int, input().split()))
deals = list(map(int, input().split()))
count = deals[0] - 1
for i in range(len(deals) - 1):
	if deals[i + 1] == deals[i]:
		count += 0
	elif deals[i + 1] <= deals[i]:
		count += n - deals[i] + deals[i + 1]
	elif deals[i + 1] >= deals[i]:
		count += deals[i + 1] - deals[i]
print(count)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
out = 0
curr = 1
for i in l:
	if i - curr < 0:
		out += n - abs(i - curr)
	else:
		out += abs(i - curr)
	curr = i
print(out)


(n, m) = map(int, input().split())
num = [int(i) for i in input().split()]
high = 1
check = 0
for i in range(m):
	if num[i] > high:
		check += num[i] - high
	elif num[i] < high:
		check += n - (high - num[i])
	high = num[i]
print(check)


import sys
(n, m) = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))

def travel_time(a, b):
	if a <= b:
		return b - a
	else:
		return (b - a) % n
total_time = 0
start = 1
for i in range(m):
	end = a[i]
	total_time += travel_time(start, end)
	start = end
sys.stdout.write(str(total_time))


(doma, koldel) = (int(i) for i in input().split())
dela = [int(i) for i in input().split()]
krug = 0
b = dela[0]
for i in dela[1:]:
	if b > i:
		krug += 1
	b = i
itog = krug * doma - 1 + dela[-1]
print(itog)


(n, m) = input().split()
(n, m) = (int(n), int(m))
lst = input().split()
for i in range(m):
	lst[i] = int(lst[i])
time = 0
for i in range(m):
	if time == 0:
		time = lst[i] - 1
	elif lst[i] < lst[i - 1]:
		time = time + (n - lst[i - 1] + 1) + (lst[i] - 1)
	else:
		time = time + (lst[i] - lst[i - 1])
print(time)


(n, m) = map(int, input().split())
data = map(int, input().split())
ptr = 1
time = 0
for i in data:
	if i < ptr:
		time += n + i - ptr
	else:
		time += i - ptr
	ptr = i
print(time)


(x, y) = map(int, input().split())
a = list(map(int, input().split()))
if x == 1:
	print('0')
	exit()
(d, sum) = (1, 0)
for b in a:
	if b - d > 0:
		sum += b - d
	elif b - d < 0:
		sum += x - d + b
	d = b
print(sum)


(houses, cases) = map(int, input().split())
arr = list(map(int, input().split()))
time = arr[0] - 1
for i in range(len(arr) - 1):
	if arr[i] <= arr[i + 1]:
		time += arr[i + 1] - arr[i]
	else:
		time += houses - arr[i] + arr[i + 1]
print(time)


x = input()
x = x.split()
y = input()
y = y.split()
na = 0
pamelita = 0
yonax = 1
for k in range(int(x[1]) - 1):
	if int(y[k]) > int(y[k + 1]):
		yonax = 0
		pamelita = pamelita + int(x[0])
pamelita = pamelita + int(y[len(y) - 1]) - 1
print(pamelita)


import sys, os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
sys.setrecursionlimit(10 ** 5)

def i():
	return sys.stdin.readline().strip()

def ii():
	return int(sys.stdin.readline())

def li():
	return list(sys.stdin.readline().strip())

def mii():
	return map(int, sys.stdin.readline().split())

def lii():
	return list(map(int, sys.stdin.readline().strip().split()))

def solve():
	(n, m) = mii()
	a = lii()
	s = a[0]
	for i in range(1, m):
		if a[i - 1] <= a[i]:
			s += a[i] - a[i - 1]
		else:
			s += n - a[i - 1] + a[i]
	print(s - 1)
for _ in range(1):
	solve()


def XeniaAndRingRoad(n, m, a):
	i = 1
	timeTaken = a[0] - 1
	while i < m:
		diff = a[i] - a[i - 1]
		if diff < 0:
			timeTaken = timeTaken + diff + n
		else:
			timeTaken += diff
		i += 1
	return timeTaken
(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
print(XeniaAndRingRoad(n, m, a))


user = list(input().split(' '))
n = int(user[0])
m = int(user[1])
arr = list(input().split(' '))
sum = 0
c = 0
if m == 0:
	c = 1
for i in range(0, m - 1):
	if int(arr[i]) < int(arr[i + 1]):
		sum = sum + int(arr[i + 1]) - int(arr[i])
	elif int(arr[i]) > int(arr[i + 1]):
		sum = sum + n - (int(arr[i]) - int(arr[i + 1]))
	else:
		sum = sum + 0
if c == 1:
	print(0)
else:
	print(sum + int(arr[0]) - 1)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
c = 1
ans = 0
for i in a:
	ans += i - c + (n if i - c < 0 else 0)
	c = i
print(ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
s = 1
c = 0
for x in l:
	if s < x:
		c += x - s
	elif x < s:
		c += n - s + x
	s = x
print(c)


(n, m) = tuple(map(int, input().split()))
s = 0
t = [1]
t.extend(list(map(int, input().split())))
for i in range(len(t))[:-1]:
	s += (t[i + 1] - t[i]) % n
print(s)


q = input()
z = q.split()
n = int(z[0])
m = int(z[1])
pointer = 1
qw = input()
ring = qw.split()
for i in range(0, m):
	ring[i] = int(ring[i])
count = 0
for i in range(0, m):
	if ring[i] > pointer:
		count += ring[i] - pointer
	elif ring[i] == pointer:
		continue
	else:
		count += n - (pointer - ring[i])
	pointer = ring[i]
print(count)


(a, b) = map(int, input().split())
d = list(map(int, input().split()))
answer = 0
for i in range(len(d)):
	if i != 0:
		if d[i - 1] > d[i]:
			answer += a - d[i - 1] + d[i]
		elif d[i - 1] < d[i]:
			answer += d[i] - d[i - 1]
print(answer + d[0] - 1)


(n, m) = map(int, input().split())
tasks = map(int, input().split())
res = 0
loc = 1
for task in tasks:
	if task >= loc:
		res += task - loc
	else:
		res += n - (loc - task)
	loc = task
print(res)


(n, m) = tuple(map(int, input().split()))
l = list(map(int, input().split()))
cur = 1
time = 0
for i in l:
	if i > cur:
		time += i - cur
	elif i < cur:
		time += n - (cur - i)
	cur = i
print(time)


(a, b) = map(int, input().split())
x = list(map(int, input().split()))
m = x[0] - 1
for i in range(1, b):
	if x[i] >= x[i - 1]:
		m += x[i] - x[i - 1]
	else:
		m += a - x[i - 1] + x[i]
print(m)


(houses, things) = [int(i) for i in input().split()]
spec = [int(i) for i in input().split()]
time = 0
loc = 1
for i in range(things):
	if spec[i] >= loc:
		time += spec[i] - loc
		loc = spec[i]
	else:
		time += houses + spec[i] - loc
		loc = spec[i]
print(time)


a = [int(i) for i in input().split()]
b = [1] + [int(i) for i in input().split()]
c = 0
sum = 0
for i in range(0, a[1]):
	if b[i] > b[i + 1]:
		c = a[0] - b[i] + b[i + 1]
	elif b[i] < b[i + 1]:
		c = b[i + 1] - b[i]
	elif b[i] == b[i + 1]:
		c = 0
	sum = sum + c
print(sum)


from itertools import accumulate as am, cycle as cy, permutations as perm, repeat
import math
from bisect import bisect

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
(n, m) = mi()
(p, tot) = (1, 0)
arr = [int(i) for i in input().split()]
for b in arr:
	if b >= p:
		tot += b - p
	else:
		tot += n - p + b
	p = b
print(tot)


(n, m) = input().split()
(n, m) = (int(n), int(m))
a = [int(i) for i in input().split()]
start = 1
res = 0
for i in a:
	if start <= i:
		res += i - start
	else:
		res += n - start + 1 + i - 1
	start = i
print(res)


(max_n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
time = 0
curent_building = 1
count = 0
for work_building in a:
	if work_building > curent_building:
		delta = work_building - curent_building
		time += delta
		curent_building += delta
	elif work_building < curent_building:
		delta = max_n - curent_building + work_building
		time += delta
		curent_building = work_building
print(time)


(n, m) = list(map(int, input().split()))
tasklist = list(map(int, input().split()))
cur = 1
cir = 0
for task in tasklist:
	if cur <= task:
		cur = task
	else:
		cir += 1
		cur = task
print(cur + n * cir - 1)


(n, m) = map(int, input().split(' '))
L = [int(x) for x in input().split()]
s = 0
p = 1
for x in L:
	if p > x:
		s += x + n - p
	else:
		s += x - p
	p = x
print(s)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
time = tasks[0] - 1
for i in range(1, len(tasks)):
	time += tasks[i] - tasks[i - 1]
	if tasks[i] < tasks[i - 1]:
		time += n
print(time)


ip = input().split()
n = ip[0]
m = ip[1]
tasks = list(input().split(' '))
weight = 0
for i in range(len(tasks)):
	if int(tasks[i]) > 1:
		weight = int(tasks[i]) - 1
		break
i = i + 1
while i < int(m):
	if int(tasks[i]) < int(tasks[i - 1]):
		weight = weight + int(tasks[i]) - int(tasks[i - 1]) + int(n)
		i = i + 1
	else:
		weight = weight + int(tasks[i]) - int(tasks[i - 1])
		i = i + 1
print(weight)


n = int(input().split()[0])
lst = list(map(int, input().split()))
ans = sum(((i + n - j) % n for (i, j) in zip(lst, [1] + lst)))
print(ans)


(n, m) = map(int, input().split())
x = list(map(int, input().split()))
ini_pos = 1
curr_pos = ini_pos
time = 0
for i in x:
	if i >= curr_pos:
		time += i - curr_pos
		curr_pos = i
	else:
		time += n - curr_pos + i
		curr_pos = i
print(time)


(n, m) = map(int, input().split())
A = [1] + list(map(int, input().split()))
time_counter = 0
for i in range(m):
	delta = A[i + 1] - A[i]
	time_counter += delta if delta >= 0 else n + delta
print(time_counter)


(n, m) = map(int, input().split())
ls = list(map(int, input().split()))
ans = 0
p = 1
i = 0
while i < m:
	ans += ls[i] - p
	p = ls[i]
	try:
		if ls[i + 1] < ls[i]:
			ans += n - ls[i]
			p = 0
	except:
		pass
	i += 1
print(ans)


(n, m) = [int(x) for x in input().strip().split()]
arr = [int(x) for x in input().strip().split()]
curr = 1
steps = 0
for i in arr:
	steps = steps + (i - curr) % n
	curr = i
print(steps)


(n, m) = map(int, input().split())
s = 0
i = int()
a = [1]
b = [int(x) for x in input().split()]
a.extend(b)
for i in range(m):
	if a[i] > a[i + 1]:
		s += a[i + 1] + n - a[i]
	else:
		s += a[i + 1] - a[i]
print(s)


(n, m) = map(int, input().split())
houses_2go = list(map(int, input().split()))
(currently_house, count) = (1, 0)
for item in houses_2go:
	if item > currently_house:
		count += item - currently_house
		currently_house = item
	elif item == currently_house:
		pass
	else:
		count += n - (currently_house - item)
		currently_house = item
print(count)


a = list(map(int, input().split()))
n = a[0]
m = a[1]
a = list(map(int, input().split()))
current = 1
ans = 0
for elem in a:
	if elem >= current:
		ans += elem - current
	else:
		ans += n - (current - elem)
	current = elem
print(ans)


(n, m) = map(int, input().split())
x = [int(i) for i in input().split()]
sum = -1
for i in range(1, len(x)):
	if x[i] < x[i - 1]:
		sum += n - (x[i - 1] - x[i])
	elif x[i] == x[i - 1]:
		sum += 0
	else:
		sum += x[i] - x[i - 1]
print(sum + x[0])


from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
input = sys.stdin.readline
(n, m) = map(int, input().split())
ar = [1] + list(map(int, input().split()))
answer = 0
for i in range(m):
	if ar[i] <= ar[i + 1]:
		answer += ar[i + 1] - ar[i]
	else:
		answer += n - (ar[i] - ar[i + 1])
print(answer)


l = [int(x) for x in input().split(' ')]
n = l[0]
m = l[1]
l1 = [int(x) for x in input().split(' ')]
sum1 = 0
k = 1
for i in l1:
	if i < k:
		sum1 = sum1 + (n - k)
		k = 0
	sum1 = sum1 + (i - k) % n
	k = i
print(sum1)


(n, m) = input().split(' ')
n = int(n)
m = int(m)
a = list(input().split(' '))
a = [int(i) for i in a]
s = 0
for i in range(0, m - 1):
	if i == m - 2 and a[i] <= a[i + 1]:
		s += a[i + 1]
	elif a[i] > a[i + 1] and i != m - 2:
		s += n
	elif i == m - 2 and a[i] > a[i + 1]:
		s += a[i + 1] + n
if m != 1:
	print(s - 1)
else:
	print(a[0] - 1)


(n, m) = [int(x) for x in input().split()]
tasks = [int(x) for x in input().split()]
(pos, time) = (1, 0)
for tsk in tasks:
	if tsk >= pos:
		time += tsk - pos
	else:
		time += n - pos + tsk
	pos = tsk
print(time)


(n, m) = map(int, input().split())
work = list(map(int, input().split()))
res = 0
home_now = 1
for k in work:
	if home_now < k:
		res += k - home_now
		home_now = k
	elif home_now > k:
		res += n - home_now + k
		home_now = k
print(res)


a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
count = b[0] - 1
for i in range(1, a[1]):
	if b[i] >= b[i - 1]:
		count += b[i] - b[i - 1]
	elif b[i] < b[i - 1]:
		count += a[0] - b[i - 1] + b[i]
print(count)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
a = 1
count = 0
b = l[0]
for i in range(m):
	if b >= a:
		count += b - a
	else:
		count += n - a + b
	if i == m - 1:
		break
	a = l[i]
	b = l[i + 1]
print(count)


(n, m) = list(map(int, input().split(' ')))
x = list(map(int, input().split(' ')))
(i, count) = (1, 0)
while i < m:
	count += x[i] - x[i - 1] if x[i] >= x[i - 1] else n - x[i - 1] + x[i]
	i += 1
print(count + x[0] - 1)


(x, y) = map(int, input().split())
l = list(map(int, input().split()))
s = l[0] - 1
i = 1
while i < y:
	if l[i - 1] == l[i]:
		pass
	elif l[i - 1] > l[i]:
		s += x - l[i - 1]
		s += l[i]
	else:
		s += l[i] - l[i - 1]
	i += 1
print(s)


(n, m) = map(int, input().split())
l = map(int, input().split())
curr = 1
c = 0
for x in l:
	c += (n - curr + x, x - curr)[x >= curr]
	curr = x
print(c)


from sys import stdout, stdin
(n, m) = map(int, stdin.readline().split())
arr = list(map(int, stdin.readline().split()))
ans = arr[0] - 1
for i in range(m - 1):
	ans += (arr[i + 1] - arr[i]) % n
stdout.write(str(ans) + '\n')


(n, m) = map(int, input().split())
l = 1
ans = 0
now = list(map(int, input().strip().split()))[:m]
for j in now:
	x = int(j)
	if x >= l:
		ans += x - l
	else:
		ans += n - (l - x)
	l = x
print(ans)


(n, m) = map(int, input().split())
L = [1]
L.extend(map(int, input().split()))
count = 0
for i in range(m):
	if L[i + 1] < L[i]:
		count += n + L[i + 1] - L[i]
	else:
		count += L[i + 1] - L[i]
print(count)


(n, m) = map(int, input().split())
li_m = list(map(int, input().split()))
time = 0
for i in range(m):
	if time == 0:
		time += li_m[i] - 1
		ptr = li_m[i]
	elif ptr > li_m[i]:
		time += n - ptr + li_m[i]
		ptr = li_m[i]
	else:
		time += li_m[i] - ptr
		ptr = li_m[i]
print(time)


while True:
	try:

		def read():
			(n, m) = map(int, input().split())
			a = list(map(int, input().split()))
			solve(n, m, a)

		def solve(n, m, a):
			ans = 0
			pos = 1
			for i in range(m):
				if a[i] >= pos:
					ans += a[i] - pos
				else:
					ans += a[i] + n - pos
				pos = a[i]
			print(ans)
		if __name__ == '__main__':
			read()
	except EOFError:
		break


(n, m) = input().split()
(n, m) = (int(n), int(m))
a = input().split()
s = 0
i = 1
if m == 1:
	print(int(a[0]) - 1)
else:
	while i < m:
		if int(a[i]) - int(a[i - 1]) < 0:
			s += 1
		i += 1
	b = s * n + int(a[m - 1]) - 1
	print(b)


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
sec = 0
houseIn = 1
for i in a:
	if houseIn <= i:
		sec = sec + (i - houseIn)
	else:
		sec = sec + (n - houseIn) + i
	houseIn = i
print(sec)


(n, m) = map(int, input().split())
L = list(map(int, input().split()))
s = 0
j = 1
for i in range(m):
	if L[i] < j:
		s += n - j + L[i]
		j = L[i]
	else:
		s += L[i] - j
		j = L[i]
print(s)


(n, m) = map(int, input().split())
l = map(int, input().split())
k = 1
c = 0
for i in l:
	c += (i - k) % n
	k = i
print(c)


from sys import stdin
(n, m) = [int(x) for x in stdin.readline().rstrip().split()]
current_index = 1
time = 0
for new_index in [int(x) for x in stdin.read().rstrip().split()]:
	distance = new_index - current_index
	time += distance if distance >= 0 else distance + n
	current_index = new_index
print(time)


(n, m) = map(int, input().split())
l = [int(x) for x in input().split()]
pos = 1
i = 0
answer = 0
for x in range(len(l)):
	if l[x] == pos:
		continue
	elif l[x] > pos:
		answer += l[x] - pos
		pos = l[x]
	elif l[x] < pos:
		answer += n - pos + l[x]
		pos = l[x]
print(answer)


[n, m] = [int(i) for i in input().split()]
a = [int(j) for j in input().split()]
t = a[0] - 1
for k in range(1, m):
	if a[k] >= a[k - 1]:
		t += a[k] - a[k - 1]
	else:
		t += n - a[k - 1] + a[k]
print(t)


l1 = [int(x) for x in input().split()]
l2 = [int(x) for x in input().split()]
step = l2[0] - 1
for i in range(1, len(l2)):
	if l2[i] >= l2[i - 1]:
		step = step + l2[i] - l2[i - 1]
	else:
		step = step + l1[0] - l2[i - 1] + l2[i]
print(step)


(n, m) = map(int, input().split())
l = []
l = input().split()
for i in range(len(l)):
	l[i] = int(l[i])
s = l[0] - 1
for i in range(len(l) - 1):
	if l[i] == l[i + 1]:
		s = s
	elif l[i] > l[i + 1]:
		s = s + n - l[i] + l[i + 1]
	else:
		s = s + l[i + 1] - l[i]
print(s)


(n, m) = [int(i) for i in input().split()]
list_a = [int(i) for i in input().split()]
house_number = 1
ans_t = 0
for i in range(m):
	if list_a[i] >= house_number:
		ans_t += list_a[i] - house_number
		house_number = list_a[i]
	else:
		ans_t += n - (house_number - list_a[i])
		house_number = list_a[i]
print(ans_t)


(n, m) = list(map(int, input().split()))
v = list(map(int, input().split()))
for i in range(1, len(v)):
	if v[i] < v[i - 1]:
		v[i] += (v[i - 1] - v[i]) // n * n
	while v[i] < v[i - 1]:
		v[i] += n
print(v[-1] - 1)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
s = 0
c = 1
for i in range(m):
	t = a[i]
	s += (t - c) % n
	c = t
print(s)


(n, m) = map(int, input().split())
lis = list(map(int, input().split()))
start = 1
res = 1
for i in lis:
	if i >= start:
		res += i - start
	else:
		res += n - start + i
	start = i
print(res - 1)


def Ringroad():
	(n, m) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	time = a[0] - 1
	for i in range(len(a) - 1):
		if a[i] == a[i + 1]:
			pass
		elif a[i] > a[i + 1]:
			cHouse = a[i] - n
			time += a[i + 1] - cHouse
		else:
			time += a[i + 1] - a[i]
	print(time)
Ringroad()


(n, m) = map(int, input().split())
h = [x for x in map(int, input().split())]
time = 0
init = 1
time += h[0] - init
init = h[0]
for i in range(1, m):
	if h[i] < init:
		s = n - init
		time += s + h[i]
		init = h[i]
	else:
		time += h[i] - init
		init = h[i]
print(time)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
x = 1
s = 0
for i in range(len(a)):
	if a[i] < x:
		s += n + a[i] - x
	else:
		s += a[i] - x
	x = a[i]
print(s)


(n, m) = map(int, input().split())
T = []
T = list(map(int, input().split()))
r = T[0] - 1
for i in range(1, m):
	if T[i] > T[i - 1]:
		r = r + T[i] - T[i - 1]
	if T[i] < T[i - 1]:
		r = r + n - T[i - 1] + T[i]
print(r)


(n, m) = [int(c) for c in input().split()]
tasks = [int(c) for c in input().split()]
mom_standpunkt = 1
time = 0
for i in range(len(tasks)):
	if tasks[i] < mom_standpunkt:
		abstand = n - mom_standpunkt + tasks[i]
	else:
		abstand = tasks[i] - mom_standpunkt
	mom_standpunkt = tasks[i]
	time += abstand
print(time)


(n, m) = map(int, input().split())
arr = [int(i) for i in input().split()]
c = 0
for i in range(m - 1):
	if arr[i] > arr[i + 1]:
		c += n
c += arr[-1] - 1
print(c)


import sys, bisect, copyreg, copy, statistics, os

def inp():
	return sys.stdin.readline().strip()

def IIX():
	return (int(x) for x in sys.stdin.readline().split())

def II():
	return int(inp())

def LI():
	return list(map(int, inp().split()))

def L(x):
	return list(x)

def out(var):
	return sys.stdout.write(str(var))
(n, m) = IIX()
tasks = LI()
time = tasks[0] - 1
for i in range(0, m - 1):
	a = tasks[i]
	b = tasks[i + 1]
	if a <= b:
		time += b - a
	elif a > b:
		time += n - a + b
out(time)


import math

def path_to(a, b, n):
	result = b - a
	if a > b:
		result = n - a + b
	return result
(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
result = 0
current = 1
for house in a:
	result += path_to(current, house, n)
	current = house
print(result)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
i = 1
j = 0
count1 = 0
while j < len(l):
	if l[j] >= i:
		count1 = count1 + l[j] - i
		i = l[j]
		j = j + 1
	else:
		count1 = count1 + (n - i) + (l[j] - 1) + 1
		i = l[j]
		j = j + 1
print(count1)


(a, b) = (int(x) for x in input().split())
l1 = [int(x) for x in input().split()]
c = 0
l2 = list()
for i in range(b - 1):
	l2.append(l1[i])
	if l1[i] > l1[i + 1]:
		c = c + 1
		l2.clear()
l2.append(l1[-1])
print(a * c + l1[-1] - 1)


s = input().split()
n = int(s[0])
m = int(s[1])
l = [int(i) for i in input().split()]
a = 0
b = 1
for i in l:
	if i == b:
		continue
	elif i > b:
		a = a + i - b
	else:
		a = a + n + i - b
	b = i
print(a)


(a, b) = map(int, input().split())
s = list(map(int, input().split()))
l = 1
f = 0
for i in s:
	f += (i - l) % a
	l = i
print(f)


(n, m) = map(int, input().split())
p = q = 0
for i in map(int, input().split()):
	i -= 1
	p += (i - q) % n
	q = i
print(p)


[n, m] = [int(i) for i in input().split()]
a = input().split()
t = 0
f = []
for i in range(m):
	if i == 0:
		f.append(int(a[i]) - 1)
	elif int(a[i]) >= int(a[i - 1]):
		f.append(int(a[i]) - int(a[i - 1]))
	else:
		f.append(n - int(a[i - 1]) + int(a[i]))
for i in range(len(f)):
	t = t + f[i]
print(t)


(n, m) = map(int, input().split())
l = input().split()
count = int(l[0]) - 1
for i in range(1, m):
	if int(l[i]) >= int(l[i - 1]):
		count += int(l[i]) - int(l[i - 1])
	else:
		count += int(l[i]) + (n - int(l[i - 1]))
print(count)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
pos = 1
count = 0
for i in l:
	if pos < i:
		count += i - pos
		pos = i
	elif pos > i:
		count += n - pos + i
		pos = i
print(count)


(n, m) = map(int, input().split())
arr = [int(x) - 1 for x in input().split()]
(p, t) = (0, 0)
for x in arr:
	if x < p:
		t += n - abs(x - p)
	else:
		t += abs(x - p)
	p = x
print(t)


(n, m) = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
val = 0
prev = 1
for ai in a:
	val += (ai - prev + n) % n
	prev = ai
print(val)


(n, m) = map(int, input().split())
tsk = [int(i) for i in input().split()]
time = tsk[0] - 1
now = tsk[0]
for j in range(1, len(tsk)):
	i = tsk[j]
	if i >= now:
		time += i - now
	else:
		time += n - now + i
	now = i
print(time)


(n, m) = map(int, input().split())
l = list(map(int, input().strip().split()))
l.insert(0, 1)
r = 0
z = []
q = []
for i in l[1:]:
	if i > l[r]:
		a = i - l[r]
		z.append(a)
	elif i < l[r]:
		b = n - l[r] + (i - 0)
		q.append(b)
	r += 1
print(sum(z) + sum(q))


a = input().split()
b = int(a[0])
n = int(a[1])
task = [int(i) for i in input().split()]
st = 1
ans = 0
for i in task:
	stp = i - st
	if stp < 0:
		stp += b
	else:
		pass
	st = i
	ans += stp
print(ans)


(n, m) = map(int, input().split())
list = [int(i) for i in input().split()]
x = 0
p = 1
for i in list:
	if p < i:
		x += i - p
		p = i
	elif p == i:
		p = i
	else:
		x += n - p + i
		p = i
print(x)


from sys import stdin

def main():
	(n, m) = map(int, stdin.readline().strip().split())
	(a, tot) = (1, 0)
	for b in map(int, stdin.readline().strip().split()):
		if a != b:
			tot += (b - a) % n
			a = b
	return tot
print(main())


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
r = sum(((x + n - y) % n for (x, y) in zip(a, [1] + a)))
print(r)


info = [int(x) for x in input().split()]
tasks = [int(x) for x in input().split()]
cb = 0
for i in range(1, info[1]):
	if tasks[i] < tasks[i - 1]:
		cb += 1
print(cb * info[0] + tasks[-1] - 1)


(n, m) = map(int, input().split())
data = map(int, input().split())
cur = 1
round = 0
for x in data:
	if cur <= x:
		cur = x
	else:
		round += 1
		cur = x
print(cur + n * round - 1)


(n, m) = input().split(' ')
(n, m) = (int(n), int(m))
a = list(map(int, input().split(' ')))
t = 0
x = 1
for i in range(len(a)):
	if a[i] < x:
		t = t + a[i] + n - x
		x = a[i]
	elif a[i] > x:
		t = t + a[i] - x
		x = a[i]
print(t)


(n, m) = [int(i) for i in input().split()]
(cur, time) = (1, 0)
for i in input().split():
	i = int(i)
	if cur <= i:
		time += i - cur
	else:
		time += n - cur + i
	cur = i
print(time)


(a, b) = list(map(int, input().split()))
houses = list(map(int, input().split()))
count = int(0)
current = int(1)
for i in houses:
	if i > current:
		count += i - current
	elif i < current:
		count += i + a - current
	current = i
print(count)


(n, m) = map(int, input().split())
array = map(int, input().split())
tmp = 1
ans = 0
for i in array:
	ans += (i - tmp) % n
	tmp = i
print(ans)


def main():
	(n, m) = map(int, input().split())
	li = list(map(int, input().split()))
	curr = 1
	ans = 0
	for i in li:
		if curr <= i:
			ans += i - curr
		else:
			ans += n - curr + i
		curr = i
	print(ans)
main()


n = input()
n = n.split()
m = int(n[1])
n = int(n[0])
k = input()
k = k.split()
for i in range(m):
	k[i] = int(k[i])
steps = 0
current = 1
for i in range(m):
	if current <= k[i]:
		q = k[i] - current
	elif current > k[i]:
		q = n - (current - k[i])
	current = k[i]
	steps += q
print(steps)


(n, m) = [int(x) for x in input().split()]
As = [int(x) for x in input().split()]
ans = 0
current = 1
for a in As:
	if a > current:
		ans += a - current
	elif a < current:
		ans += n + a - current
	current = a
print(ans)


(n, m) = tuple(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
l = 1
for i in a:
	if i > l:
		ans += i - l
	elif i == l:
		continue
	else:
		ans += n - l + i
	l = i
print(ans)


(n, m) = map(int, input().split())
list = input().split()
list = [int(i) for i in list]
steps = list[0] - 1
for i in range(m - 1):
	if list[i + 1] >= list[i]:
		steps += list[i + 1] - list[i]
	else:
		steps += list[i + 1] - list[i] + n
print(steps)


(n, m) = input().split()
(n, m) = (int(n), int(m))
move = 0
a = [int(i) for i in input().split()]
before = 1
for i in a:
	if i != before:
		move += (i + n - before) % n
	before = i
print(move)


(n, m) = map(int, input().split())
a1 = input().split()
a = []
for i in a1:
	a.append(int(i))
p = 0
for i in range(len(a)):
	if i == 0:
		k = a[i] - 1
	elif a[i] > p:
		k += a[i] - p
	elif a[i] < p:
		k += n - p + a[i]
	p = a[i]
print(k)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
(res, e) = (0, 1)
for (i, x) in enumerate(a):
	if x >= e:
		res += x - e
		e = x
	else:
		res += n - e + x
		e = x
print(res)


(n, m) = [int(i) for i in input().split()]
s = 0
o = 1
events = [int(i) for i in input().split()]
for i in range(m):
	a = events[i]
	if o <= a:
		s = s + (a - o)
		o = a
	else:
		s = s + (n - o) + a
		o = a
print(s)


(n, m) = input().split()
(n, m) = (int(n), int(m))
x = input().split()
x = [int(x[i]) for i in range(m)]
(t, b, u) = (0, 1, 0)
for i in range(m):
	a = x[i]
	if a < b:
		a += n
		u += 1
	b = x[i]
c = x[len(x) - 1] - 1
print(u * n + c)


(n, m) = map(int, input().split())
todo = [int(x) for x in input().split()]
i = 0
where = 1
time = 0
while i < m:
	item = todo[i]
	if item != where:
		time += n - (where - item) % n
		where = item
	else:
		i += 1
print(time)


[n, m] = [int(x) for x in input().split()]
houses = [int(x) for x in input().split()]
current = 1
time = 0
i = 0
while i < len(houses):
	if houses[i] >= current:
		time += houses[i] - current
		current = houses[i]
	else:
		time += n + houses[i] - current
		current = houses[i]
	i += 1
print(time)


(n, t) = map(int, input().split())
nums = [int(x) for x in input().split()]
total = 0
start = 1
for x in range(0, t):
	if nums[x] < start:
		total += n - start + nums[x]
	else:
		total += nums[x] - start
	start = nums[x]
print(total)


(n, m) = input().split()
n = int(n)
m = int(m)
flag = 0
a = 0
s = input().split()
s.insert(0, 1)
for i in range(m):
	if int(s[i]) <= int(s[i + 1]):
		a = a + int(s[i + 1]) - int(s[i])
	else:
		a = a + n - int(s[i]) + int(s[i + 1])
print(a)


i = lambda : map(int, input().split())
(n, m) = i()
t = 0
h = 1
for x in i():
	if x - h < 0:
		t += n + (x - h)
	else:
		t += x - h
	h = x
print(t)


(n, t) = map(int, input().split())
h = list(map(int, input().split()))
ini = 1
time = 0
for i in range(len(h)):
	c = h[i] - ini
	if c >= 0:
		time += c
	else:
		l = n - ini + h[i]
		time += l
	ini = h[i]
print(time)


(n, m) = list(map(int, input().split()))
arr = list(map(int, input().split()))
res = arr[0] - 1
for i in range(0, m - 1):
	if arr[i] <= arr[i + 1]:
		res = res + arr[i + 1] - arr[i]
	else:
		res = res + arr[i + 1] + (n - arr[i])
print(res)


(n, m) = input().split()
n = int(n)
A = [int(x) for x in input().split()]
A = [1] + A
ans = 0
for i in range(len(A) - 1):
	if A[i] <= A[i + 1]:
		ans += A[i + 1] - A[i]
	else:
		ans += n - A[i] + A[i + 1]
print(ans)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def L():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
try:
	sys.stdin = open('input.txt', 'r')
except:
	pass
(n, m) = L()
A = L()
ans = 0
curr = 1
for i in range(m):
	if A[i] >= curr:
		ans += A[i] - curr
	else:
		ans += n - curr
		ans += 1
		ans += A[i] - 1
	curr = A[i]
print(ans)
endtime = time.time()


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
init = 1
ans = 0
for val in a:
	diff = val - init
	if diff == 0:
		continue
	elif diff > 0:
		ans += diff
		init = val
	elif diff < 0:
		ans += n - init + val
		init = val
print(ans)


(n, m) = input().split()
n = int(n)
m = int(m)
l = input().split()
l = list(map(int, l))
c = 1
t = 0
for i in l:
	if i < c:
		t = t + n + 1 - c
		c = 1
	t = t + i - c
	c = i
print(t)


(homes, business) = map(int, input().split())
businesses = list(map(int, input().split()))
current_home = 1
time = 0
for b in range(business):
	if businesses[b] < current_home:
		time += homes - current_home + businesses[b]
		current_home = businesses[b]
	else:
		time += businesses[b] - current_home
		current_home = businesses[b]
print(time)


x = input().split()
b = input().split()
a = 1
i = 0
count = 0
for i in b:
	if a <= int(i):
		count = count + (int(i) - a)
	else:
		count = count + (int(x[0]) - a) + int(i)
	a = int(i)
print(count)


import sys
(n, m) = list(map(int, sys.stdin.readline().rstrip().split(' ')))
s = list(map(int, sys.stdin.readline().rstrip().split(' ')))
ss = 0
p0 = 0
for i in s:
	if i - 1 > p0:
		ss += i - 1 - p0
		p0 = i - 1
	elif i - 1 < p0:
		ss += i - 1 + n - p0
		p0 = i - 1
print(ss)


(n, m) = input().split()
n = int(n)
m = int(m)
tasks = [int(i) for i in input().split()]
t = 0
t = tasks[0] - 1
for k in range(1, m):
	if tasks[k] >= tasks[k - 1]:
		t = t + tasks[k] - tasks[k - 1]
	else:
		t = t + tasks[k] + n - tasks[k - 1]
print(t)


(n, m) = [int(i) for i in input().split()]
mas = [int(i) for i in input().split()]
cur = 1
suma = 0
for i in range(m):
	suma += (mas[i] - cur) % n
	cur = mas[i]
print(suma)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
sm = 0
sm += arr[0] - 1
for i in range(1, m):
	if arr[i] < arr[i - 1]:
		sm += n - arr[i - 1] + arr[i]
	elif arr[i] > arr[i - 1]:
		sm += arr[i] - arr[i - 1]
print(int(sm))


import os, sys
from collections import defaultdict, Counter
from io import BytesIO, IOBase
from math import ceil

def main():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	an = a[0] - 1
	for i in range(1, m):
		if a[i] >= a[i - 1]:
			an += a[i] - a[i - 1]
		else:
			an += n - a[i - 1] + a[i]
	print(an)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


(n, m) = [int(x) for x in input().split()]
a = list(map(int, input().split()))
a.insert(0, 1)
flag = 0
for i in range(len(a) - 1):
	if a[i + 1] > a[i]:
		flag += a[i + 1] - a[i]
	elif a[i + 1] < a[i]:
		flag += n - a[i] + a[i + 1] - 0
	else:
		flag += 0
print(flag)


(n, m) = map(int, input().split())
pos = 1
t = 0
l = list(map(int, input().split()))
for i in range(m):
	t += (l[i] - pos) % n
	pos = l[i]
print(t)


(n, m) = [int(__) for __ in input().strip().split()]
arr = [int(__) for __ in input().strip().split()]
cur = 1
ans = 0
for i in range(m):
	if arr[i] != cur:
		if arr[i] > cur:
			ans += arr[i] - cur
		else:
			ans += arr[i] + n - cur
		cur = arr[i]
print(ans)


line1 = input().split()
line2 = list(map(int, input().split()))
n = int(line1[0])
m = int(line1[1])
k = 0
s = 0
for i in range(m - 1):
	if line2[i] > line2[i + 1]:
		k += 1
s = k * n + line2[-1] - 1
print(s)


w = input().split(' ')
n = int(w[0])
m = int(w[1])
w2 = input().split(' ')
time = abs(int(w2[0]) - 1)
for i in range(m - 1):
	if int(w2[i + 1]) < int(w2[i]):
		time = time + n - int(w2[i]) + int(w2[i + 1])
	elif int(w2[i + 1]) > int(w2[i]):
		time = time + int(w2[i + 1]) - int(w2[i])
	else:
		time = time
print(time)


(n, t) = map(int, input().split())
houses = list(map(int, input().split()))
time = 0
last = 1
for i in houses:
	if i > last:
		time += abs(last - i)
	elif i < last:
		time += abs(n - last) + 1 + abs(1 - i)
	last = i
print(time)


(n, m) = input().split()
n = int(n)
p = 1
dis = 0
for x in input().split():
	y = int(x)
	if y >= p:
		dis += y - p
	else:
		dis += y - p + n
	p = y
print(dis)


p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
t = q[0] - 1
for i in range(1, len(q)):
	if q[i] >= q[i - 1]:
		t += q[i] - q[i - 1]
	else:
		t += p[0] + q[i] - q[i - 1]
print(t)


(n, m) = map(int, input().split())
a = map(int, input().split())
total = 0
prev = 1
for b in a:
	if b > prev:
		total += b - prev
	elif b < prev:
		total += n - prev + b
	prev = b
print(total)


h = 0
p = 1
(n, m) = map(int, input().split())
a = map(int, input().split())
for i in a:
	h += (i - p) % n
	p = i
print(h)


(n, m) = map(int, input().split())
houses = list(map(int, input().split()))
i = 1
res = 0
for h in houses:
	if i <= h:
		res += h - i
		i = h
	else:
		res += n - i + h
		i = h
print(res)


(a, b) = input().split()
a = int(a)
b = int(b)
z = 1
c = input().split()
t = 0
for i in range(b):
	r = int(c[i])
	if r >= z:
		t += r - z
	else:
		t += a - z + r
	z = r
print(t)


(n, m) = map(int, input().split())
x = list(map(int, input().split()))
temp = 1
s = 0
for i in range(m):
	s += (x[i] - temp + n) % n
	temp = x[i]
print(s)


(a, b) = map(int, input().split())
s = 0
re0 = 1
ls = input().split()
for i in range(b):
	if int(ls[i]) >= re0:
		s += int(ls[i]) - re0
	else:
		s += a + (int(ls[i]) - re0)
	re0 = int(ls[i])
print(s)


def x(n, b, a):
	if b > a:
		return n + a - b
	else:
		return a - b
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
s = l[0] - 1
for i in range(1, len(l)):
	s = s + x(n, l[i - 1], l[i])
print(s)


global n
(n, m) = input().split()
n = int(n)
m = int(m)
l = input().split()

def dis(x, y):
	x = int(x)
	y = int(y)
	if y >= x:
		return y - x
	else:
		return n - x + y
d = dis(1, l[0])
for i in range(m - 1):
	d += dis(l[i], l[i + 1])
print(d)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.insert(0, 1)
print(sum(([a[i] - a[i - 1], n - a[i - 1] + a[i]][a[i] < a[i - 1]] for i in range(1, m + 1))))


(n, m) = [int(i) for i in input().split()]
a = [0] * 1
a[0] = 1
ans = 0
s = input().split()
for i in s:
	a.append(int(i))
for i in range(1, m + 1):
	if a[i] - a[i - 1] < 0:
		ans += a[i] + n - a[i - 1]
	else:
		ans += a[i] - a[i - 1]
print(ans)


l = int(input().split()[0])
tasks = input().split()
p = 1
a = 0
for t in tasks:
	if int(t) < p:
		a = a + int(t) + l - p
		p = int(t)
		continue
	if int(t) > p:
		a = a + int(t) - p
		p = int(t)
print(a)


(n, q) = map(int, input().split())
l = list(map(int, input().split()))
r = l[0] - 1
for i in range(1, q):
	if l[i] - l[i - 1] < 0:
		r += n - l[i - 1] + l[i]
	else:
		r += l[i] - l[i - 1]
print(r)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
i = 0
pos = 1
t = 0
for i in range(m):
	if a[i] > pos:
		t = t + a[i] - pos
	elif a[i] < pos:
		t = t + n - pos + a[i]
	else:
		pass
	pos = a[i]
print(t)


(n, t) = list(map(int, input().split()))
z = list(map(int, input().split()))
t = 0
curr_po = 1
for i in z:
	if curr_po <= i:
		t += i - curr_po
	else:
		t += n + i - curr_po
	curr_po = i
print(t)


s = [int(i) for i in input().split()]
d = [int(i) for i in input().split()]
answer = d[0] - 1
for i in range(s[1] - 1):
	if d[i] > d[i + 1]:
		answer += s[0] - d[i] + d[i + 1]
	elif d[i] < d[i + 1]:
		answer += d[i + 1] - d[i]
print(answer)


(n, m) = map(int, input().split())
task = [int(i) for i in input().split()]
time = task[0] - 1
for j in range(1, m):
	if task[j] >= task[j - 1]:
		time += task[j] - task[j - 1]
	else:
		time += task[j] - task[j - 1] + n
print(time)


A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
C = [0] * (A[1] - 1)
c = 0
for i in range(1, A[1]):
	m = B[i] - B[i - 1]
	if m < 0:
		c += 1
print(c * A[0] + B[-1] - 1)


t = input().split()
n = int(t[0])
m = int(t[1])
u = m - 1
s = [0] * (m + 1)
k = input().split()
for i in range(m):
	s[i] = int(k[i])
for i in range(m - 1):
	if s[i] <= s[i + 1]:
		u = u - 1
o = u * n + s[m - 1] - 1
print(o)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
f = 1
for i in a:
	ans += (i - f + n) % n
	f = i
print(ans)


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
k = a[0]
i = 0
while i < m - 1:
	if a[i] < a[i + 1]:
		k += a[i + 1] - a[i]
		i += 1
	elif a[i] == a[i + 1]:
		i += 1
	else:
		k += n - a[i] + a[i + 1]
		i += 1
print(k - 1)


ss = lambda : input()
si = lambda : int(input())
(n, m) = map(int, ss().split())
ret = 0
s = 1
for d in map(int, ss().split()):
	ret += d - s if d - s >= 0 else d - s + n
	s = d
print(ret)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
current = 1
c = 0
for x in tasks:
	if current <= x:
		c += x - current
		current = x
	else:
		c += n - current + 1
		current = 1
		c += x - current
		current = x
print(c)


(room, n) = map(int, input().split(' '))
tasks = input().split(' ')
(res, now) = (0, 1)
for task in tasks:
	if int(task) >= now:
		res += int(task) - now
	else:
		res += room + int(task) - now
	now = int(task)
print(res)


(n, m) = list(map(int, input().split()))
l = list(map(int, input().split()[:m]))
res = 0
if len(l) == 2:
	res = l[1] - l[0]
elif len(l) == 1:
	res = 0
else:
	res = res + (l[0] - 1)
	for i in range(1, m):
		if l[i] < l[i - 1]:
			res = res + (n - l[i - 1]) + l[i]
		elif l[i] > l[i - 1]:
			res = res + (l[i] - l[i - 1])
		else:
			res = res + 0
print(res)


(n, m) = map(int, input().split())
a = (1,) + tuple(map(int, input().split()))
ans = 0
for i in range(1, len(a)):
	ans += (a[i] - a[i - 1] + n) % n
print(ans)


(n, m) = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
count = 0
pos = 1
for i in arr:
	if pos == i:
		continue
	elif i > pos:
		count += i - pos
		pos = i
	else:
		count = count + (n - pos) + i
		pos = i
print(count)


[m, a] = [int(i) for i in input().split()]
line = [int(i) for i in input().split()]
s = 1
t = 0
for i in line:
	if i - s >= 0:
		t = t + i - s
		s = i
	else:
		t = t + m + i - s
		s = i
print(t)


(house_count, buisnes_count) = map(int, input().split(' '))
buisneses = list(map(int, input().split(' ')))
time = 0
that_house = 1
for next_house in buisneses:
	if next_house == that_house:
		pass
	elif next_house < that_house:
		time += house_count - that_house + next_house
		that_house = next_house
	else:
		time += next_house - that_house
		that_house = next_house
print(time)


(n, m) = list(map(int, input('').split()))
l = list(map(int, input('').split()))
t = l[0] - 1
for i in range(0, len(l) - 1):
	a = n - l[i] + l[i + 1]
	b = l[i + 1] - l[i]
	if l[i] > l[i + 1]:
		t = t + a
	elif l[i] < l[i + 1]:
		t = t + b
	else:
		continue
print(t)


(n, m) = [int(x) for x in input().split()]
time = 0
ar = [1]
arr = [int(y) for y in input().split()]
arr = ar + arr
for i in range(m):
	if arr[i + 1] > arr[i]:
		time += arr[i + 1] - arr[i]
	elif arr[i + 1] == arr[i]:
		time += 0
	else:
		time += n - arr[i] + arr[i + 1]
print(time)


(n, d) = list(map(int, input().split()))
s = list(map(int, input().split()))
(c, v) = (1, 0)
try:
	for i in s:
		if s[c] > i:
			v += s[c] - i
		elif s[c] == i:
			pass
		else:
			v += n - i + s[c]
		c += 1
except:
	pass
print(v + s[0] - 1)


(house, work) = input().split()
house = int(house)
work = int(work)
lst = list(map(int, input().split()))
previous = 1
last = house
time = 0
for current in lst:
	if previous > current:
		time += last - previous + current
	elif previous == current:
		time += 0
	else:
		time += current - previous
	previous = current
print(time)


def Xenia_and_Ringroad():
	line = input()
	(n, m) = line.split(' ')
	n = int(n)
	m = int(m)
	line = input()
	task = line.split(' ')
	task = [int(item) for item in task]
	ans = 0
	pos = 1
	for i in range(m):
		if task[i] == pos:
			continue
		if task[i] > pos:
			ans += task[i] - pos
		else:
			ans += task[i] - pos + n
		pos = task[i]
	print(ans)
Xenia_and_Ringroad()


(n, m) = map(int, input().split())
task = ['1'] + input().split()
time = 0
for i in range(m + 1):
	if i >= m:
		break
	if int(task[i + 1]) >= int(task[i]):
		time = time + int(task[i + 1]) - int(task[i])
	if int(task[i + 1]) < int(task[i]):
		time = time + n + (int(task[i + 1]) - int(task[i]))
print(time)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
result = 0
curr_point = 1
for i in range(len(a)):
	if a[i] > curr_point:
		result += a[i] - curr_point
	elif a[i] < curr_point:
		result += n - curr_point + a[i]
	curr_point = a[i]
print(result)


(z, zz) = (input, lambda : list(map(int, z().split())))
zzz = lambda : [int(i) for i in stdin.readline().split()]
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from string import *
from re import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
(n, m) = zz()
li = [-1] + []
i = 0
for j in zzz():
	if j != li[i]:
		li.append(j)
		i += 1
ans = 0
c = 0
x = li[0]
for i in range(1, len(li)):
	if c == 0:
		ans += li[i] - 1
		c = 1
	elif x < li[i] and c == 1:
		ans += li[i] - x
	else:
		ans += n - x
		ans += li[i]
	x = li[i]
print(ans)


input_str = input()
l1 = input_str.split()
n = int(l1[0])
str_m = input()
l = str_m.split()
for i in range(len(l)):
	l[i] = int(l[i])
time = l[0] - 1
for i in range(1, len(l)):
	if l[i] > l[i - 1]:
		time = time + l[i] - l[i - 1]
	elif l[i] < l[i - 1]:
		time += n - (l[i - 1] - l[i])
print(time)


(n, m) = map(int, input().split())
lst = [int(i) for i in input().split()]
deal = 0
pos = 1
for i in lst:
	if i >= pos:
		deal += i - pos
		pos = i
	else:
		deal += n - pos + 1 + i - 1
		pos = i
print(deal)


(n, m) = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
total = 0
substract = 1
for item in l:
	a = item - substract
	substract = item
	if a < 0:
		a = a + n
	total = total + a
print(total)


import os
import sys
from math import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def array():
	return [int(i) for i in input().split()]
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(k, n) = map(int, input().split())
a = array()
ans = a[0] - 1
for i in range(1, n):
	dif = a[i] - a[i - 1]
	ans += dif % k
print(ans)


line1 = input()
line2 = input()
list1 = line1.split()
list2 = line2.split()
n = list1[0]
m = line1[1]
x = 0
o = 1
ans = 0
for a in list2:
	goal = int(list2[x])
	if goal >= o:
		ans = ans + abs(goal - o)
	else:
		ans = ans + (int(n) - abs(goal - o))
	x = x + 1
	o = goal
print(ans)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
time = 0
house = 1
for i in a:
	if house == i:
		continue
	if i > house:
		time += i - house
	else:
		time += n - house + i
	house = i
print(time)


qwe = input().split()
n = int(qwe[0])
m = int(qwe[1])
line = input().split()
for i in range(m):
	line[i] = int(line[i])
ans = line[0] - 1
for i in range(1, m):
	if line[i] < line[i - 1]:
		ans += n - line[i - 1] + line[i]
	else:
		ans += line[i] - line[i - 1]
print(ans)


[n, m] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
cnt = 0
p = 1
for i in a:
	if i > p:
		cnt += i - p
		p = i
	elif i < p:
		cnt += n - (p - i)
		p = i
	else:
		continue
print(cnt)


(n, m) = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
count = arr[0] - 1
for i in range(1, m):
	if arr[i] >= arr[i - 1]:
		count += arr[i] - arr[i - 1]
	else:
		count += n - arr[i - 1] + arr[i]
print(count)


import math
(n, m) = map(int, input().split())
L = list(map(int, input().split()))
ans = 0
for i in range(m):
	if i == 0:
		ans += L[i] - 1
	elif L[i] >= L[i - 1]:
		ans += L[i] - L[i - 1]
	else:
		ans += n - L[i - 1] + L[i]
print(ans)


(n, m) = input().strip().split()
(n, m) = (int(n), int(m))
a = [int(i) for i in input().strip().split()]
cnt = a[0] - 1
for i in range(1, m):
	if a[i] < a[i - 1]:
		cnt += n - (a[i - 1] - a[i])
	else:
		cnt += a[i] - a[i - 1]
print(cnt)


(house, jobs) = map(int, input().split())
order = list(map(int, input().split()))
nowspot = 1
ans = 0
for i in range(len(order)):
	if order[i] > nowspot:
		ans += order[i] - nowspot
		nowspot = order[i]
	elif order[i] < nowspot:
		ans += house - nowspot + 1
		nowspot = 1
		ans += order[i] - nowspot
		nowspot = order[i]
	elif order[i] == nowspot:
		ans = ans
print(ans)


from sys import stdin
(n, m) = list(map(int, stdin.readline().split()))
arr = list(map(int, stdin.readline().split()))
total = 0
cur = 1
for i in range(m):
	if cur <= arr[i]:
		pass
	else:
		total += n - cur + 1
		cur = 1
	total += arr[i] - cur
	cur = arr[i]
print(total)


l = list(map(int, input().split()))
n = l[0]
m = l[1]
tasks = list(map(int, input().split()))
count = tasks[0]
if len(tasks) == 1:
	print(tasks[0] - 1)
else:
	for i in range(0, m - 1):
		if tasks[i] < tasks[i + 1]:
			count += tasks[i + 1] - tasks[i]
		elif tasks[i] == tasks[i + 1]:
			count = count
		elif tasks[i] > tasks[i + 1]:
			count = count + n - tasks[i] + tasks[i + 1]
	count = count - 1
	print(count)


import math
from sys import stdin

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
(n, m) = mi()
a = li()
c = 0
for i in range(m - 1):
	if a[i] > a[i + 1]:
		c += 1
print(c * n + a[m - 1] - 1)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
ctr = 0
for i in range(1, m):
	if a[i - 1] > a[i]:
		ctr += 1
print(ctr * n + a[m - 1] - 1)


(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
i = 1
s = a[0] - 1
while i < m:
	if a[i] < a[i - 1]:
		s += n - a[i - 1] + a[i]
	elif a[i] > a[i - 1]:
		s += a[i] - a[i - 1]
	i += 1
print(s)


import sys
import math
from collections import Counter
from collections import OrderedDict
from collections import defaultdict
from functools import reduce
sys.setrecursionlimit(10 ** 6)

def inputt():
	return sys.stdin.readline().strip()

def printt(n):
	sys.stdout.write(str(n) + '\n')

def listt():
	return [int(i) for i in inputt().split()]

def gcd(a, b):
	return math.gcd(a, b)

def lcm(a, b):
	return a * b // gcd(a, b)

def factors(n):
	step = 2 if n % 2 else 1
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(math.sqrt(n)) + 1, step) if n % i == 0)))

def comb(n, k):
	factn = math.factorial(n)
	factk = math.factorial(k)
	fact = math.factorial(n - k)
	ans = factn // (factk * fact)
	return ans

def is_prime(n):
	if n <= 1:
		return False
	if n == 2:
		return True
	if n > 2 and n % 2 == 0:
		return False
	max_div = math.floor(math.sqrt(n))
	for i in range(3, 1 + max_div, 2):
		if n % i == 0:
			return False
	return True

def maxpower(n, x):
	B_max = int(math.log(n, x)) + 1
	return B_max
t = 1
for _ in range(t):
	(n, m) = map(int, inputt().split())
	l = listt()
	time = 0
	a = 1
	for i in l:
		if i >= a:
			time += i - a
			a = i
		elif i < a:
			time += n - a + i
			a = i
	print(time)


def main():
	(n, m) = map(int, input().split())
	s = list(map(int, input().split()))
	s = list(map(lambda x: x - 1, s))
	time = 0
	cur = 0
	for i in s:
		dis = (n + (i - cur)) % n
		time = time + dis
		cur = (cur + dis) % n
	print(time)
main()


(n, m) = list(map(int, input().split()))
li = list(map(int, input().split()))
count = 0
dis = 0
i = 1
li2 = []
while count < m:
	if i == li[count]:
		count += 1
	elif li[count] > i:
		dif = li[count] - i
		dis += dif
		i += dif
	else:
		dif = n - i + li[count]
		dis += dif
		i = li[count]
print(dis)


n = list(map(int, input('').split()))
li = list(map(int, input('').split()))
sum = 0
for i in range(n[1]):
	if i == 0:
		sum = li[i] - 1
	elif li[i] < li[i - 1]:
		sum = sum + (n[0] - li[i - 1] + li[i])
	else:
		sum = sum + (li[i] - li[i - 1])
print('', sum)


(n, t) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
e = t - 2
sum = a[0] - 1
j = 0
while j <= e:
	if a[j] <= a[j + 1]:
		sum = sum + (a[j + 1] - a[j])
	else:
		sum = sum + (n - a[j] + a[j + 1])
	j = j + 1
print(sum)


(n, m) = list(map(int, input().split()))
s = list(map(int, input().split()))
count = 0
c = 1
for x in s:
	if x >= c:
		steps = x - c
	else:
		steps = x - c + n
	c = x
	count += steps
print(count)


(n, m) = map(int, input().split())
listA = list(map(int, input().split()))
i = s = 0
while i < m - 1:
	if listA[i] <= listA[i + 1]:
		i += 1
	else:
		i += 1
		s += n
	if i == m - 1:
		s = s + listA[-1] - 1
print(s)


(n, m) = list(map(int, input().split()))
l = [1]
l.extend(list(map(int, input().split())))
count = 0
i = 1
while i <= m:
	if l[i] >= l[i - 1]:
		count += l[i] - l[i - 1]
	else:
		count += n - l[i - 1] + l[i]
	i += 1
print(count)


(n, m) = map(int, input().split())
L = list(map(int, input().split()))
K = [L[0]]
i = 1
while i < m:
	if L[i] != L[i - 1]:
		K.append(L[i])
	i += 1
ans = K[0] - 1
for i in range(1, len(K)):
	if K[i] > K[i - 1]:
		ans += K[i] - K[i - 1]
	else:
		ans += n - K[i - 1] + K[i]
print(ans)


def read() -> int:
	return int(input())

def read_list() -> list:
	return list(map(int, input().split()))

def print_float(e):
	print('{:.10f}'.format(float(e)))
(n, m) = read_list()
a = read_list()
c = 1
step = 0
for i in a:
	if i == c:
		continue
	elif i > c:
		step += i - c
	elif i < c:
		step += n - c + i
	c = i
print(step)


(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
y = a[0] - 1
for i in range(0, m - 1):
	if a[i] <= a[i + 1]:
		y += a[i + 1] - a[i]
	elif a[i] > a[i + 1]:
		y += n - (a[i] - a[i + 1])
print(y)


import math
houses = int(input().split()[0])
tasks = list(map(int, input().split()))
current = tasks.pop(0)
moves = current - 1
while len(tasks) > 0:
	nextTask = tasks.pop(0)
	if nextTask != current:
		if nextTask < current:
			moves += houses - current + nextTask
			current = nextTask
		else:
			moves += nextTask - current
			current = nextTask
print(moves)


(n, m) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
t = 0
last = 1
for i in range(0, len(l)):
	if l[i] > last:
		t = t + l[i] - last
	elif l[i] < last:
		t = t + n - last + l[i]
	last = l[i]
print(t)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
step = a[0] - 1
for i in range(m - 1):
	if a[i + 1] > a[i]:
		step += a[i + 1] - a[i]
	elif a[i + 1] == a[i]:
		step += 0
	else:
		step += n - a[i] + a[i + 1]
print(step)


def __starting_point():
	inp = input()
	arr = inp.split()
	n = int(arr[0])
	m = int(arr[1])
	inp = input()
	arr = inp.split()
	L = []
	for a in arr:
		L.append(int(a))
	st = 0
	pr = 1
	ans = 0
	bst = True
	while st + 1 < m:
		if L[st] > L[st + 1]:
			if bst:
				ans += L[st] - pr
				bst = False
			else:
				ans += L[st] - pr + n
			pr = L[st]
		st += 1
	if bst:
		ans += L[st] - 1
	else:
		ans += L[st] - pr + n
	print(ans)
__starting_point()


line = input().split()
n = int(line[0])
m = int(line[1])
nums = input().split()
i = 0
sums = int(nums[0])
while i < m - 1:
	if int(nums[i]) <= int(nums[i + 1]):
		sums += int(nums[i + 1]) - int(nums[i])
		i += 1
	else:
		sums += n - int(nums[i]) + int(nums[i + 1])
		i += 1
print(sums - 1)


(n, m) = map(int, input().split())
l = [int(i) for i in input().split()]
prev = 1
ans = 0
for i in l:
	if i >= prev:
		dist = i - prev
	else:
		dist = n - prev + i
	ans += dist
	prev = i
print(ans)


(n, m) = [int(i) for i in input().split()]
line = input().split()
rd = 0
for i in range(len(line) - 1):
	if int(line[i]) > int(line[i + 1]):
		rd = rd + 1
step = rd * n + int(line[len(line) - 1]) - 1
print(step)


ls = input().split(' ')
l = int(ls[0])
n = int(ls[1])
ls = input().split(' ')
res = 0
cnt = 0
for x in range(1, n):
	if int(ls[x - 1]) > int(ls[x]):
		cnt += 1
res = int(ls[n - 1]) - 1 + cnt * l
print(res)


(a, b) = list(map(int, input().split()))
s = list(map(int, input().split()))[:b]
ini = 1
ans = 0
for i in range(b):
	ans += (s[i] - ini + a) % a
	ini = s[i]
print(ans)


line = input().split()
n = int(line[0])
m = int(line[1])
tasks = input().split()
cur = 1
steps = int(tasks[0]) - 1
for i in range(m - 1):
	d = int(tasks[i + 1]) - int(tasks[i])
	if d < 0:
		steps += n
	steps += d
print(steps)


(n, m) = map(int, input().split())
l = [int(x) for x in input().split()]
curr_pos = 1
time = 0
for i in l:
	if i < curr_pos:
		time = time + (n - curr_pos + i)
		curr_pos = i
	elif i == curr_pos:
		continue
	else:
		time += i - curr_pos
		curr_pos = i
print(time)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a = [1] + a
t = 0
for i in range(1, m + 1):
	temp = a[i] - a[i - 1]
	if temp < 0:
		temp += n
	t += temp
print(t)


(n, m) = map(int, input().split())
a = map(int, input().split())
t = 1
r = 0
for i in a:
	r += (i - t) % n
	t = i
print(r)


(n, m) = (int(x) for x in input().split())
place = 1
step = 0
pos = [int(x) for x in input().split()]
for p in pos:
	x = p - place
	if x < 0:
		step += n + x
	else:
		step += x
	place = p
print(step)


p = 1
ans = 0
(n, _) = map(int, input().split())
for el in map(int, input().split()):
	if el >= p:
		ans += el - p
	else:
		ans += n - p + el
	p = el
print(ans)


(n, m) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
p = 1
cnt = 0
for i in range(m):
	if p <= l[i]:
		cnt = cnt + l[i] - p
	else:
		cnt = cnt + (n - (p - l[i]))
	p = l[i]
print(cnt)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))[:m]
time = 0
currentPos = 1
for i in range(len(arr)):
	if arr[i] > currentPos:
		time = time + (arr[i] - currentPos)
		currentPos = arr[i]
	elif arr[i] < currentPos:
		time = time + (n - currentPos) + arr[i]
		currentPos = arr[i]
print(time)


(n, m) = map(int, input().split())
(ans, loc) = (0, 1)
homes = list(map(int, input().split()))
for dest in homes:
	if dest >= loc:
		ans += dest - loc
	else:
		ans += n - loc + dest
	loc = dest
print(ans)


import sys
a = list(map(int, input().split()))
b = list(map(int, input().split()))
k = b[0] - 1
for i in range(1, a[1]):
	if b[i] >= b[i - 1]:
		k = k + b[i] - b[i - 1]
	else:
		k = k + a[0] - b[i - 1] + b[i]
print(k)


(n, m) = [int(x) for x in input().split()]
home = [int(x) for x in input().split()]
count = home[0] - 1
for i in range(m - 1):
	if home[i] > home[i + 1]:
		count += home[i + 1] + n - home[i]
	else:
		count += home[i + 1] - home[i]
print(count)


n = [int(i) for i in input().split(' ')]
m = [int(i) for i in input().split(' ')]
j = 1
c = 0
for i in m:
	if i - j >= 0:
		c += i - j
	else:
		c += i + n[0] - j
	j = i
print(c)


import sys
(n, m) = map(int, sys.stdin.readline().split())
Tasks = map(int, sys.stdin.readline().split())
p = 1
ans = 0
for item in Tasks:
	x = item - p
	if x < 0:
		x = n + x
	ans += x
	p = item
print(ans)


(a, b) = map(int, input().split())
s = input().split()
(k, n) = (0, 1)
for i in range(b):
	if n <= int(s[i]):
		k += int(s[i]) - n
		n = int(s[i])
	else:
		k += a + int(s[i]) - n
		n = int(s[i])
print(k)


line = [int(k) for k in input().split()]
n = line[0]
m = line[1]
tasks = [int(k) for k in input().split()]
numcircle = 0
for i in range(1, m):
	if tasks[i] < tasks[i - 1]:
		numcircle += 1
time = numcircle * n + tasks[m - 1] - 1
print(time)


import sys
from bisect import bisect_left, bisect_right, insort
from collections import deque, Counter
from math import gcd, sqrt, factorial, ceil, log10, log2
from itertools import permutations
from heapq import heappush, heappop, heapify
inf = float('inf')
mod = 1000000007

def factorial_p(n, p):
	ans = 1
	if n <= p // 2:
		for i in range(1, n + 1):
			ans = ans * i % p
	else:
		for i in range(1, p - n):
			ans = ans * i % p
		ans = pow(ans, p - 2, p)
		if n % 2 == 0:
			ans = p - ans
	return ans

def nCr_p(n, r, p):
	ans = 1
	while n != 0 or r != 0:
		(a, b) = (n % p, r % p)
		if a < b:
			return 0
		ans = ans * factorial_p(a, p) * pow(factorial_p(b, p), p - 2, p) * pow(factorial_p(a - b, p), p - 2, p) % p
		n //= p
		r //= p
	return ans

def prime_sieve(n):
	flag = n % 6 == 2
	sieve = bytearray((n // 3 + flag >> 3) + 1)
	for i in range(1, int(n ** 0.5) // 3 + 1):
		if not sieve[i >> 3] >> (i & 7) & 1:
			k = 3 * i + 1 | 1
			for j in range(k * k // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
			for j in range(k * (k - 2 * (i & 1) + 4) // 3, n // 3 + flag, 2 * k):
				sieve[j >> 3] |= 1 << (j & 7)
	return sieve

def prime_list(n):
	res = []
	if n > 1:
		res.append(2)
	if n > 2:
		res.append(3)
	if n > 4:
		sieve = prime_sieve(n + 1)
		res.extend((3 * i + 1 | 1 for i in range(1, (n + 1) // 3 + (n % 6 == 1)) if not sieve[i >> 3] >> (i & 7) & 1))
	return res

def binary(number):
	result = 0
	while number:
		result = result + 1
		number = number & number - 1
	return result

def is_prime(n):
	if n < 5 or n & 1 == 0 or n % 3 == 0:
		return 2 <= n <= 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			p = p * p % n
			if p == n - 1:
				break
		else:
			return False
	return True

def all_factors(n):
	(small, large) = ([], [])
	for i in range(1, int(n ** 0.5) + 1, 2 if n & 1 else 1):
		if not n % i:
			small.append(i)
			large.append(n // i)
	if small[-1] == large[-1]:
		large.pop()
	large.reverse()
	small.extend(large)
	return small

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
(n, m) = get_ints()
Arr = get_array()
total = Arr[0] - 1
last = Arr[0]
for i in range(1, m):
	if Arr[i] >= last:
		total += Arr[i] - last
	else:
		total += n - last + 1 + (Arr[i] - 1)
	last = Arr[i]
print(total)


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
last = 1
ans = 0
for i in A:
	if i >= last:
		ans += i - last
	else:
		ans += N - last + i
	last = i
print(ans)


(n, m) = map(int, input().split())
arr = tuple(map(int, input().split()))
ans = arr[0] - 1
for i in range(1, m):
	if arr[i - 1] > arr[i]:
		ans += n - arr[i - 1] + arr[i]
	else:
		ans += arr[i] - arr[i - 1]
print(ans)


(n, m) = map(int, input().split())
ar = [int(i) for i in input().strip().split()]
s = 0
s = ar[0] - 1
for i in range(0, m - 1):
	if ar[i] > ar[i + 1]:
		s = s + ar[i + 1] + n - ar[i]
	else:
		s = s + ar[i + 1] - ar[i]
print(s)


import sys, getpass
import math, random
import functools, itertools, collections, heapq, bisect
from collections import Counter, defaultdict, deque
input = sys.stdin.readline
M9 = 10 ** 9 + 7
MAXINT = sys.maxsize
OFFLINE_TEST = getpass.getuser() == 'hkmac'

def log(*args):
	if OFFLINE_TEST:
		print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)

def solve(*args):
	if OFFLINE_TEST:
		log('----- solving ------')
		log(*args)
		log('----- ------- ------')
	return solve_(*args)

def read_matrix(rows):
	return [list(map(int, input().split())) for _ in range(rows)]

def read_strings(rows):
	return [input().strip() for _ in range(rows)]

def solve_():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = [a[0]]
	for i in range(m - 1):
		if a[i + 1] < a[i]:
			b.append(b[i] + a[i + 1] - a[i] + n)
		else:
			b.append(b[i] + a[i + 1] - a[i])
	return b[-1] - 1
for case_num in [0]:
	res = solve()
	print(res)


import sys
INT_MAX = sys.maxsize
INT_MIN = -sys.maxsize - 1
sys.setrecursionlimit(10 ** 7)
mod = 1000000007

def IOE():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def main():
	(n, m) = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	a.insert(0, 1)
	ans = 0
	for i in range(m):
		val = a[i + 1] - a[i]
		if val < 0:
			val = n - a[i] + a[i + 1]
		ans += val
	sys.stdout.write(str(ans) + '\n')
try:
	IOE()
except:
	pass
main()


city = input().split()
n = int(city[0])
m = int(city[1])
list1 = input().split()
i = 1
ds = 0
for g in range(0, m):
	if int(list1[g]) >= i:
		ds = ds + int(list1[g]) - i
		i = int(list1[g])
	else:
		ds = ds + (n + (int(list1[g]) - i))
		i = int(list1[g])
print(ds)


(n, m) = map(int, input().split(' '))
lH = list(map(int, input().split(' ')))
i = 0
ans = 0
curH = 1
while i < m:
	if curH == lH[i]:
		ans += 0
	elif curH < lH[i]:
		ans += lH[i] - curH
	else:
		ans += n - curH + lH[i]
	curH = lH[i]
	i += 1
print(ans)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = 0
d = 0
last = None
for i in a:
	if last is None:
		s += i - 1
		last = i
	elif last == i:
		s += 0
	elif last < i:
		s += i - last
		last = i
	else:
		s += n - (last - 1) + i
		last = i
		d += 1
print(s - d)


A = input().split()
n = int(A[0])
m = int(A[1])
B = input().split()
a = int(B[0]) - 1
for i in range(m - 1):
	if int(B[i]) > int(B[i + 1]):
		a += n - int(B[i]) + int(B[i + 1])
	else:
		a += int(B[i + 1]) - int(B[i])
print(a)


(a, b) = [int(i) for i in input().split()]
numbers = [int(i) for i in input().split()]
start = 1
count = 0
for i in range(b):
	if start != numbers[i]:
		if start > numbers[i]:
			count += numbers[i] - start + a
		else:
			count += (numbers[i] - start) % a
		start = numbers[i]
print(count)


(n, m) = map(int, input().split())
num = list(map(int, input().split()))
time = num[0] - 1
for i in range(m - 1):
	if num[i] < num[i + 1]:
		time += num[i + 1] - num[i]
	elif num[i] > num[i + 1]:
		time += n - num[i] + num[i + 1]
	else:
		continue
print(time)


l1 = [int(x) for x in input().split()]
n = l1[0]
m = l1[1]
l2 = [int(x) for x in input().split()]
time = 0
for i in range(0, m):
	if i == 0:
		time += l2[0] - 1
	elif l2[i] < l2[i - 1]:
		time = time + (n - l2[i - 1]) + l2[i]
	else:
		time += l2[i] - l2[i - 1]
print(time)


(n, m) = [int(x) for x in input().split()]
task = [int(x) for x in input().split()]
count = 0
cur = 1
for x in task:
	if cur > x:
		cur = n - cur + x
	elif cur < x:
		cur = x - cur
	else:
		cur = 0
	count += cur
	cur = x
print(count)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
counter = 0
curr = 1
for num in a:
	if curr <= num:
		counter += num - curr
		curr = num
	else:
		counter += n + (num - curr)
		curr = num
print(counter)


def x_to_y(n, x, y):
	if x <= y:
		return y - x
	else:
		return n - (x - y)
inp = input().split()
(n, m) = [int(i) for i in inp]
lst = input().split()
lst = [int(i) for i in lst]
ans = lst[0] - 1
for i in range(m - 1):
	ans += x_to_y(n, lst[i], lst[i + 1])
print(ans)


(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
l = [q for q in range(1, n)]
x = 1
count = 0
for i in range(len(a)):
	if a[i] > x:
		count += a[i] - x
		x = a[i]
	elif a[i] < x:
		count += n - x + a[i]
		x = a[i]
print(count)


(n, m) = map(int, input().split())
arr = [int(i) for i in input().split()]
c = 0
initial = 1
for i in arr:
	if i >= initial:
		c += i - initial
	else:
		c += n - initial + i
	initial = i
print(c)


(n, m) = map(int, input().split())
x = input().split()
y = 0
for i in range(1, m):
	x[i] = int(x[i])
	x[i - 1] = int(x[i - 1])
	if x[i] >= x[i - 1]:
		y = y + x[i] - x[i - 1]
	else:
		y = y + n + x[i] - x[i - 1]
print(y + int(x[0]) - 1)


b = input()
c = input()
(n, m) = b.split()
d = c.split()
d[0:0] = '1'
e = []
for i in range(1, int(m) + 1):
	if int(d[i]) > int(d[i - 1]):
		e.append(int(d[i]) - int(d[i - 1]))
	elif int(d[i]) < int(d[i - 1]):
		e.append(int(n) - int(d[i - 1]) + int(d[i]))
	else:
		pass
print(sum(e))


(n, m) = [int(x) for x in input().split(' ')]
raw = [int(x) for x in input().split(' ')]
pst = 1
cnt = 0
for i in range(m):
	if raw[i] >= pst:
		pst = raw[i]
	else:
		cnt += 1
		pst = raw[i]
road = cnt * n + pst - 1
print(road)


(n, m) = map(int, input().split())
ar = list(map(int, input().split()))
li = 1
su = 0
for i in ar:
	if i >= li:
		su += i - li
	else:
		su += n - (li - i)
	li = i
print(su)


(n, l) = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 1)
s = 0
for i in range(1, l + 1):
	if a[i] >= a[i - 1]:
		s += a[i] - a[i - 1]
	else:
		s += n - a[i - 1] + a[i]
print(s)


(x, y) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
sum = l[0] - 1
for i in range(1, y):
	if l[i] > l[i - 1]:
		sum = sum + l[i] - l[i - 1]
	elif l[i] < l[i - 1]:
		sum = sum + x + l[i] - l[i - 1]
	else:
		continue
print(sum)


(n, m) = list(map(int, input().split()))
k = list(map(int, input().split()))
print([x > y for (x, y) in zip(k[:-1], k[1:])].count(1) * n + k[-1] - 1)


from sys import stdin, stdout
n = [int(x) for x in stdin.readline().split(' ')]
s = [int(x) for x in stdin.readline().split(' ')]
a = n[0] - 1
steps = 0
p = s[0] - 1
for i in range(len(s)):
	if i == 0:
		steps = steps + p
	elif s[i] - 1 > p:
		k = s[i] - 1 - p
		steps = steps + k
		p = s[i] - 1
	elif s[i] - 1 < p:
		k = a - p + (s[i] - 1) + 1
		steps = steps + k
		p = s[i] - 1
stdout.write(str(steps))


(n, m) = list(map(int, input().split()))
houses = list(map(int, input().split()))
times = 0
a1 = 1
for i in range(m):
	a2 = int(houses[i])
	if a2 > a1:
		times = times + a2 - a1
	elif a2 == a1:
		pass
	else:
		times = times + a2 + n - a1
	a1 = a2
print(times)


(n, m) = map(int, input().split())
a = map(int, input().split())
cur = 1
suma = 0
for aa in a:
	if aa < cur:
		aa += n
	steps = aa - cur
	cur += steps
	if cur > n:
		cur -= n
	suma += steps
print(suma)


def d(a, b, m):
	if a == b:
		return 0
	elif a < b:
		return b - a
	else:
		return b + m - a
m = int(input().split()[0])
s = list(map(int, input().split()))
s.insert(0, 1)
c = 0
for i in range(len(s) - 1):
	c += d(s[i], s[i + 1], m)
print(c)


(n, m) = map(int, input().split())
a = [1] + list(map(int, input().split()))
a1 = []
t = 0
for i in a:
	a1.append(i - 1)
for i in range(0, len(a1) - 1, 1):
	if a[i + 1] >= a[i]:
		t += a[i + 1] - a[i]
	else:
		t += n - a[i] + a[i + 1]
print(t)


import re
import math
import copy
(a, b) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
n = 0
for i in range(len(l) - 1):
	if l[i + 1] < l[i]:
		n = n + a
n += l[-1]
print(n - 1)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
tmp = 0
for i in range(m - 1):
	if a[i] > a[i + 1]:
		tmp += 1
print(int(tmp * n + a[m - 1] - 1))


from sys import stdin, stderr

def readInts():
	return map(int, stdin.readline().strip().split())

def print_err(*args, **kwargs):
	print(*args, file=stderr, **kwargs)

def solve(modval, n, vs):
	cur = 0
	steps = 0
	for v in vs:
		v2 = v - 1
		d = (v2 + modval - cur) % modval
		steps += d
		cur = v2
	return steps

def run():
	(n, m) = readInts()
	vs = readInts()
	print(solve(n, m, vs))
run()


(n, m) = map(int, input().split())
a = list(map(int, input().strip().split()))[:m]
count = 0
h = 1
for i in range(0, m):
	if a[i] >= h:
		count += a[i] - h
	else:
		count += n - (h - a[i])
	h = a[i]
print(count)


s = input().split()
h = int(s[0])
a = input().split()
a = [int(item) for item in a]
ans = []
i = 0
while i < len(a):
	if i == len(a) - 1:
		ans.append([a[i]])
	else:
		temp = [a[i]]
		while i < len(a) - 1 and a[i] <= a[i + 1]:
			temp.append(a[i + 1])
			i += 1
		ans.append(temp)
	i += 1
res = (len(ans) - 1) * h + ans[-1][-1] - 1
print(res)


(n, m) = map(int, input().split())
h_no = [1] + list(map(int, input().split()))
total_time = 0
for i in range(0, len(h_no) - 1):
	if h_no[i] < h_no[i + 1]:
		total_time += h_no[i + 1] - h_no[i]
	elif h_no[i] > h_no[i + 1]:
		total_time += n - (h_no[i] - h_no[i + 1])
	else:
		total_time += 0
print(total_time)


a = input().split()
n = int(a[0])
m = int(a[1])
inp = input().split()
oup = int(inp[0]) - 1
for i in range(1, len(inp)):
	if int(inp[i - 1]) < int(inp[i]):
		oup += int(inp[i]) - int(inp[i - 1])
	elif int(inp[i - 1]) > int(inp[i]):
		oup += int(inp[i]) - int(inp[i - 1]) + n
print(oup)


(x, y) = map(int, input().split())
s = list(map(int, input().split()))
c = s[0] - 1
for i in range(1, y):
	if s[i] > s[i - 1]:
		c = c + (s[i] - s[i - 1])
	elif s[i] < s[i - 1]:
		c = c + (x - s[i - 1]) + (s[i] - 1) + 1
print(c)


def calcDistance(start, end):
	if start > end:
		dist = n - start + end
	else:
		dist = end - start
	return dist
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
start = 1
for i in range(m):
	ans = ans + calcDistance(start, a[i])
	start = a[i]
print(ans)


A = [int(i) for i in input().split(' ')]
a = A[0]
b = A[1]
B = [int(i) for i in input().split(' ')]
time = B[0] - 1
i = 1
while i < b:
	if B[i] >= B[i - 1]:
		time += B[i] - B[i - 1]
	else:
		time += a - B[i - 1] + B[i]
	i += 1
print(time)


(n, m) = map(int, input().split())
l = [int(x) for x in input().split()]
k = l[0] - 1
for i in range(1, m):
	if l[i - 1] > l[i]:
		k = k + n - l[i - 1] + l[i]
	else:
		k = k + l[i] - l[i - 1]
print(k)


(n, m) = map(int, input().split())
ans = 0
s = 1
I = map(int, input().split())
for v in I:
	ans += (v - s) % n
	s = v
print(ans)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
x = 0
for nxt in a:
	dlt = (nxt - 1 - x + n) % n
	ans += dlt
	x = nxt - 1
print(ans)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
(house, ans) = (1, 0)
for i in arr:
	if i > house:
		ans += i - house
	elif i < house:
		ans += n - house + i
	house = i
print(ans)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
moves = 0
current_house = 1
i = 0
while i < m:
	if tasks[i] < current_house:
		moves += n
		current_house = tasks[i]
	else:
		current_house = tasks[i]
	i += 1
print(moves + current_house - 1)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
ma = 1
ans = 0
for f in range(m):
	if l[f] == ma:
		continue
	elif ma < l[f]:
		ans += l[f] - ma
		ma = l[f]
	else:
		ans += l[f] + n - ma
		ma = l[f]
print(ans)


(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
currpos = 1
time = 0
for item in l1:
	if item >= currpos:
		time += item - currpos
		currpos = item
	else:
		time += item + n - currpos
		currpos = item
print(time)


(n, k) = map(int, input().split())
t = list(map(int, input().split()))
road = [x for x in range(n)]
pos = 0
r = 0
for i in t:
	if pos <= i - 1:
		r += i - 1 - pos
	else:
		r += i - 1 + (n - pos)
	pos = i - 1
print(r)


(n, m) = map(int, input().split())
num = [int(x) for x in input().split()]
ans = 0
pre = 1
for i in range(0, m):
	ans += (num[i] - pre + n) % n
	pre = num[i]
print(ans)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
st = a[0] - 1
for i in range(1, m):
	if a[i] >= a[i - 1]:
		st = st + (a[i] - a[i - 1])
	else:
		st = st + (n - a[i - 1]) + a[i]
print(st)


(n, m) = map(int, input().split())
house = list(map(int, input().split()))
distance = 0
last_location = 1
for i in house:
	if i < last_location:
		distance += n + i - last_location
		last_location = i
	else:
		distance += i - last_location
		last_location = i
print(distance)


def go():
	(n, m) = [int(i) for i in input().split(' ')]
	a = [int(i) for i in input().split(' ')]
	start = 1
	total = 0
	for i in a:
		if i > start:
			total += i - start
			start = i
		elif i < start:
			total += n - start + i
			start = i
	return total
print(go())


(n, m) = map(int, input().split())
data = [int(i) for i in input().split()]
s = 0
cur = 1
for i in data:
	s += i - cur
	if i < cur:
		s += n
	cur = i
print(s)


total_houses = int(input().split(' ')[0])
current_house = 1
time_taken = 0
for h in list(map(int, input().split(' '))):
	if h < current_house:
		time_taken += abs(total_houses - current_house + h)
	else:
		time_taken += abs(h - current_house)
	current_house = h
print(time_taken)


(n, m) = list(map(int, input().split()))
l = list(map(int, input().split()))
count = 0
current = 1
for i in l:
	if current == n + 1:
		current = 1
	if current <= i:
		count += i - current
	else:
		count += n - current + i
	current = i
print(count)


(n, m) = map(int, input().split())
a = input().split()
l = []
for i in a:
	l += [int(i)]
position = 1
time_ = 0
for i in l:
	if i < position:
		time_ += n - position + i
		position = i
	if i >= position:
		time_ += i - position
		position = i
print(time_)


o = 0
[m, n] = map(int, input().split())
l = [1] + [int(x) for x in input().split()]

def x(a, b, c):
	if b >= a:
		return b - a
	else:
		return b + c - a
for i in range(len(l) - 1):
	o = o + x(l[i], l[i + 1], m)
print(o)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
result = 0
b = 1
for i in range(m):
	result += a[i] - b
	if a[i] - b < 0:
		result += n
	b = a[i]
print(result)


str1 = input()
str2 = input()
L1 = str1.split(' ')
L2 = str2.split(' ')
steps = 0
pos = 1
for number in L2:
	steps += int(number) - pos
	if int(number) < pos:
		steps += int(L1[0])
	pos = int(number)
print(steps)


(n, k) = map(int, input().split())
li = list(map(int, input().split()))
s = li[0] - 1
i = 0
while i < k - 1:
	if li[i] > li[i + 1]:
		s += li[i + 1] + n - li[i]
	elif li[i] < li[i + 1]:
		s += li[i + 1] - li[i]
	i += 1
print(s)


def Sol(n, m, arr):
	cur = 1
	mov = 0
	for i in arr:
		if cur <= i:
			mov += i - cur
			cur = i
		else:
			mov += n - cur + i
			cur = i
	print(mov)
	return
(n, k) = map(int, input().split())
arr = list(map(int, input().split()))
Sol(n, k, arr)


(n, m) = map(int, input().split())
houses = list(map(int, input().split()))
time = 0
temp = 1
for i in range(m):
	if houses[i] >= temp:
		x = houses[i] - temp
		time += x
		temp = houses[i]
	else:
		x = n - temp + houses[i]
		time += x
		temp = houses[i]
print(time)


a = list(map(int, input().split()))
x = list(map(int, input().split()))
count = 0
prev_house = 1
for i in range(a[1]):
	if prev_house <= x[i]:
		count += x[i] - prev_house
	else:
		count += a[0] - prev_house + x[i]
	prev_house = x[i]
print(count)


(n, m) = map(int, input().split())
a = map(int, input().split())
(cnt, solve) = (1, 0)
for i in a:
	if i < cnt:
		solve += n + i - cnt
	else:
		solve += i - cnt
	cnt = i
print(solve)


x = [int(i) for i in input().split(' ')]
m = x[0]
n = x[1]
task = [int(i) for i in input().split(' ')]
sum = task[0] - 1
for i in range(1, n):
	if task[i] < task[i - 1]:
		sum += m - abs(task[i] - task[i - 1])
	else:
		sum += abs(task[i] - task[i - 1])
print(sum)


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
indexes = [j for j in range(m - 1) if a[j + 1] < a[j]]
if len(indexes) > 0:
	print(len(indexes) * n + max(a[indexes[-1] + 1:]) - 1)
else:
	print(a[-1] - 1)


(n, m) = map(int, input().split())
b = [int(s) for s in input().split()]
s = abs(1 - b[0])
for i in range(1, len(b)):
	if b[i] - b[i - 1] >= 0:
		s += b[i] - b[i - 1]
	else:
		s += n + b[i] - b[i - 1]
print(s)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
q = 0
p = 1
for i in range(len(l)):
	if p <= l[i]:
		q += l[i] - p
		p = l[i]
	else:
		q += n - p + l[i]
		p = l[i]
print(q)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
s = l[m - 1] - 1
for i in range(m - 2, -1, -1):
	if l[i] <= l[i + 1]:
		continue
	else:
		s += n
print(s)


def get_numlist():
	list_in = list(input().split())
	list_out = []
	for k in list_in:
		list_out += [int(k)]
	return list_out

def main():
	list0 = get_numlist()
	a = list0[0]
	b = list0[1]
	list1 = get_numlist()
	check = 0
	mid = list1[0]
	for k in range(1, b):
		if mid > list1[k]:
			mid = list1[k]
			check += 1
		else:
			mid = list1[k]
	print(check * a + list1[-1] - 1)
main()


(n, m) = input().split()
n = int(n)
m = int(m)
ans = 0
j = 1
listt = input().split()
for i in range(m):
	if int(listt[i]) > j:
		ans += int(listt[i]) - j
		j = int(listt[i])
	elif int(listt[i]) == j:
		continue
	else:
		ans += n - j + int(listt[i])
		j = int(listt[i])
print(ans)


(n, m) = list(map(int, input().split()))
housenums = list(map(int, input().split()))
count = -1
house = 1
i = 0
while i < len(housenums):
	if housenums[i] <= n and housenums[i] >= house:
		if i >= 1:
			count += housenums[i] - housenums[i - 1]
		else:
			count += housenums[i]
		house = housenums[i]
	else:
		count += n - housenums[i - 1] + housenums[i]
		house = housenums[i]
	i += 1
print(count)


(n, m) = input().split()
n = int(n)
m = int(m)
points = input().split()
step = int(points[0]) - 1
for i in range(m - 1):
	if int(points[i + 1]) >= int(points[i]):
		step = step + int(points[i + 1]) - int(points[i])
	else:
		step = step + int(points[i + 1]) - int(points[i]) + n
print(step)


(t, n) = map(int, input('').split(' '))
l = list(map(int, input('').split(' ')))
v = 0
p = 1
for i in l:
	if p < i:
		v += i - p
		p = i
	elif p > i:
		v += t - p + i
		p = i
	else:
		pass
print(v)


(n, m) = map(int, input().split(' '))
p = input().split(' ')
problem = []
for i in p:
	problem.append(int(i))
path = problem[0] - 1
for i in range(1, m):
	if problem[i] > problem[i - 1]:
		path += problem[i] - problem[i - 1]
	elif problem[i] < problem[i - 1]:
		path += n - problem[i - 1] + problem[i]
print(path)


(n, m) = map(int, input().split())
time = 0
cur_house = 1
a = [int(x) for x in input().split()]
for i in range(m):
	time += a[i] - cur_house if a[i] >= cur_house else n - (cur_house - a[i])
	cur_house = a[i]
print(time)


(n, m) = input().split()
m = int(m)
n = int(n)
lista = input().split()
p = 1
count = 0
for i in lista:
	cha = (int(i) - p) % n
	p = int(i)
	count += cha
print(count)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
a = 1
t = 0
for i in l:
	if i >= a:
		t += i - a
	else:
		t += n - a + i
	a = i
print(t)


(n, m) = [int(i) for i in input().split()]
task = [int(i) for i in input().split()]
result = task[0] - 1
a = 0
for i in task[1:]:
	if i >= task[a]:
		result += i - task[a]
	else:
		result += n - (task[a] - i)
	a += 1
print(result)


(n, m) = map(int, input().split())
prev = t = 0
tareas = list(map(int, input().split()))
prev = tareas[0]
t = tareas[0] - 1
for i in range(1, m):
	if tareas[i] >= prev:
		t += tareas[i] - prev
	else:
		t += n - prev + tareas[i]
	prev = tareas[i]
print(t)


(a, c) = map(int, input().split())
b = list(map(int, input().split()))
b.insert(0, 1)
e = 0
for i in range(1, len(b)):
	if b[i] > b[i - 1]:
		e += b[i] - b[i - 1]
	elif b[i] == b[i - 1]:
		pass
	else:
		e += b[i] - 1
		e += a - b[i - 1]
		e += 1
print(e)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = 1
count = 0
n = a[0]
for k in b:
	temp = 0
	if i < k:
		count += k - i
		i = k
	elif i > k:
		temp = n - i + 1
		temp = temp + (k - 1)
		count += temp
		i = k
	else:
		i = k
print(count)


(n, m) = list(map(int, input().split()))
lst = list(map(int, input().split()))
count = 0
start = 1
end = n
for i in range(m):
	house_to_reach = lst[i]
	if house_to_reach < start:
		count += end - start + 1
		start = 1
		count += house_to_reach - start
		start = house_to_reach
	else:
		count += house_to_reach - start
		start = house_to_reach
print(count)


(n, m) = map(int, input().split(' '))
arr = list(map(int, input().split(' ')))
time = 0
prev = 1
for i in arr:
	if i >= prev:
		time = time + (i - prev)
	else:
		time = time + (n - prev) + i
	prev = i
print(time)


(n, x) = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
i = 1
c = 0
for _ in A:
	if _ >= i:
		c = c + _ - i
		i = _
	else:
		c = c + n - i + _
		i = _
print(c)


import sys
import math

def main():
	(n, m) = list(map(int, sys.stdin.readline().strip().split(' ')))
	a = list(map(int, sys.stdin.readline().strip().split(' ')))
	cnt = 0
	cur_pos = 0
	for i in range(m):
		next_pos = a[i] - 1
		cnt += (next_pos - cur_pos) % n
		cur_pos = next_pos
	sys.stdout.write(str(cnt))
main()


(m, s) = list(map(int, input().split()))
l = list(map(int, input().split()))
res = l[0] - 1
for i in range(1, s):
	if l[i] < l[i - 1]:
		res += m - l[i - 1] + (l[i] - 1) + 1
	else:
		res += l[i] - l[i - 1]
print(res)


num1 = list(map(int, input().strip().split()))
num2 = list(map(int, input().strip().split()))
count = 0
temp = 1
for i in num2:
	x = i - temp
	if x >= 0:
		count += x
	else:
		count += num1[0] - temp + i
	temp = i
print(count)


from sys import stdin, stdout
from math import ceil, floor, sqrt
from collections import deque, Counter
inp = stdin.readline
out = stdout.write
(n, m) = map(int, inp().strip().split())
a = list(map(int, inp().strip().split()))
t = a[0] - 1
for i in range(1, m):
	if a[i] < a[i - 1]:
		t += a[i] - 1
		t += n - a[i - 1]
		t += 1
	else:
		t += a[i] - a[i - 1]
out(str(t))


(n, m) = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
c = 1
t = 0
for d in a:
	t += (n + d - c) % n
	c = d
print(t)


(n, m) = map(int, input().split())
deals = [int(x) for x in input().split()]
count = deals[0] - 1
for i in range(1, m):
	if deals[i] < deals[i - 1]:
		count += n - (deals[i - 1] - deals[i])
	else:
		count += deals[i] - deals[i - 1]
print(count)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
count = 0
prev = l[0]
count += prev - 1
for i in range(1, m):
	if l[i] > prev:
		count += l[i] - prev
		prev = l[i]
	elif l[i] == prev:
		pass
	else:
		count += n - prev + l[i]
		prev = l[i]
print(count)


def read_ints():
	return list(map(int, input().split()))
(n, _) = read_ints()
tasks = read_ints()
accumulator = tasks[0] - 1
for index in range(1, len(tasks)):
	if tasks[index - 1] > tasks[index]:
		accumulator += tasks[index] + (n - tasks[index - 1])
	else:
		accumulator += tasks[index] - tasks[index - 1]
print(accumulator)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
b = 1
c = l[0] - 1
for i in range(1, len(l)):
	if l[i - 1] < l[i]:
		c += l[i] - l[i - 1]
	elif l[i - 1] > l[i]:
		c += n - l[i - 1] + 1
		c += l[i] - 1
	else:
		pass
print(c)


line = input().split()
task = input().split()
num_house = int(line[0])
num_task = int(line[1])
position = 1
step = 0
for i in range(num_task):
	if int(task[i]) < position:
		step += num_house + int(task[i]) - position
		position = int(task[i])
	else:
		step += int(task[i]) - position
		position = int(task[i])
print(step)


(a, b) = map(int, input().split())
c = list(map(int, input().split()))
k = int(0)
x = int(1)
while x < b:
	if c[x] - c[x - 1] >= 0:
		k = k + c[x] - c[x - 1]
	else:
		k = k + a - abs(c[x] - c[x - 1])
	x = x + 1
print(k + c[0] - 1)


import sys
inf = float('inf')
(mod, MOD) = (1000000007, 998244353)

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
(n, m) = get_ints()
Arr = get_array()
last = 1
ans = 0
for i in range(m):
	if Arr[i] == last:
		continue
	elif Arr[i] > last:
		ans += Arr[i] - last
		last = Arr[i]
	else:
		ans += n - last + Arr[i]
		last = Arr[i]
print(ans)


s = str(input()).split()
n = int(s[0])
m = int(s[1])
tot = 0
s = str(input()).split()
for i in range(-1, m - 1):
	if i == -1:
		x = 1
		y = int(s[0])
	else:
		x = int(s[i])
		y = int(s[i + 1])
	if y < x:
		tot += n - x + y
	else:
		tot += y - x
print(tot)


(houses, tasks) = [int(x) for x in input().split()]
all_tasks = [int(x) for x in input().split()]
times = 0
start = 1
for i in range(len(all_tasks)):
	times += (all_tasks[i] - start + houses) % houses
	start = all_tasks[i]
print(times)


(a, b) = [int(x) for x in input().split()]
c = [int(i) for i in input().split(' ')]
i = 0
total = 0
while i < len(c) - 1:
	if c[i] <= c[i + 1]:
		total += c[i + 1] - c[i]
		i += 1
	else:
		total += a - c[i] + c[i + 1]
		i += 1
print(total + c[0] - 1)


import sys
(n, m) = map(int, sys.stdin.readline().split())
alist = list(map(int, sys.stdin.readline().split()))
count = 0
for i in range(m - 1):
	if alist[i] > alist[i + 1]:
		count += 1
print(count * n + alist[-1] - 1)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
hosss = 1
besss = 0
for i in a:
	if i >= hosss:
		besss = besss + (i - hosss)
	else:
		besss = besss + (n - hosss + i)
	hosss = i
print(besss)


(n, m) = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
count = 0
k = 1
for i in range(0, m):
	if a[i] >= k:
		count = count + (a[i] - k)
		k = a[i]
	else:
		count = count + (n - k + a[i])
		k = a[i]
print(count)


(n, m) = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = A[0] - 1
cur = A[0]
for i in range(1, m):
	if A[i] < cur:
		ans += A[i] + n - cur
		cur = A[i]
	else:
		ans += A[i] - cur
		cur = A[i]
print(ans)


(a, b) = map(int, input().split())
c = list(map(int, input().split()))
i = 0
t = 0
while i < b:
	while i < b - 1 and c[i + 1] >= c[i]:
		del c[i]
		b = len(c)
	i += 1
	t += a
t = t - (a - c[-1])
print(t - 1)


nm = input().split()
n = int(nm[0])
m = int(nm[1])
house = list(map(int, input().split()))
present = 1
sol = 0
for next in house:
	if next >= present:
		sol += next - present
	else:
		sol += n - (present - next)
	present = next
print(sol)


(N, M) = map(int, input().split())
S = input().split()
s = [int(i) for i in S]
r = s[0] - 1
for i in range(len(s) - 1):
	if s[i] <= s[i + 1]:
		r += s[i + 1] - s[i]
	elif s[i] > s[i + 1]:
		r += N - s[i] + s[i + 1]
print(r)


(a, b) = map(int, input().split())
z = list(map(int, input().split()))
c = z[0] - 1
for i in range(1, b):
	if z[i] - z[i - 1] >= 0:
		c += z[i] - z[i - 1]
	else:
		c += a - abs(z[i] - z[i - 1])
print(c)


(n, m) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
round_ = 0
last = -1
for ai in a:
	if last > ai:
		round_ += 1
	last = ai
print(round_ * n + a[-1] - 1)


(m, n) = [int(j) for j in input().split()]
lst = list(map(int, input().strip().split()))[:n]
count = 0
lst.insert(0, 1)
for i in range(0, len(lst) - 1):
	if lst[i + 1] >= lst[i]:
		count = count + lst[i + 1] - lst[i]
	else:
		count = count + (m - lst[i] + lst[i + 1] + 1) - 1
print(count)


args1 = input().split()
n = int(args1[0])
m = int(args1[1])
args2 = input().split()
position = 1
time = 0
for i in args2:
	if int(i) > position:
		time += int(i) - position
		position = int(i)
	elif int(i) == position:
		time += 0
		position = int(i)
	else:
		time += n - position + int(i)
		position = int(i)
print(time)


(n, m) = input().split()
b = input().split()
a = []
for c in b:
	c = int(c)
	a.append(c)
if len(a) == 1:
	print(a[0] - 1)
else:
	steps = a[0] - 1
	index = 0
	for i in range(1, len(a) - 1):
		if a[index] > a[index + 1]:
			steps += int(n) - a[index]
			steps += a[index + 1]
		elif a[index] < a[index + 1]:
			steps += a[index + 1] - a[index]
		elif a[index] == a[index + 1]:
			pass
		index += 1
	if a[-1] > a[-2]:
		steps += a[-1] - a[-2]
	elif a[-1] < a[-2]:
		steps += int(n) - a[-2]
		steps += a[-1]
	print(steps)


def main():
	(n, m) = map(int, input().split())
	house = list(map(int, input().split()))
	curr_house = 1
	cost = 0
	for i in house:
		if curr_house <= i:
			cost += i - curr_house
		else:
			cost += n - curr_house + i
		curr_house = i
	print(cost)
main()


row = list(map(int, input().split()))
(n, m) = (row[0], row[1])
tasks = list(map(int, input().split()))
count = 0
curr = 1
for i in tasks:
	if curr > i:
		count += n - curr + 1
		curr = 1
	count += i - curr
	curr = i
print(count)


inp = input().split()
[num, ll] = [int(inp[0]), int(inp[1])]
tas = input().split()
hou = 1
ans = 0
for x in tas:
	x = int(x)
	if x > hou:
		ans += x - hou
	elif x < hou:
		ans += num - hou + x
	hou = x
print(ans)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
c = 0
x = 1
for i in a:
	c += (i - x) % n
	x = i
print(c)


(n, m) = map(int, input().split())
a = 1
b = 0
for i in map(int, input().split()):
	b += (i - a) % n
	a = i
print(b)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
j = 1
d = 0
for i in a:
	if i >= j:
		d = d + i - j
		j = i
	else:
		d = d + n + i - j
		j = i
print(d)


def array():
	return list(map(int, input().strip().split()))

def space_str():
	return input().strip().split()

def space_int():
	return map(int, input().strip().split())
(n, m) = space_int()
arr = array()
brr = [1] + arr
ans = 0
for i in range(m):
	if brr[i] <= brr[i + 1]:
		ans += brr[i + 1] - brr[i]
	else:
		ans += n - brr[i] + brr[i + 1]
print(ans)


(n, m) = map(int, input().split())
station = []
station = input().split()
m = len(station)
m1 = 0
summary = int(station[0]) - 1
while m1 < m - 1:
	num1 = int(station[m1])
	num2 = int(station[m1 + 1])
	if num1 < num2:
		summary += num2 - num1
	elif num1 == num2:
		pass
	else:
		summary += n + num2 - num1
	m1 += 1
print(summary)


(n, m) = map(int, input().split())
houses = [int(i) for i in input().split()]
result = 0
for i in range(m - 1):
	move = houses[i + 1] - houses[i]
	if move >= 0:
		result += move
	else:
		result += n + move
result += houses[0] - 1
print(result)


def TotalCaminos(n, A):
	count = A[0] - 1
	for k in range(len(A) - 1):
		count += n - A[k] + A[k + 1]
	return count

def Sin_Menores(A):
	SinMen = []
	for k in range(len(A) - 1):
		if A[k] > A[k + 1]:
			SinMen.append(A[k])
	SinMen.append(A[len(A) - 1])
	return SinMen

def Sin_Repetidos(n, A):
	var = A[0]
	SinRep = [A[0]]
	for k in range(1, n):
		if var != A[k]:
			SinRep.append(A[k])
			var = A[k]
	return SinRep
L = input()
L = L.split()
n = int(L[0])
m = int(L[1])
A = input()
A = A.split()
for k in range(m):
	A[k] = int(A[k])
SR = Sin_Repetidos(m, A)
SM = Sin_Menores(SR)
Total = TotalCaminos(n, SM)
print(Total)


(n, m) = [int(i) for i in input().split()]
a = []
a = [int(i) for i in input().split()]
p = 1
s = 0
for v in a:
	s = s + (v - p) % n
	p = v
print(s)


(a, b) = map(int, input().split())
(m, n, k) = (0, 0, 1)
lst = list(map(int, input().split()))
for (i, j) in enumerate(lst):
	m = m + (j - k)
	if j - k < 0:
		m = m + a
	k = j
print(m)


(n, m) = input().split()
m = int(m)
n = int(n)
b = [int(x) for x in input().split()]
t = b[0] - 1
for i in range(1, len(b)):
	t += (b[i] - b[i - 1]) % n
print(t)


def scan(type):
	return list(map(type, input().split()))
(n, m) = scan(int)
arr = scan(int)
ans = 0
last = 1
for x in arr:
	if x > last:
		ans += abs(x - last)
	elif x < last:
		ans += n - last
		ans += x
	last = x
print(ans)


(n, m) = list(map(int, input().split()))
order = list(map(int, input().split()))
time = 0
j = 1
while j < m:
	if order[j] == order[j - 1]:
		order[j - 1:j] = []
		m -= 1
	j += 1
len_order = len(order)
j = 0
pre_location = 1
while j < len_order:
	distance = order[j] - pre_location
	if distance >= 0:
		time += distance
	else:
		time += n + distance
	pre_location = order[j]
	j += 1
print(time)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
ans = arr[0] - 1
prev = arr[0]
for i in range(1, m):
	if prev <= arr[i]:
		ans += arr[i] - prev
	else:
		k = n - prev
		k += arr[i]
		ans += k
	prev = arr[i]
print(ans)


(n, k) = [int(x) for x in input().split(' ')]
arr = [int(x) for x in input().split(' ')]
ans = 0
prev = 1
for item in arr:
	if item >= prev:
		ans += item - prev
	else:
		ans += n - prev + item
	prev = item
print(ans)


(n, m) = map(int, input().split())
task = list(map(int, input().split()))
location = 1
t = 0
for i in range(m):
	if task[i] > location:
		t += task[i] - location
		location = task[i]
	elif task[i] < location:
		t += n - (location - task[i])
		location = task[i]
	else:
		continue
print(t)


def time(arr, n, m):
	count = 0
	for x in range(m):
		if x == 0:
			count = count + arr[x]
		elif arr[x - 1] <= arr[x]:
			count = count + arr[x] - arr[x - 1]
		else:
			count = count + n - arr[x - 1] + arr[x]
	return count
(n, m) = map(int, input().split())
num = list(map(int, input().split()))
tim = time(num, n, m)
print(tim - 1)


(n, m) = map(int, input().split())
ls = [1] + list(map(int, input().split()))
t = 0
for e in range(m):
	if ls[e] < ls[e + 1]:
		t += abs(ls[e] - ls[e + 1])
	if ls[e] > ls[e + 1]:
		t += n - abs(ls[e] - ls[e + 1])
print(t)


(n, m) = map(int, input().split())
s = map(int, input().split())
f = [1]
f = f + list(s)
d = 0
for i in range(m):
	if f[i] > f[i + 1]:
		d += n + f[i + 1] - f[i]
	elif f[i] < f[i + 1]:
		d += f[i + 1] - f[i]
print(d)


(N, M) = [int(x) for x in input().split()]
X = [int(x) for x in input().split()]
pos = 1
count = 0
for x in X:
	pos_new = x
	while x < pos:
		x += N
	count += x - pos
	pos = pos_new
print(count)


import sys, os
import math
from fractions import Fraction
from collections import defaultdict
from random import randint
from itertools import combinations, permutations

def linput():
	return list(minput())

def minput():
	return map(int, sys.stdin.readline().strip().split())
(n, m) = minput()
a = linput()
ans = 0
pos = 1
for j in range(m):
	if pos <= a[j]:
		ans += a[j] - pos
	else:
		ans += n - pos + a[j]
	pos = a[j]
print(ans)


line1 = list(map(int, input().split()))
line2 = list(map(int, input().split()))
(n, m) = (line1[0], line1[1])
time_step = 0
for i in range(0, line1[1] - 1):
	if line2[i] > line2[i + 1]:
		time_step += n - line2[i] + line2[i + 1]
	elif line2[i] < line2[i + 1]:
		if line2[i + 1] - line2[i] > 0:
			time_step += line2[i + 1] - line2[i]
	else:
		pass
if line2[0] > 1:
	time_step += line2[0] - 1
print(time_step)


str = input().split()
n = int(str[0])
m = int(str[1])
l = [int(x) for x in input().split()]
s = 0
for i in range(m - 1):
	if l[i] > l[i + 1]:
		s += 1
print(l[-1] - 1 + n * s)


(n, m) = [int(i) for i in input().split()]
s = input().split()
t = int(s[0]) - 1
for i in range(len(s) - 1):
	if int(s[i]) <= int(s[i + 1]):
		t += int(s[i + 1]) - int(s[i])
	else:
		t += int(s[i + 1]) - int(s[i]) + n
print(t)


(a, b) = map(int, input().split())
s = 1
num = 0
c = list(map(int, input().split()))
for i in range(b):
	if c[i] >= s:
		num += c[i] - s
	else:
		num += a + c[i] - s
	s = c[i]
print(num)


(n, m) = map(int, input().split(' '))
h = list(map(int, input().split(' ')))
house = 1
step = 0
for i in range(len(h)):
	if h[i] >= house:
		step += h[i] - house
	else:
		step += h[i] + n - house
	house = h[i]
print(str(step))


def main():
	x = input().split()
	n = int(x[0])
	m = int(x[1])
	lasthome = 1
	time = 0
	task = input().split()
	for i in range(m):
		if int(task[i]) > lasthome:
			time += int(task[i]) - lasthome
			lasthome = int(task[i])
		elif int(task[i]) == lasthome:
			pass
		else:
			time += n - (lasthome - int(task[i]))
			lasthome = int(task[i])
	print(time)
main()


(n, m) = [int(i) for i in input().split()]
aList = [int(i) for i in input().split()]
last = 0
time = 0
for a in aList:
	if a < last:
		time += n
	last = a
time += aList[-1] - 1
print(time)


(n, m) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [i for i in A]
B.sort()
if B == A:
	print(A[m - 1] - 1)
else:
	s = 0
	for i in range(m - 1):
		if A[i] <= A[i + 1]:
			continue
		else:
			s += 1
	print(s * n + A[m - 1] - 1)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
s = l[0]
temp = l[0]
for t in range(1, m):
	if l[t] < temp:
		s = s + l[t] + n - temp
		temp = l[t]
	else:
		s = s + l[t] - temp
		temp = l[t]
print(s - 1)


nm = input().split(' ')
n = int(nm[0])
m = int(nm[1])
ln = [int(a) for a in input().split(' ')]
ans = ln[0] - 1
for i in range(m - 1):
	if ln[i + 1] > ln[i]:
		ans += ln[i + 1] - ln[i]
	elif ln[i + 1] < ln[i]:
		ans += ln[i + 1] + n - ln[i]
print(ans)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
if m == 1:
	print(int(a[0]) - 1)
	quit()
counter = 0
for i in range(m - 1):
	if a[i] > a[i + 1]:
		counter += 1
print(counter * n + (a[-1] - 1))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
tour = 0
cur = 1
for i in a:
	if cur < i:
		tour += i - cur
		cur = i
	elif cur > i:
		tour += n - cur + i
		cur = i
print(tour)


a = map(int, input().split())
b = list(a)
c = b[0]
d = b[1]
e = map(int, input().split())
f = list(e)
s = f[d - 1] - 1
for i in range(1, d):
	if f[i] < f[i - 1]:
		s += c
print(s)


def solve(n, tasks):
	(cur, count) = (1, 0)
	for i in tasks:
		count += (i - cur) % n
		cur = i
	return count
parse = lambda st: map(int, st.split())
(n, m) = parse(input())
res = solve(n, parse(input()))
print(res)


a = [int(i) for i in input().split(' ')]
n = a[0]
m = a[1]
a = [int(i) for i in input().split(' ')]
r = a[0] - 1
for i in range(len(a) - 1):
	if a[i] < a[i + 1]:
		r += a[i + 1] - a[i]
	elif a[i + 1] < a[i]:
		r += a[i + 1] + n - a[i]
print(r)


nm = list(map(int, input().strip().split()))
n = nm[0]
m = nm[1]
tasks = list(map(int, input().strip().split()))
i = 1
time = 0
for j in range(m):
	task = tasks[j]
	if task >= i:
		time += task - i
	else:
		time += task - i + n
	i = task
print(time)


(n, m) = map(int, input().split())
l = [int(i) for i in input().split()]
s = 0
d = 1
for i in l:
	if d > i:
		s = s + (n - d + 1)
		d = 1
	s += i - d
	d = i
print(s)


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split())

def li():
	return list(mi())
main()
(n, m) = mi()
l = li()
curr = 1
res = 0
for i in l:
	if i >= curr:
		res += i - curr
	else:
		res += n - (curr - i)
	curr = i
print(res)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
ini = 0
c = 0
e = l[m - 1] - 1
for i in range(m):
	diff = l[i] - ini
	ini = l[i]
	if diff < 0:
		c += 1
ld = c * n
print(ld + e)


(n, m) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
time = 0
prev = 1
for i in arr:
	if i > prev:
		time += i - prev
	elif i < prev:
		time += n - prev + i
	prev = i
print(time)


(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
num = a[0] - 1
i = 1
while i < m:
	if a[i] < a[i - 1]:
		num += a[i] + n - a[i - 1]
	else:
		num += a[i] - a[i - 1]
	i += 1
print(num)


(n, m) = input().split()
n = int(n)
m = int(m)
tasks = input().split()
house = 1
time = 0
for i in range(m):
	tasks[i] = int(tasks[i])
for i in range(m):
	if tasks[i] >= house:
		time += tasks[i] - house
	else:
		time += n + tasks[i] - house
	house = tasks[i]
print(time)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
ma = max(l)
count = l[0] - 1
for i in range(m - 1):
	if l[i + 1] > l[i]:
		count += l[i + 1] - l[i]
	elif l[i + 1] == l[i]:
		count += 0
	else:
		count += n - l[i] + l[i + 1]
print(count)


(n, m) = map(int, input().split())
tasks = map(int, input().split())
cur = 1
total = 0
for task in tasks:
	if task > cur:
		total += task - cur
	elif task < cur:
		total += n + 1 - cur + (task - 1)
	cur = task
print(total)


3
(n, m) = tuple(map(int, input().split()))
a = [1] + list(map(int, input().split()))
ans = 0
for (i, j) in zip(a[:-1], a[1:]):
	ans += j - i if j >= i else n - i + j
print(ans)


(a, b) = map(int, input().split())
c = list(map(int, input().split()))
d = 1
e = 0
for i in c:
	if d < i:
		e += i - d
		d = i
	elif d > i:
		e += i
		e += a - d
		d = i
print(e)


(n, m) = map(int, input().split())
ar = list(map(int, input().split()))
arr = [ar[0]]
for i in range(1, len(ar)):
	if ar[i] != ar[i - 1]:
		arr.append(ar[i])
i = 0
ans = arr[0] - 1
p = arr[0]
for i in range(1, len(arr)):
	if arr[i] >= p:
		ans += arr[i] - p
		p = arr[i]
	else:
		ans += n - p + arr[i]
		p = arr[i]
print(ans)


(n, m) = input().split()
n = int(n)
m = int(m)
x = 1
cn = 0
a = list(map(int, input().split()))
for i in range(0, m):
	if a[i] > x:
		cn += a[i] - x
		x = a[i]
	if a[i] < x:
		cn += n - x + a[i]
		x = a[i]
print(cn)


for _ in range(1):
	(n, m) = map(int, input().split())
	arr = list(map(int, input().split()))
	curr = 1
	total = 0
	for i in range(m):
		if curr > arr[i]:
			total += n - curr + arr[i]
			curr = arr[i]
		else:
			total += arr[i] - curr
			curr = arr[i]
	print(total)


import math, itertools, fractions, heapq, collections, bisect, sys, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	(n, m) = LI()
	l = LI()
	st = 1
	ans = 0
	for x in l:
		if st == x:
			continue
		elif st < x:
			ans += x - st
			st = x
		else:
			ans += n - st + x
			st = x
	return ans
print(main())


(h, n) = input().split()
h = int(h)
n = int(n)
a = [int(n) for n in input().split()]
t = a[0]
for i in range(n - 1):
	if a[i] <= a[i + 1]:
		t += a[i + 1] - a[i]
	else:
		t += h + a[i + 1] - a[i]
print(t - 1)


a1 = input().split()
n = int(a1[0])
ta = int(a1[1])
a2 = input().split()
l = []
for i in range(ta):
	l.append(int(a2[i]))
sum = l[0] - 1
for i in range(1, ta):
	if l[i] == l[i - 1]:
		sum += 0
	elif l[i] > l[i - 1]:
		sum += l[i] - l[i - 1]
	else:
		sum += l[i] - l[i - 1] + n
print(sum)


from sys import stdin, stdout
input = stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
ans = a[0] - 1
pnt = a[0]
for i in range(1, m):
	if a[i] < pnt:
		ans += n - pnt + a[i]
		pnt = a[i]
	else:
		ans += a[i] - pnt
		pnt = a[i]
stdout.write(str(ans))


(n, m) = map(int, input().split())
M = [int(i) for i in input().split()]
icount = 0
for i in range(m - 1):
	if M[i] > M[i + 1]:
		icount += n - abs(M[i] - M[i + 1])
	elif M[i] == M[i + 1]:
		icount += 0
	else:
		icount += abs(M[i] - M[i + 1])
icount += M[0] - 1
print(icount)


(n, m) = map(int, input().split())
f = list(map(int, input().split()))
time = 0
p = 1
for i in range(m):
	if f[i] >= p:
		time += f[i] - p
	else:
		time += n - p + f[i]
	p = f[i]
print(time)


(n, m) = map(int, input().split())
lt = list(map(int, input().split()))
l = 0
p = 1
for i in range(m):
	l += (lt[i] - p) % n
	p = lt[i]
print(l)


(n, m) = input().split(' ')
m_l = list(map(int, input().split(' ')))
prev = 0
s = 0
for i in range(int(m)):
	if i == 0:
		prev = m_l[0]
		s = m_l[0]
	else:
		if prev > m_l[i]:
			s += int(n) - prev + m_l[i]
		else:
			s += m_l[i] - prev
		prev = m_l[i]
print(s - 1)


(n, m) = [int(i) for i in input().split()]
t = 0
b = 1
a = [int(i) for i in input().split()]
for p in a:
	if p >= b:
		t = t + p - b
		b = p
	else:
		t = t + (p + n) - b
		b = p
print(t)


from sys import stdin
(n, m) = map(int, stdin.readline().split())
houses = [int(x) for x in stdin.readline().split()]
ans = houses[0] - 1
for x in range(len(houses) - 1):
	if houses[x] == houses[x + 1]:
		continue
	if houses[x] < houses[x + 1]:
		ans += houses[x + 1] - houses[x]
	else:
		ans += n - (houses[x] - houses[x + 1])
print(ans)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
i = 0
now = 1
all = 0
while i != m:
	if a[i] - now > 0:
		all += a[i] - now
		now = a[i]
	elif a[i] - now < 0:
		all += n - now + a[i]
		now = a[i]
	i += 1
print(all)


(n, m) = list(map(int, input().split()))
cur_house = 1
res = 0
for task in list(map(int, input().split())):
	if task >= cur_house:
		res += task - cur_house
		cur_house = task
	else:
		res += n - (cur_house - task)
		cur_house = task
print(res)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
(a1, a2, a3) = (0, 0, 0)
for i in range(len(a) - 1):
	if a[i] > a[i + 1]:
		a1 += n - a[i] + a[i + 1]
	elif a[i] <= a[i + 1]:
		a2 += a[i + 1] - a[i]
for i in range(1, a[0]):
	a3 += 1
print(a1 + a2 + a3)


s = input().split()
n = int(s[0])
m = int(s[1])
w = 1
s = input()
a = [int(x) for x in s.split()]
ans = 0
for x in a:
	if x >= w:
		ans = ans + (x - w)
	else:
		ans = ans + (n - w + x)
	w = x
print(ans)


(n, m) = [int(x) for x in input().split()]
thing = list(map(int, input().split()))
length = len(thing)
time = 0
spot = 1
for i in range(length):
	if thing[i] >= spot:
		time += thing[i] - spot
	else:
		time += n + thing[i] - spot
	spot = thing[i]
print(time)


(a, m) = map(int, input().split())
n = input().split()
t = []
s = 0
p = 1
for q in range(m):
	t.append(int(n[q]))
for i in range(m):
	if t[i] >= p:
		s += t[i] - p
	else:
		s += a + t[i] - p
	p = t[i]
print(s)


s = input().split(' ')
t = int(s[0])
n = int(s[1])
s = input().split(' ')
s[0] = int(s[0])
d = s[0] - 1
for i in range(1, n):
	s[i] = int(s[i])
	temp = s[i] - s[i - 1]
	if temp < 0:
		temp += t
	d += temp
print(d)


[n, m] = [int(x) for x in input().split()]
workList = [int(x) for x in input().split()]
timesAround = 0
i = 0
while i < len(workList) - 1:
	if workList[i] > workList[i + 1]:
		timesAround += 1
	i += 1
print(timesAround * n + workList[-1] - 1)


(n, m) = [int(i) for i in input().strip().split()]
a = [int(i) for i in input().strip().split()]
multiplier = 0
for i in range(1, len(a)):
	if a[i - 1] > a[i]:
		multiplier = multiplier + 1
ans = a[-1] - 1 + multiplier * n
print(ans)


(a, b) = list(map(int, input().split(' ')))
c = list(map(int, input().split(' ')))
count = 0
p = 1
for i in c:
	if i == p:
		count = count + 0
	elif i > p:
		count = count + (i - p)
		p = i
	elif i < p:
		count = count + (a - p) + i
		p = i
print(count)


(n, k) = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
arr.insert(0, 1)
ans = 0
for i in range(1, k + 1):
	temp = arr[i] - arr[i - 1]
	if temp < 0:
		ans = ans + n + temp
	else:
		ans += temp
print(ans)


(n, m) = map(int, input().split())
li = list(map(int, input().split()))
temp1 = abs(1 - li[0])
li1 = []
for i in range(len(li) - 1):
	if li[i] < li[i + 1]:
		li1.append(abs(li[i] - li[i + 1]))
	elif li[i] > li[i + 1]:
		li1.append(abs(n - li[i]) + li[i + 1])
res = temp1 + sum(li1)
print(res)


(n, m) = map(int, input().split())
tasks = map(lambda x: int(x) - 1, input().split())
time = 0
p = 0
for task in tasks:
	if task > p:
		time += task - p
	elif task < p:
		time += n - p + task
	p = task
print(time)


(n, m) = map(int, input().split(' '))
l = list(map(int, input().split(' ')))
currentHouse = 1
time = 0
i = 0
while i < len(l):
	if l[i] < currentHouse:
		time += n - currentHouse + l[i]
		currentHouse = l[i]
	else:
		time += l[i] - currentHouse
		currentHouse = l[i]
	i += 1
print(time)


(a, b) = list(map(int, input().split()))
d = 1
kq = 0
lst = list(map(int, input().split()))
for i in lst:
	if d <= i:
		kq = kq + i - d
	else:
		kq = kq + a - d + i
	d = i
print(kq)


(n, m) = [int(q) for q in input().split(' ')]
ar = []
time = 0
j = 0
ctr = 0
ar = [int(s) for s in input().split(' ')]
time = ar[0] - 1
for i in range(1, m):
	if ar[i] >= ar[i - 1]:
		time += ar[i] - ar[i - 1]
	else:
		time += n + ar[i] - ar[i - 1]
print(str(time))


(n, m) = map(int, input().split())
s = list(map(int, input().split()))
sum = 0
i = 1
f = 1
for i in range(1, m + 1):
	j = f
	if s[i - 1] >= j:
		k = s[i - 1] - j
		f = s[i - 1]
		sum += k
	else:
		k = n - j + s[i - 1]
		f = s[i - 1]
		sum += k
print(sum)


(n, m) = map(int, input().split())
(d, e) = (0, 1)
for i in map(int, input().split()):
	d += (i - e) % n
	e = i
print(d)


import itertools
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
o = 1
time = 0
for y in range(len(l)):
	if o < l[y]:
		time += l[y] - o
		o = l[y]
	elif o > l[y]:
		time += n - (o - l[y])
		o = l[y]
	else:
		pass
print(time)


(n, m) = list(map(int, input().split()))
things = list(map(int, input().split()))
home_num = 1
cntr = 0
for thing in things:
	if home_num <= thing:
		cntr += thing - home_num
	else:
		cntr += n + thing - home_num
	home_num = thing
print(cntr)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
ans = l[0] - 1
for i in range(1, m):
	if i > 0 and l[i - 1] > l[i]:
		ans = ans + (n - l[i - 1] + l[i])
	if i > 0 and l[i - 1] < l[i]:
		ans = ans + (l[i] - l[i - 1])
print(ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
x = l[0]
c = x - 1
for i in range(1, m):
	c += (n - l[i - 1] + l[i]) % n
print(c)


def Main():
	(n, m) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = 0
	for i in range(1, m):
		if arr[i] < arr[i - 1]:
			ans += n
	ans += arr[m - 1] - 1
	print(ans)
Main()


ln = list(map(int, input().split()))
l = ln[0]
n = ln[1]
mission = list(map(int, input().split()))
now = 1
ans = 0
for i in range(n):
	if mission[i] >= now:
		ans = ans + mission[i] - now
	else:
		ans = ans + mission[i] + l - now
	now = mission[i]
print(ans)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
cnt = 0
prev = 1
for i in range(m):
	if arr[i] > prev:
		cnt += arr[i] - prev
	elif arr[i] == prev:
		cnt += 0
	else:
		cnt = cnt + (n - prev) + (arr[i] - 1) + 1
	prev = arr[i]
print(cnt)


(n, m) = input().split()
n = int(n)
m = int(m)
line = [int(i) for i in input().split()]
situ = 1
time = 0
for i in line:
	if i > situ:
		time += i - situ
		situ = i
	elif i == situ:
		continue
	else:
		time += n - (situ - i)
		situ = i
print(time)


s = input().split()
t = input().split()
t.insert(0, 1)
a = 0
for i in range(1, int(s[1]) + 1):
	b = int(t[i]) - int(t[i - 1])
	if b > 0:
		a = a + b
	elif b == 0:
		a = a
	else:
		a = a + int(s[0]) + b
print(a)


(n, m) = [int(i) for i in input().split()]
nums = [int(i) for i in input().split()]
time = nums[0] - 1
if m == 1:
	print(time)
else:
	for k in range(1, m):
		if nums[k] >= nums[k - 1]:
			time += nums[k] - nums[k - 1]
		else:
			time += n + nums[k] - nums[k - 1]
	print(time)


l = list(map(int, input().split()))
t = list(map(int, input().split()))
time = t[0] - 1
for i in range(1, len(t)):
	if t[i] >= t[i - 1]:
		time += t[i] - t[i - 1]
	else:
		time += l[0] - t[i - 1] + t[i]
print(time)


(n, m) = map(int, input().split())
t = 0
tp = 1
for i in list(map(int, input().split())):
	if i > tp:
		t += i - tp
	elif i < tp:
		t += n - tp + i
	tp = i
print(t)


(n, t) = map(int, input().split())
l = list(map(int, input().split()))
t = 0
h = 1
for i in l:
	if i >= h:
		t = t + i - h
		h = i
	else:
		t = t + n - h + i
		h = i
print(t)


def inp():
	return map(int, input().split())
(r, c) = (0, 1)
(n, m) = inp()
for x in inp():
	r += (x - c) % n
	c = x
print(r)


[n, m] = [int(x) for x in input().split()]
tasks = [int(x) for x in input().split()]
time = 0
prev = 1
for t in tasks:
	if t > prev:
		time = time + (t - prev)
		prev = t
	elif t < prev:
		time = time + (n - prev) + t
		prev = t
print(time)


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
si = lambda : input()
(n, m) = mi()
l = li()
z = 1
p = 1
for i in l:
	z += (i - p) % n
	p = i
print(z - 1)


(n, m) = [int(i) for i in input().split(' ')]
tasks = [int(i) for i in input().split(' ')]
total_moves = 0
current_house = 1
for visiting in tasks:
	if visiting >= current_house:
		total_moves += visiting - current_house
	else:
		total_moves += n - current_house + visiting
	current_house = visiting
print(total_moves)


(a, b) = input().split()
a = int(a)
b = int(b)
li = list(map(int, input().split()))
c = 0
i = 0
while i < len(li) - 1:
	if li[i + 1] < li[i]:
		c = c + 1
	i = i + 1
print(a * c + li[-1] - 1)


(n, m) = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
cur = 1
t = 0
for i in a:
	if i >= cur:
		t += i - cur
	else:
		t += n - cur + i
	cur = i
print(t)


def visit(s, d, n):
	if s <= d:
		return d - s
	else:
		return d + (n - s)
(n, m) = input().split()
n = int(n)
m = int(m)
sum = 0
source = 1
dest = input().split()
for i in dest:
	dest = int(i)
	sum += visit(source, dest, n)
	source = dest
print(sum)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
res = a[0] - 1
for i in range(1, m):
	prev = a[i - 1]
	curr = a[i]
	if curr > prev:
		res += curr - prev
	elif curr < prev:
		res += curr + n - prev
print(res)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
time = tasks[0] - 1
for i in range(1, m):
	a = tasks[i - 1]
	b = tasks[i]
	if a == b:
		continue
	elif a > b:
		time += n - (a - b)
	else:
		time += b - a
print(time)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
cur_in = 1
sum1 = 0
count = 0
for i in range(m):
	if a[i] > cur_in:
		sum1 += a[i] - cur_in
		cur_in = a[i]
	elif a[i] < cur_in:
		count = n - cur_in + a[i]
		sum1 += count
		cur_in = a[i]
print(sum1)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
from atexit import register
from io import BytesIO
import io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, m) = map(int, input().split())
ar = [1] + list(map(int, input().split()))
answer = 0
for i in range(m):
	if ar[i] <= ar[i + 1]:
		answer += ar[i + 1] - ar[i]
	else:
		answer += n - (ar[i] - ar[i + 1])
print(answer)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
prev_task = 1
count = 0
for i in b:
	task = i
	if task == prev_task:
		count += 0
	elif task > prev_task:
		count += task - prev_task
		prev_task = task
	else:
		count += a[0] - prev_task + task
		prev_task = task
print(count)


import math
(a, b) = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
d = 1
sum = 0
for i in range(0, len(c)):
	if c[i] == d:
		d = c[i]
		pass
	elif c[i] > d:
		e = abs(c[i] - d)
		d = c[i]
		sum = sum + e
	else:
		e = a - abs(c[i] - d)
		d = c[i]
		sum = sum + e
print(sum)


(N, M) = [int(n) for n in input().split()]
L = [int(n) for n in input().split()]
c = 0
ini = 1
for x in L:
	if x >= ini:
		c += x - ini
		ini = x
	else:
		p = x
		x += N
		c += x - ini
		ini = p
print(c)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
(x, r) = (1, 0)
for v in a:
	r += (v - x + n) % n
	x = v
print(r)


(n, m) = [int(number) for number in input().split(' ')]
houses = [int(number) for number in input().split(' ')]
current = 1
time = 0
for house in houses:
	if house >= current:
		time += house - current
	else:
		time += n - current + house
	current = house
print(time)


from itertools import cycle
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
step = 0
current = 1
for k in range(len(arr)):
	if arr[k] > current:
		step += arr[k] - current
	elif arr[k] < current:
		step += n - current + arr[k]
	elif arr[k] == current:
		pass
	current = arr[k]
print(step)


(n, m) = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
count = a[0] - 1
t = a[0]
for i in a:
	if t < i:
		count += i - t
	elif t > i:
		count += n - t + i
	t = i
print(count)


(n, m) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
c = 1
d = 0
for i in l:
	if i > c:
		d += i - c
		c = i
	elif i < c:
		d += n - c + i
		c = i
print(d)


(n, m) = map(int, input().split())
List = list(map(int, input().split()))
Sum = List[0] - 1
for x in range(1, len(List)):
	if List[x] < List[x - 1]:
		Sum += List[x] + n - List[x - 1]
	else:
		Sum += List[x] - List[x - 1]
print(Sum)


(n, m) = map(int, input().split())
s = list(map(int, input().split()))
c = s[0]
for i in range(1, m):
	if s[i] == s[i - 1]:
		continue
	elif s[i] > s[i - 1]:
		c += s[i] - s[i - 1]
	else:
		c += n - s[i - 1] + s[i]
print(c - 1)


(n, m) = map(int, input().split())
ta = list(map(int, input().split()))
la = 0
s = -1
for a in ta:
	if a > la:
		d = a - la
		s += d
		la = a
	elif a < la:
		d = n + a - la
		s += d
		la = a
	elif a == la:
		a = la
print(s)


(n, m) = map(int, input().split())
s = [int(x) for x in input().split()]
count = 0
for i in range(m - 1):
	if s[i + 1] < s[i]:
		count += 1
print(n * count + int(s[m - 1]) - 1)


def inpNum():
	return int(input())

def inpNums():
	return map(int, input().split())

def inpList():
	return list(map(int, input().split()))
(n, m) = inpNums()
ai = inpList()
ans = ai[0] - 1
for i in range(1, m):
	if ai[i] >= ai[i - 1]:
		ans += ai[i] - ai[i - 1]
	else:
		ans += n - ai[i - 1] + ai[i]
print(ans)


(n, m) = map(int, input().split())
t = list(map(int, input().split()))
print(n * sum((t[i] > t[i + 1] for i in range(m - 1))) + t[m - 1] - 1)


(a, b) = map(int, input().split())
c = list(map(int, input().split()))
s = 0
k = 1
c.append(0)
for i in range(b):
	if k != c[i] and c[i] > k:
		s += c[i] - k
		k = c[i]
	elif k != c[i] and c[i] < k:
		s += a - k + c[i]
		k = c[i]
	if k == a and c[i] != c[i + 1]:
		k = 0
print(s)


(a, b) = map(int, input().split())
if b == 1:
	print(b - 1)
else:
	c = list(map(int, input().split()))
	l = [(i, z) for (i, z) in zip(c, c[1:])]
	total = l[0][0] - 1
	for i in l:
		if i[1] >= i[0]:
			total += i[1] - i[0]
		else:
			total += a - i[0] + i[1]
	print(total)


import sys
(n, m) = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
total_time = 0
start = 1
for i in range(m):
	end = a[i]
	total_time += (end - start) % n
	start = end
sys.stdout.write(str(total_time))


(a, b) = map(int, input().split())
li = [int(x) for x in input().split()]
k = 0
ncount = li[0] - 1
for i in range(b - 1):
	if li[k] <= li[k + 1]:
		ncount += li[k + 1] - li[k]
		k += 1
	else:
		ncount += a - li[k] + li[k + 1]
		k += 1
print(ncount)


(n, m) = map(int, input().split())
tasks = [int(x) for x in input().split()]
current = 1
res = 0
for x in tasks:
	if x > current:
		res += x - current
	elif x < current:
		res += n - current + x
	current = x
print(res)


(x, y) = list(map(int, input().split()))
a = []
a.extend(list(map(int, input().split())))
a = list(map(lambda x: x - 1, a))
count = 0
curr = 0
for i in a:
	if i >= curr:
		count = count + i - curr
		curr = i
	else:
		count = count + x - curr
		curr = 0
		count += i
		curr = i
print(count)


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
d = 0
p = 1
for a in A:
	if a > p:
		d += a - p
	elif a < p:
		d += n - p + a
	p = a
print(d)


(n, m) = map(int, input().split())
(*i,) = map(int, input().split())
p = 0
for j in range(m - 1):
	if i[j + 1] < i[j]:
		p += 1
print(n * p + i[-1] - 1)


def ringroad(n, m, a):
	res = 0
	prev = 1
	for i in a:
		if i > prev:
			res += i - prev
			prev = i
		elif i < prev:
			res += n - prev + i
			prev = i
	return res
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
print(ringroad(n, m, a))


(n, m) = [int(x) for x in input().split()]
an = [int(x) for x in input().split()]
moves = an[0] - 1
for i in range(1, m):
	if an[i] >= an[i - 1]:
		moves += an[i] - an[i - 1]
	else:
		moves += n - an[i - 1] + an[i]
print(moves)


(n, m) = map(int, input().split())
A = [int(x) for x in input().split()]
total_time = 0
cur_pos = 1
for ai in A:
	dif = ai - cur_pos
	total_time += dif + n if dif < 0 else dif
	cur_pos = ai
print(total_time)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
i = 1
r = a[0] - 1
while i < m:
	r += (a[i] - a[i - 1]) % n
	i += 1
print(r)


(n, m) = input().split()
s = [int(i) for i in input().split()]
time = 0
result = 0
for i in range(int(m)):
	if s[i] >= s[i - 1] and i != 0:
		result += s[i] - s[i - 1]
	elif s[i] < s[i - 1] and i != 0:
		result += int(n) - s[i - 1] + s[i]
print(result + s[0] - 1)


(x, y) = input().split()
x = int(x)
y = int(y)
a = [int(i) for i in input().split()]
l = []
s = 0
for i in range(y - 1):
	if a[i] <= a[i + 1]:
		s = a[i + 1] - a[i]
		l.append(s)
	else:
		s = x - a[i] + a[i + 1]
		l.append(s)
result = sum(l) + a[0] - 1
print(result)


arr = input().split()
m = int(arr[0])
n = int(arr[1])
dist = 0
arr = input().split()
for i in range(n):
	arr[i] = int(arr[i])
start = 1
ans = 0
for i in range(n):
	diff = arr[i] - start
	if diff < 0:
		diff += m
	ans += diff
	start = arr[i]
print(int(ans))


(n, m) = [int(x) for x in input().split(' ')]
array = [int(x) for x in input().split(' ')]
before = 1
tot = 0
for i in range(len(array)):
	tot += (array[i] - before) % n
	before = array[i]
print(tot)


(n, m) = map(int, input().split())
t = list(map(int, input().split()))
count = 0
curr = 1
for h in t:
	if h >= curr:
		count += h - curr
		curr = h
	else:
		count += n - curr + h
		curr = h
print(count)


(m, n) = input().split()
m = int(m)
n = int(n)
a = [int(x) for x in input().split(' ')]
loc = 1
ans = 0
for x in a:
	if loc > x:
		ans += x + m - loc
	else:
		ans += x - loc
	loc = x
print(ans)


a = input()
n = int(a.split()[0])
m = int(a.split()[1])
line = [int(i) for i in input().split()]
min = 0
for i in range(1, m):
	if line[i - 1] <= line[i]:
		continue
	else:
		min += n
min += line[m - 1] - 1
print(min)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
N = 1
sh = 0

def distance(a, b):
	global n
	if b >= a:
		return b - a
	return n - a + b
for i in l:
	sh += distance(N, i)
	N = i
print(sh)


(n, m) = map(int, input().split())
M = 1
s = 0
l = input().split()
for i in range(m):
	ai = int(l[i])
	if ai >= M:
		s += ai - M
		M = ai
	else:
		s += n - M + ai
		M = ai
print(s)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
result = 0
po = 1
for i in range(len(a)):
	if a[i] < po:
		result += n - a[i - 1] + a[i]
		po = a[i]
	else:
		result += a[i] - po
		po = a[i]
print(result)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
step = a[0] - 1 if a[0] != 1 else 0
i = 1
current = a[0]
while i < len(a):
	next_pos = a[i]
	step += next_pos - current + (n if next_pos < current else 0)
	current = next_pos
	i += 1
print(step)


(n, m) = map(int, input().split())
L = [int(i) for i in input().split()]
L.insert(0, int(1))

def t(a, b):
	if b >= a:
		return b - a
	else:
		return n - a + b
time = 0
for i in range(0, len(L) - 1):
	time = time + t(L[i], L[i + 1])
print(time)


def solve(n, m, a):
	cur = 1
	s = 0
	for i in a:
		if i >= cur:
			s += i - cur
		else:
			s += n - cur + i
		cur = i
	return s

def main():
	(n, m) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	print(solve(n, m, a))
main()


while True:
	try:
		(n, m) = map(int, input().split())
		t = range(n)
		l = [int(i) for i in input().split()]
		l = [1] + l
		time = 0
		for i in range(m):
			time += t[l[i + 1] - l[i]]
		print(time)
	except EOFError:
		break


def convert(arr):
	for i in range(len(arr)):
		arr[i] = int(arr[i])
	return arr
inp = convert(input().split())
arr = convert(input().split())
ans = arr[0] - 1
for i in range(1, len(arr)):
	if arr[i] < arr[i - 1]:
		ans += inp[0] - arr[i - 1] + arr[i]
	else:
		ans += arr[i] - arr[i - 1]
print(ans)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().rstrip().split()))
sum1 = a[0] - 1
for i in range(0, m - 1):
	if a[i + 1] < a[i]:
		sum1 += n - a[i]
		sum1 += a[i + 1]
	else:
		sum1 += a[i + 1] - a[i]
print(sum1)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = [1] + a
c = 0
for i in range(m):
	k = b[i + 1] - b[i]
	if k >= 0:
		c += k
	else:
		c += n + k
print(c)


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
(n, m) = rinput()
A = rlinput()
time = 0
for i in range(m):
	if i == 0:
		time += A[i] - 1
	elif A[i] < A[i - 1]:
		time += n - A[i - 1]
		time += A[i]
	else:
		time += A[i] - A[i - 1]
print(time)


p = input().split()
a = input().split()
house = int(p[0])
task = int(p[1])
l = []
s = int(a[0])
k = int(a[0])
for i in range(1, len(a)):
	if int(a[i]) == k:
		s = s
		k = int(a[i])
	elif int(a[i]) > k:
		s = s + (int(a[i]) - k)
		k = int(a[i])
	else:
		s = s + (house - k) + int(a[i])
		k = int(a[i])
print(s - 1)


a = list(input().split(' '))
(n, m) = (int(a[0]), int(a[1]))
a = list(input().split(' '))
a = list((int(x) for x in a))
res = 0
last = 1
for i in range(m):
	if last > a[i]:
		res += n - last + a[i]
	else:
		res += a[i] - last
	last = a[i]
print(res)


(n, m) = map(int, input().split())
t = list(map(int, input().split()))
prev = 0
bigs = []
for i in t:
	if i >= prev:
		prev = i
	else:
		bigs.append(prev)
		prev = i
bigs.append(t[-1])
print((len(bigs) - 1) * n + (bigs[-1] - 1))


(n, m) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
d = a[0] - 1
for i in range(0, m - 1):
	b = a[i:i + 2]
	if b[1] < b[0]:
		d = d + (n - b[0] + b[1])
	else:
		d = d + (b[1] - b[0])
print(d)


k = lambda : map(int, input().split())
(n, m) = k()
s = 0
p = 1
for v in k():
	s += (v - p) % n
	p = v
print(s)


houses = int(input().split()[0])
tasks = list(map(int, input().split()))
current = tasks[0]
moves = current - 1
idx = 1
while len(tasks) > idx:
	nextTask = tasks[idx]
	if nextTask != current:
		if nextTask < current:
			moves += houses - current + nextTask
			current = nextTask
		else:
			moves += nextTask - current
			current = nextTask
	idx += 1
print(moves)


l = list(map(int, input().split()))
n = l[0]
tasks = l[1]
xx = list(map(int, input().split()))
c = xx[0]
co = xx[0] - 1
for i in range(1, len(xx)):
	if c != xx[i]:
		if c < xx[i]:
			co += xx[i] - c
			c = xx[i]
		else:
			co += n - c + xx[i]
			c = xx[i]
print(co)


(n, m) = [int(i) for i in input().split()]
tasks = [int(i) for i in input().split()]
res = tasks[0] - 1
for i in range(1, len(tasks)):
	if tasks[i] >= tasks[i - 1]:
		res = res + (tasks[i] - tasks[i - 1])
	else:
		res = res + (n - (tasks[i - 1] - tasks[i]))
print(res)


(n, m) = input().split()
n = int(n)
m = int(m)
l = [int(i) for i in input().split()]
a = 1
b = 0
c = 1
d = 0
for i in l:
	if i >= a:
		b += i - a
		a = i
	else:
		b += n - a + i
		a = i
print(b)


(n, t) = map(int, input().split())
m = list(map(int, input().split()))
time = m[0] - 1
for i in range(1, t):
	if m[i] > m[i - 1]:
		time += m[i] - m[i - 1]
	elif m[i] < m[i - 1]:
		time += n - m[i - 1] + m[i]
print(time)


from functools import reduce

def input_values(f):
	return map(f, input().split())

def main():
	(n, m) = input_values(int)
	koto = list(map(lambda x: x - 1, [1] + list(input_values(int))))
	ans = 0
	for i in range(1, m + 1):
		ans += (koto[i] - koto[i - 1] + n) % n
	print(ans)
main()


u = input().split()
n = int(u[0])
k = 0
l = list(map(int, input().split()))
l[0:0] = [1]
for i in range(len(l) - 1):
	p = l[i + 1] - l[i]
	if p >= 0:
		k += p
	else:
		k += p + n
print(k)


first = input()
second = input()
n = int(str(first).split(' ')[0])
m = int(str(first).split(' ')[1])
ts = [1] + [int(i) for i in str(second).split(' ')]
sum = 0
for i in range(0, m):
	sum += (ts[i + 1] - ts[i]) % n
print(sum)


n = [int(j) for j in input().split()]
k = [int(j) for j in input().split()]
i = 0
for s in range(n[1] - 1):
	if k[s + 1] < k[s]:
		i += 1
print(i * n[0] + k[-1] - 1)


(a, b) = [int(x) for x in input().split()]
lst = [int(x) for x in input().split()]
time = 0
c = 1
for i in lst:
	if i - c >= 0:
		time += i - c
	else:
		time += a - c + i
	c = i
print(time)


l = [int(x) for x in input().split()]
n = l[0]
m = l[1]
l = [int(x) for x in input().split()]
count = 0
count = count + (l[0] - 1)
for i in range(1, len(l)):
	x = l[i] - l[i - 1]
	if x >= 0:
		count = count + x
	else:
		count = count + (n - l[i - 1] + l[i])
print(count)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
(prev, ans) = (1, 0)
for i in range(0, m):
	ans += (l[i] - prev) % n
	prev = l[i]
print(ans)


inp = input('')
n_m = inp.split(' ')
n_m_num = []
for i in n_m:
	n_m_num.append(int(i))
n = n_m_num[0]
m = n_m_num[1]
inp = input('')
a = inp.split(' ')
a_num = []
for i in a:
	a_num.append(int(i))
out = 0
position = 1
for i in range(0, m):
	if position > a_num[i]:
		out += abs(n - position) + a_num[i]
	elif position < a_num[i]:
		out += abs(a_num[i] - position)
	elif position == a_num:
		out += 0
	position = a_num[i]
print(out)


(n, m) = [int(i) for i in input().split()]
tasks = [int(i) for i in input().split()]
(currentHouse, totalTime) = [1, 0]
for task in tasks:
	if task >= currentHouse:
		totalTime += task - currentHouse
	else:
		totalTime += n - currentHouse
		currentHouse = 1
		totalTime += task - currentHouse + 1
	currentHouse = task
print(totalTime)


(n, m) = [int(x) for x in input().split()]
L = [int(x) for x in input().split()]
x = 0
y = 1
for i in range(len(L)):
	if L[i] >= y:
		x = x + L[i] - y
		y = L[i]
	else:
		x = x + n + L[i] - y
		y = L[i]
print(x)


(n, m) = list(map(int, input().split()))
A = list(map(int, input().split()))
answer = 0
cur = 1
for i in range(m):
	answer += (A[i] - cur + n) % n
	cur = A[i]
print(answer)


(n_houses, m_tasks) = map(int, input().split(' '))
houses = [int(i) for i in input().split(' ')]
time = houses[0] - 1
for i in range(m_tasks - 1):
	if houses[i] < houses[i + 1]:
		time += houses[i + 1] - houses[i]
	elif houses[i] > houses[i + 1]:
		time += n_houses - houses[i] + houses[i + 1]
	else:
		time += 0
print(time)


(n, m) = [int(x) for x in input().split()]
p = 1
time = 0
tasks = [int(x) for x in input().split()]
for task in tasks:
	if task < p:
		time += n - p + task
	else:
		time += task - p
	p = task
print(time)


a = input().split()
tot = int(a[0])
m = int(a[1])
b = input().split()
for i in range(0, m):
	b[i] = int(b[i])
i = 0
s = b[0] - 1
while i < m - 1:
	if b[i + 1] < b[i]:
		s = s - b[i] + tot + b[i + 1]
	else:
		s = s + b[i + 1] - b[i]
	i = i + 1
print(s)


(n, m) = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
sum = 0
tmp = 1
for i in range(m):
	if arr[i] >= tmp:
		sum += arr[i] - tmp
		tmp = arr[i]
	else:
		sum += n - tmp + arr[i]
		tmp = arr[i]
print(sum)


(a, b) = map(int, input().split())
mas = list(map(int, input().split()))
x = 1
k = 0
for i in mas:
	if i > x:
		k += i - x
		x = i
	elif i < x:
		k += a - x
		k += i
		x = i
print(k)


(n, k) = input().split()
n = int(n)
k = int(n)
l = [int(i) for i in input().split()]
curr = 1
time = 0
for i in l:
	if i == curr:
		continue
	elif i < curr:
		time += n - curr + i
		curr = i
		continue
	else:
		time += i - curr
		curr = i
		continue
print(time)


b = [int(x) for x in input().split()]
n = b[0]
m = b[1]
a = [int(x) for x in input().split()]
br = 0
tren = 1
for i in range(0, m):
	if tren <= a[i]:
		br += a[i] - tren
		tren = a[i]
	else:
		br += n + 1 - tren
		tren = 1
		br += a[i] - tren
		tren = a[i]
print(br)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
kol = 0
polog = 1
for i in range(m):
	if a[i] >= polog:
		kol += a[i] - polog
	else:
		kol += n - polog + a[i]
	polog = a[i]
print(kol)


(a, b) = map(int, input().rstrip().split(' '))
l = list(map(int, input().rstrip().split(' ')))
last = 1
cycle = 0
for i in l:
	if i < last:
		cycle += 1
	last = i
print(a * cycle + l[-1] - 1)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
for i in range(m - 1):
	if a[i] > a[i + 1]:
		cnt += 1
print(cnt * n + a[-1] - 1)


a = [int(i) for i in input().split()]
line = [int(i) for i in input().split()]
n = a[0]
m = a[1]
t = line[0] - 1
for i in range(m - 1):
	if line[i] <= line[i + 1]:
		t += line[i + 1] - line[i]
	else:
		t += line[i + 1] - line[i] + n
print(t)


(n, m) = map(int, input().split())
cases = map(int, input().split())
c = 0
a = 1
for b in cases:
	if a <= b:
		c += b - a
	else:
		c += n - a + b
	a = b
print(c)


l1 = [int(x) for x in input().split()]
(n, m) = (l1[0], l1[1])
l2 = [1] + [int(x) for x in input().split()]
ans = 0
for x in range(m):
	if l2[x] <= l2[x + 1]:
		ans += l2[x + 1] - l2[x]
	else:
		ans += n - l2[x] + l2[x + 1]
print(ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
pos = 1
co = 0
for i in l:
	if i >= pos:
		co += i - pos
		pos = i
	else:
		co += n - pos
		co += i
		pos = i
print(co)


(n, m) = [int(x) for x in input().split()]
task = [int(x) for x in input().split()]
place = 1
count = 0
for i in range(m):
	if task[i] - place >= 0:
		count = count + task[i] - place
		place = task[i]
	if task[i] - place < 0:
		count = count + n + task[i] - place
		place = task[i]
print(count)


import sys
sys.setrecursionlimit(100000)
from collections import defaultdict
from collections import deque
import heapq
I = lambda : list(map(int, input().split(' ')))
(n, m) = I()
lst = [ele for ele in I()]
t = 0
cur = 0
t += lst[0]
for i in range(1, len(lst)):
	if lst[i] >= lst[i - 1]:
		t += lst[i] - lst[i - 1]
	else:
		t += n - abs(lst[i] - lst[i - 1])
print(t - 1)


a = int(input().split()[0])
(*b,) = map(int, input().split())
c = d = ans = 0
for i in b:
	c = i
	if c < d:
		ans += a + c - d
	else:
		ans += c - d
	d = i
print(ans - 1)


R = lambda : map(int, input().split())
(n, m) = R()
a = [1] + [*R()]
s = 0
for i in range(m):
	diff = a[i + 1] - a[i]
	s += [diff, n + diff][diff < 0]
print(s)


I = lambda : map(int, input().split())
(n, m) = I()
r = 0
t = 1
for i in I():
	r += (n + i - t) % n
	t = i
print(r)


(n, m) = map(int, input().split(' '))
road = list(map(int, input().split(' ')))
circle = 1
for i in range(len(road) - 1):
	if road[i] > road[i + 1]:
		circle += 1
print(n * (circle - 1) + road[len(road) - 1] - 1)


nm = input().split()
tasks = list(map(lambda x: int(x), input().split()))
current_place = 1
time = 0
for x in tasks:
	if x > current_place:
		time += x - current_place
		current_place = x
	if x < current_place:
		time += int(nm[0]) - current_place + x
		current_place = x
print(time)


x = int(input().split()[0])
y = map(int, input().split())
a = 1
b = 0
for i in y:
	b += (i - a) % x
	a = i
print(b)


(n, m) = map(int, input().split())
li = [int(x) for x in input().split()]
pos = 1
t = 0
for i in li:
	if i >= pos:
		t += i - pos
		pos = i
	else:
		t += n - pos + 1 + (i - 1)
		pos = i
print(t)


(X, Sum, Index) = (list(map(int, input().split())), 0, 0)
Tasks = list(map(lambda x: int(x) - 1, input().split()))
for i in range(X[1]):
	Diff = Tasks[i] - Index
	Sum += Diff if Diff >= 0 else X[0] - Index + Tasks[i]
	Index = Tasks[i]
print(Sum)


(numOfHouses, numOfTasks) = [int(x) for x in input().split()]
tasks = [int(x) for x in input().split()]
position = 1
count = 0
for i in tasks:
	if position <= i:
		count += i - position
	else:
		count += numOfHouses - position + i
	position = i
print(count)


import sys

def ii():
	return sys.stdin.readline().strip()

def idata():
	return [int(x) for x in ii().split()]

def solve_of_problem():
	(n, m) = idata()
	data = idata()
	ans = 0
	t = 1
	for i in range(m):
		ans += (data[i] - t) % n
		t = data[i]
	print(ans)
	return
for ______ in range(1):
	solve_of_problem()


l = input().split()
n = int(l[0])
l = list(map(int, input().split()))
x = l[0]
m = 1
for i in l[1:]:
	if i < l[m - 1]:
		x += n + i - l[m - 1]
	elif i > l[m - 1]:
		x += i - l[m - 1]
	elif i == l[m - 1]:
		x += 0
	m += 1
print(x - 1)


(n, m) = map(int, input().split())
t = r = 0
for x in map(int, input().split()):
	x -= 1
	r += (x - t) % n
	t = x
print(r)


def solve(n, xs):

	def house_distance(a, b):
		return (b - a) % n
	return sum((house_distance(a, b) for (a, b) in zip([1] + xs, xs)))

def main():
	[n, m] = [int(i) for i in input().strip().split()]
	xs = [int(i) for i in input().strip().split()]
	assert m == len(xs)
	assert all((1 <= a <= n for a in xs))
	result = solve(n, xs)
	print(result)
main()


(n, m) = map(int, input().split())
l = [int(i) for i in input().split()]
num_loops = 0
for x in range(m - 1):
	if l[x] > l[x + 1]:
		num_loops += 1
print(num_loops * n + l[-1] - 1)


inp = input().split()
n = int(inp[0])
m = int(inp[1])
inp = input().split()
place = 1
tim = 0
for i in range(m):
	if int(inp[i]) >= place:
		tim += int(inp[i]) - place
	else:
		tim += n - place + int(inp[i])
	place = int(inp[i])
print(tim)


(n, m) = map(int, input().split())
li = [int(x) for x in input().split()]
ti = li[0]
for i in range(1, m):
	if li[i] >= li[i - 1]:
		ti += li[i] - li[i - 1]
	else:
		ti += n - (li[i - 1] - li[i])
print(ti - 1)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
dist = 0
pos = 1
idx = 0
curr = 0
for i in tasks:
	if i >= pos:
		dist += i - pos
	else:
		dist += n - pos + i
	pos = i
print(dist)


(k, b) = list(map(int, input().split()))
a = list(map(int, input().split()))
c = 0
d = 1
for i in a:
	c += (i - d) % k
	d = i
print(c)


(n, m) = [int(x) for x in input().split()]
list = [int(x) for x in input().split()]
k = list[0] - 1
for i in range(1, m):
	if list[i] >= list[i - 1]:
		k += list[i] - list[i - 1]
	else:
		k += list[i] + n - list[i - 1]
print(k)


(n, m) = map(int, input().split())
works = list(map(int, input().split()))
ans = 0
current_home = 1
for i in works:
	ans += n - current_home + 1 + i - 1 if current_home > i else i - current_home
	current_home = i
print(ans)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
time = 0
start_point = 1
for task in tasks:
	if task >= start_point:
		time += task - start_point
		start_point = task
	else:
		time += n - start_point + task
		start_point = task
print(time)


(n, m) = [int(x) for x in input().strip().split()]
tasks = [int(x) for x in input().strip().split()]
time_taken = 0
curr_house = 1
for task in tasks:
	if task < curr_house:
		time_taken += n - curr_house + task
	else:
		time_taken += task - curr_house
	curr_house = task
print(time_taken)


(amount_of_houses, amount_of_deals) = list(map(int, input().split()))
deals = list(map(int, input().split()))
deals.insert(0, 1)
coord_o_k = 1
answer = 0
for i in range(1, len(deals)):
	dist = deals[i] - deals[i - 1]
	if dist < 0:
		dist = amount_of_houses + dist
	answer += dist
print(answer)


(n, m) = map(int, input().split(' '))
s = [int(i) for i in input().split(' ')]
prev = 1
ans = 0
for i in s:
	ans += (i - prev + n) % n
	prev = i
print(ans)


import sys
lines = []
for line in sys.stdin:
	lines.append(line.rstrip('\n'))
(houses, nb_of_tasks) = map(int, lines[0].split(' '))
tasks = map(int, lines[1].split(' '))
position = 1
time = 0
for task in tasks:
	if task < position:
		time += houses - position
		position = 0
	time += task - position
	position = task
print(time)


l1 = input()
l2 = input()
arr1 = list(map(lambda x: int(x), l1.split(' ')))
arr2 = list(map(lambda x: int(x), l2.split(' ')))
(m, n) = (arr1[0], arr1[1])
t = arr2[0] - 1
res = 0
for i in range(n - 1):
	if arr2[i] <= arr2[i + 1]:
		t = arr2[i + 1] - 1
	else:
		res += m
		t = arr2[i + 1] - 1
res += t
print(res)


(total, tasks) = map(int, input().split())
ans = 0
lis = list(map(int, input().split()))
start = 1
for i in lis:
	if i >= start:
		ans += i - start
	else:
		ans += total - (start - i)
	start = i
print(ans)


linea = input().split()
n = int(linea[0])
task = int(linea[1])
line = input()
lineb = [int(i) for i in line.split()]
time = 0
time = time + lineb[0] - 1
i = 1
while i < len(lineb):
	if lineb[i] == lineb[i - 1]:
		time = time
		i = i + 1
	elif lineb[i] > lineb[i - 1]:
		time = time + (lineb[i] - lineb[i - 1])
		i = i + 1
	else:
		time = time + (n - lineb[i - 1] + lineb[i])
		i = i + 1
print(time)


(n, m) = input().split()
(n, m) = (int(n), int(m))
pos = 1
rez = 0
tasks = input().split()
for num in tasks:
	num = int(num)
	if num >= pos:
		rez += num - pos
	else:
		rez += n - pos + num
	pos = num
print(rez)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
current = 1
work_time = 0
for houses in tasks:
	if houses < current:
		work_time += n - current + 1
		current = 1
	work_time += houses - current
	current = houses
print(work_time)


Q = input().split()
n = int(Q[0])
m = int(Q[1])
task = [int(i) for i in input().split()]
start = 1
count = 0
for i in range(m):
	if task[i] >= start:
		count += task[i] - start
		start = task[i]
	else:
		count += n - start + task[i]
		start = task[i]
print(count)


import collections

def solve(n, m):
	pass

def sort(key):
	return key[0]
(n, m) = map(int, input().split())
ok = 0
lst = list(map(int, input().split()))
ans = 0
maxed = 0
for i in range(len(lst) - 1):
	if lst[i] > lst[i + 1]:
		ans += n
		maxed = 0
	elif lst[i] < lst[i + 1]:
		maxed = max(lst[i + 1], maxed)
if not maxed:
	ans += lst[len(lst) - 1] - 1
	print(ans)
else:
	ans += maxed - 1
	print(ans)


prim = [int(i) for i in input().split()]
task = [int(i) for i in input().split()]
location = 1
move = 0
while prim[1] != 0:
	if task[0] >= location:
		move += task[0] - location
		location = task[0]
	else:
		move += prim[0] - location + task[0]
		location = task[0]
	prim[1] -= 1
	task.remove(task[0])
print(move)


def from_to(_from, _to, n):
	if _from > _to:
		return n - _from + _to
	elif _from < _to:
		return _to - _from
	else:
		return 0
(n, m) = list(map(int, input().split()))
t = list(map(int, input().split()))
out = 0
for i in range(len(t)):
	if not i == 0:
		out += from_to(t[i - 1], t[i], n)
	else:
		out += from_to(1, t[i], n)
print(out)


import sys
inputs = sys.stdin.readline().split()
(n, m) = (int(inputs[0]), int(inputs[1]))
tasks = sys.stdin.readline().split()
currentTask = 1
time = 0
for i in range(m):
	task = int(tasks[i])
	if task == currentTask:
		continue
	elif task > currentTask:
		time += task - currentTask
	elif task < currentTask:
		time += n - currentTask + task
	currentTask = task
print(time)


(n, m) = list(map(int, input().rstrip().split()))
am = list(map(int, input().rstrip().split()))
t = 0
pos = 1

def getDistance(a):
	if a >= pos:
		return a - pos
	else:
		return n - (pos - a)
for a in am:
	t += getDistance(a)
	pos = a
print(t)


(n, m) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
temp = n
count = 0
for i in range(m - 1):
	if i == 0:
		count += a[i] - 1
	if a[i] == a[i + 1]:
		continue
	if a[i] < a[i + 1]:
		count += a[i + 1] - a[i]
	else:
		count += n + a[i + 1] - a[i]
print(count)


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
con = 0
last = 0
for i in range(m - 1):
	if a[i] > a[i + 1]:
		con += 1
if con != 0:
	print(con * n + a[m - 1] - 1)
else:
	print(a[m - 1] - 1)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
loc = 1
ans = 0
for now in a:
	if now >= loc:
		ans += now - loc
	else:
		ans += n - (loc - now)
	loc = now
print(ans)


(n, m) = map(int, input().split())
a = map(int, input().split())
time = 0
current = 1
for ai in a:
	if ai > current:
		time += ai - current
	elif ai < current:
		time += n - current + ai
	current = ai
print(time)


(n, m) = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
h = 1
t = 0
for i in l:
	if i >= h:
		t += i - h
	else:
		t += n - h + i
	h = i
print(t)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
sum = arr[0]
for i in range(1, m):
	if arr[i] < arr[i - 1]:
		sum += n - arr[i - 1] + arr[i]
	elif arr[i] == arr[i - 1]:
		continue
	else:
		sum += arr[i] - arr[i - 1]
print(sum - 1)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
x = 1
c = 0
i = 0
while i < m:
	if x < a[i]:
		c += a[i] - x
	elif x > a[i]:
		c += n - x + a[i]
	x = a[i]
	i += 1
print(c)


nt = input().split()
nt = [int(x) for x in nt]
n = nt[0]
t = nt[1]
ringroad = input().split()
ringroad = [int(x) for x in ringroad]
time = ringroad[0] - 1
for i in range(1, len(ringroad)):
	if ringroad[i] > ringroad[i - 1]:
		time += ringroad[i] - ringroad[i - 1]
	elif ringroad[i] == ringroad[i - 1]:
		continue
	else:
		time += n - ringroad[i - 1] + ringroad[i]
print(time)


I = lambda : map(int, input().split())
(n, m) = I()
a = I()
(ans, cur) = (0, 1)
for x in a:
	ans += (x - cur, n - cur + x)[cur > x]
	cur = x
print(ans)


(n, m) = map(int, input().split())
v = [int(i) for i in input().split()]
(t, h) = (v[0] - 1, v[0])
for i in v[1:]:
	if i > h:
		t = t + i - h
		h = i
	elif i < h:
		t = t + n - h + i
		h = i
print(t)


(a, b) = map(int, input().split())
tasks = list(map(int, input().split()))
steps = 0
tasks = [1] + tasks
for i in range(1, b + 1):
	if tasks[i] >= tasks[i - 1]:
		steps += tasks[i] - tasks[i - 1]
	else:
		steps += tasks[i] - tasks[i - 1] + a
print(steps)


(n, k) = map(int, input().split())
aa = [int(x) for x in input().split()]
bb = aa[0] - 1
for i in range(k - 1):
	if aa[i] <= aa[i + 1]:
		bb = bb + aa[i + 1] - aa[i]
	else:
		bb = bb + n - aa[i] + aa[i + 1]
print(bb)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
prev = 1
ans = 0
for x in a:
	ans += (x + n - prev) % n
	prev = x
print(ans)


def _339B(n, m, a):
	time = 0
	a.insert(0, 1)
	for index in range(1, m + 1):
		difference = a[index] - a[index - 1]
		if difference >= 0:
			time += difference
		else:
			time += n + difference
	return time
(n, m) = list(map(lambda x: int(x), input().split()))
a = list(map(lambda x: int(x), input().split()))
print(_339B(n, m, a))


def calc(a, pos, n):
	if a[pos + 1] >= a[pos]:
		g = a[pos + 1] - a[pos]
	else:
		g = n + (a[pos + 1] - a[pos])
	return g
num = str(input())
num = [int(j) for j in num.split()]
n = num[0]
m = num[1]
a = str(input())
a = [int(j) for j in a.split()]
a.insert(0, 1)
l = len(a)
count = 0
for i in range(l - 1):
	count = count + calc(a, i, n)
print(count)


(n, m) = map(int, input().strip().split(' '))
tasks = [int(m) for m in input().split(' ')]
time = tasks[0] - 1
for i in range(0, len(tasks) - 1):
	if tasks[i + 1] < tasks[i]:
		time += n - abs(tasks[i + 1] - tasks[i])
	else:
		time += tasks[i + 1] - tasks[i]
print(time)


(a, b) = input().split(' ')
list = []
a = int(a)
b = int(b)
d = input()
str = ''
counter = 0
start = 1
cnt = 0
for i in range(len(d)):
	if d[i] != ' ':
		str = str + d[i]
	else:
		list.append(int(str))
		str = ''
	if i == len(d) - 1:
		list.append(int(str))
for i in range(len(list)):
	if i == 0:
		counter += list[i] - start
	elif list[i] >= list[i - 1]:
		counter += list[i] - list[i - 1]
	else:
		counter += a - list[i - 1]
		cnt += 1
		counter += list[i] - start
counter += cnt
print(counter)


(n, m) = map(int, input().split())
a = input().split()
for i in range(m):
	a[i] = int(a[i])
count = 0
count += a[0] - 1
for i in range(m - 1):
	if a[i + 1] == a[i]:
		count += 0
	if a[i + 1] > a[i]:
		count += a[i + 1] - a[i]
	if a[i + 1] < a[i]:
		count += n - a[i] + a[i + 1]
print(count)


(n, m) = map(int, input().split())
x = list(map(int, input().split()))
start = 1
t = 0
for i in x:
	t += i - start
	if i - start < 0:
		t += n
	start = i
print(t)


(nhouse, nchore) = map(int, input().split(' '))
chores = list(map(int, input().split(' ')))
pos = 1
time = 0
for i in range(nchore):
	dest = chores[i]
	if dest >= pos:
		time += dest - pos
	else:
		time += nhouse + dest - pos
	pos = dest
print(time)


def gint():
	return int(input())

def gint_arr():
	return list(map(int, input().split()))

def gfloat():
	return float(input())

def gfloat_arr():
	return list(map(float, input().split()))

def pair_int():
	return map(int, input().split())
INF = 1 << 31
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
(n, m) = gint_arr()
a = gint_arr()
pos = 1
res = 0
for i in a:
	if pos <= i:
		res += i - pos
	else:
		res += i + (n - pos)
	pos = i
print(res)


(n, m) = map(int, input().split())
lis = []
[lis.append(int(i)) for i in input().split()]
num = lis[0] - 1
for i in range(len(lis) - 1):
	if lis[i] <= lis[i + 1]:
		num += lis[i + 1] - lis[i]
	else:
		num += n - lis[i] + lis[i + 1]
print(num)


(n, m) = map(int, input().split())
li = [int(x) for x in input().split()]
pre_pos = 1
count = 0
for i in li:
	if i >= pre_pos:
		count = count + i - pre_pos
		pre_pos = i
	else:
		count = count + n + 1 - pre_pos + i - 1
		pre_pos = i
print(count)


(n, m) = input().split()
(n, m) = (int(n), int(m))
s = input()
lst = s.split()
for i in range(len(lst)):
	lst[i] = int(lst[i])
p = 1
s = 0
for i in range(len(lst)):
	if lst[i] >= p:
		s = s + lst[i] - p
	elif lst[i] < p:
		s = s + n - (p - lst[i])
	p = lst[i]
print(s)


def hi(n, l):
	time = l[0] - 1
	for i in range(1, len(l)):
		if l[i - 1] > l[i]:
			c = n - l[i - 1] + l[i]
			time += c
		else:
			d = l[i] - l[i - 1]
			time += d
	return time
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
print(hi(n, l))


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
s = 0
for i in range(1, m):
	if A[i] < A[i - 1]:
		s = s + 1
	else:
		pass
print(n * s + A[m - 1] - 1)


s = input().split()
n = int(s[0])
m = int(s[1])
s = input().split()
for i in range(len(s)):
	s[i] = int(s[i])
time = 0
cur = 1
for i in s:
	time += (i + n - cur) % n
	cur = i
print(time)


(n, m) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.insert(0, 1)
out = []
for i in range(1, m + 1):
	if A[i] >= A[i - 1]:
		out.append(A[i] - A[i - 1])
	else:
		out.append(n + A[i] - A[i - 1])
print(sum(out))


import sys
parametrs = list(map(int, input().split()))
count_homes = parametrs[0]
count_tasks = parametrs[1]
tasks = list(map(int, input().split()))
last_step = 1
count_steps = 0
for task in tasks:
	if last_step == task:
		count_steps += 0
	if last_step > task:
		count_steps += count_homes - last_step + task
	if last_step < task:
		count_steps += task - last_step
	last_step = task
print(count_steps)


import sys
import math
import collections
from collections import deque
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))
T = 1
for _ in range(T):
	(n, m) = list(map(int, stdin.readline().split()))
	a = list(map(int, stdin.readline().split()))
	a = [1] + a
	t = 0
	cp = 1
	for i in range(1, m + 1):
		if a[i] >= a[i - 1]:
			t += a[i] - cp
			cp = a[i]
		else:
			t += n - a[i - 1] + 1 + (a[i] - 1)
			cp = a[i]
	print(t)


(n, m) = [int(x) for x in input().split(' ')]
arr = [int(x) for x in input().split(' ')]
newNum = 0
prevNum = 0
time = -1
for i in arr:
	if i >= prevNum:
		time += i - prevNum
		prevNum = i
	else:
		time += n - prevNum
		time += i
		prevNum = i
print(time)


list = [int(i) for i in input().split()]
n = list[0]
m = list[1]
tasks = [int(i) for i in input().split()]
result = 0
for i in range(1, m):
	if tasks[i] < tasks[i - 1]:
		result += n
result += tasks[-1] - 1
print(result)


(n, m) = map(int, input().split())
p = [int(i) for i in input().split()]
s = p[0] - 1
for i in range(m - 1):
	if p[i + 1] >= p[i]:
		s += p[i + 1] - p[i]
	else:
		s += n - p[i] + p[i + 1]
print(s)


(n, m) = list(map(int, input().split()))
city = list(map(int, input().split()))
r = 0
i = 1
while len(city) > 0:
	if i < city[0]:
		r += city[0] - i
		i = city[0]
		city.pop(0)
	else:
		if i != city[0]:
			r += n - i + city[0]
			i = city[0]
		city.pop(0)
	if len(city) == 0:
		break
print(r)


(n, m) = [int(x) for x in input().split()]
task = [int(x) for x in input().split()]
time = int(task[0]) - 1
for i in range(m - 1):
	if task[i] <= task[i + 1]:
		time += task[i + 1] - task[i]
	else:
		time += n - (task[i] - task[i + 1])
print(time)


def main():
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	i = 0
	step = 0
	position = 1
	a = list(map(int, input().split()))
	while i < m:
		if position <= a[i]:
			step += a[i] - position
		elif position > a[i]:
			step += n + a[i] - position
		position = a[i]
		i = i + 1
	print(step)
main()


(n, m) = map(int, input().split(' '))
li = list(map(int, input().split(' ', m)[:m]))
cnt = li[0] - 1
ptr = li[0]
for i in range(1, m):
	if li[i] < li[i - 1]:
		cnt += n - ptr + 1
		ptr = li[i]
		cnt += ptr - 1
	else:
		cnt += li[i] - ptr
		ptr = li[i]
print(cnt)


(n, m) = map(int, input().split())
things = list(map(int, input().split()))
cur = 1
times = 0
for i in range(m):
	if things[i] < cur:
		times += n - cur + things[i]
	else:
		times += things[i] - cur
	cur = things[i]
print(times)


(n, k) = list(map(int, input().split()))
ar = list(map(int, input().split()))
sum = ar[0] - 1
for i in range(1, k):
	a = n - ar[i - 1]
	b = ar[i]
	c = (a + b) % n
	sum = sum + c
print(sum)


[n, m] = [int(i) for i in input().split()]
tasks = [int(i) for i in input().split()]
a = tasks[0] - 1
for i in range(1, m):
	if tasks[i] < tasks[i - 1]:
		a += n - tasks[i - 1] + tasks[i]
	elif tasks[i] >= tasks[i - 1]:
		a += tasks[i] - tasks[i - 1]
print(a)


(n, m) = map(int, input().split())
li = list(map(int, input().split()))
dis = li[0] - 1
for i in range(len(li) - 1):
	if li[i] > li[i + 1]:
		dis += n - (li[i] - li[i + 1])
	else:
		dis += li[i + 1] - li[i]
print(dis)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
so = 0
p = 1
for bb in b:
	k = (bb - p) % a[0]
	so += k
	p = bb
print(so)


inpt = [int(i) for i in input().split()]
val = [int(m) for m in input().split()]
sum = val[0] - 1
for s in range(len(val) - 1):
	if val[s] <= val[s + 1]:
		sum = sum + (val[s + 1] - val[s])
	else:
		sum = sum + (inpt[0] - val[s]) + val[s + 1]
print(sum)


(n, _) = map(int, input().split())
a = list(map(int, input().split()))
result = sum((n for i in range(len(a) - 1) if a[i] > a[i + 1])) + a[-1] - 1
print(result)


(m, n) = input().split()
m = int(m)
n = int(n)
ls = input().split()
count = 0
ls = [int(i) for i in ls]
for i in range(n - 1):
	if ls[i + 1] < ls[i]:
		count += m
count += ls[-1]
print(count - 1)


(n, m) = map(int, input().split())
s = [int(s) for s in input().split()]
c = 0
t = 1
for i in range(m):
	if t <= s[i]:
		c = c + s[i] - t
		t = s[i]
	else:
		c = c + s[i] + n - t
		t = s[i]
print(c)


(n, m) = map(int, input().split())
t = 1
task = list(map(int, input().split()))
l = task[0]
while t < m:
	if task[t] < task[t - 1]:
		l = l + n + task[t] - task[t - 1]
	if task[t] >= task[t - 1]:
		l = l + task[t] - task[t - 1]
	t = t + 1
print(l - 1)


(n, t) = map(int, input().split())
sp = list(map(int, input().split()))
k = 0
if len(sp) > 1:
	for i in range(1, t):
		if sp[i - 1] > sp[i]:
			k += 1
	print(k * n + sp[i] - 1)
else:
	print(sp[0] - 1)


mas = input().split()
(n, m) = (int(mas[0]), int(mas[1]))
mas = ['1']
mas += input().split()

def time_dif(beg=1, end=2, n_max=3):
	if beg <= end:
		return end - beg
	else:
		return n_max - beg + 1 + time_dif(1, end, n_max)
time = 0
for i in range(m):
	time += time_dif(int(mas[i]), int(mas[i + 1]), n)
print(time)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
total = 0
last = 1
for i in a:
	total += (n + i - last) % n
	last = i
print(total)


(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
(x, ans) = (1, 0)
for i in a:
	if i >= x:
		ans += i - x
	else:
		ans += n - x + i
	x = i
print(ans)


(n, m) = map(int, input().split())
houses = list(map(int, input().split()))
position = houses[0]
time = houses[0] - 1
for i in range(1, m):
	if houses[i] > position:
		time += houses[i] - position
		position = houses[i]
	elif houses[i] == position:
		pass
	else:
		time += n - (position - houses[i])
		position = houses[i]
print(time)


nm = input().split(' ')
n = int(nm[0])
m = int(nm[1])
a = list(map(int, input().split(' ')))
lasttask = a[-1]
c = 0
for i in range(1, len(a)):
	if a[i - 1] > a[i]:
		c += 1
print(a[-1] - 1 + c * n)


(n, m) = map(int, input().split())
i = list(map(int, input().split()))
pointer = 1
count = 0
for value in i:
	if pointer < value:
		count += value - pointer
	if pointer > value:
		count += n - pointer + 1
		pointer = 1
		count += value - pointer
	pointer = value
print(count)


(n, k) = list(map(int, input().split()))
h = 1
t = []
a = list(map(int, input().split()))
for i in a:
	t.append(i - h if i >= h else n + i - h)
	h = i
print(sum(t))


(n, m) = input().split()
n = int(n)
m = int(m)
a = input().split()
for i in range(m):
	a[i] = int(a[i])
cur = 1
move = 0
for i in range(m):
	if cur > a[i]:
		move += n - cur + a[i]
		cur = a[i]
	elif cur < a[i]:
		move += a[i] - cur
		cur = a[i]
print(move)


(n, m) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
x = 1
y = a[0] - 1
while x < m:
	if a[x] > a[x - 1]:
		y = y + a[x] - a[x - 1]
	elif a[x] == a[x - 1]:
		pass
	else:
		y += a[x] + (n - a[x - 1])
	x += 1
print(y)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
point = 1
steps = 0
for i in range(len(l)):
	if l[i] > point:
		steps = steps + l[i] - point
	elif point > l[i]:
		steps = steps + n - point + l[i]
	point = l[i]
print(steps)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
p = 1
c = 0
for ai in a:
	if p <= ai:
		c += ai - p
	else:
		c += n - p
		c += ai
	p = ai
print(c)


(n, m) = map(int, input().split())
j = [int(i) for i in input().split()]
k = j[0] - 1
for i in range(m - 1):
	if j[i] <= j[i + 1]:
		k = k + j[i + 1] - j[i]
	else:
		k = k + n - j[i] + j[i + 1]
print(k)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
xenia = 1
time = 0
for task in a:
	if xenia <= task:
		time += task - xenia
	else:
		time += task + n - xenia
	xenia = task
print(time)


(n, m) = map(int, input().split())
input_list = list(map(int, input().split()))
input_list.insert(0, 1)
time_ans = 0
for i in range(1, m + 1):
	if input_list[i] >= input_list[i - 1]:
		time_ans += input_list[i] - input_list[i - 1]
	else:
		time_ans += n - input_list[i - 1] + 1
		time_ans += input_list[i] - 1
print(time_ans)


inp = lambda : map(int, input().split())
(n, k) = inp()
l = list(inp())
time = 0
for i in range(k):
	if i == 0:
		time = time + l[i] - 1
	if i != 0 and l[i - 1] > l[i]:
		time += n - l[i - 1] + l[i]
		continue
	elif i != 0 and l[i - 1] < l[i]:
		time = time + l[i] - l[i - 1]
	else:
		pass
print(time)


(n, k) = [int(x) for x in input().split()]
d = [int(x) for x in input().split()]
res = 0
last = 1
for x in d:
	if x >= last:
		res += x - last
	else:
		res += n - last + x
	last = x
print(res)


(n, m) = [int(j) for j in input().split()]
lst = [int(k) for k in input().split()]
count = lst[0] - 1
if m == 1:
	print(count)
else:
	for i in range(1, m):
		if lst[i] >= lst[i - 1]:
			count += lst[i] - lst[i - 1]
		else:
			count += lst[i] + n - lst[i - 1]
	print(count)


import sys
import os

def ad1():
	x = input()
	nums = [int(n) for n in x.split(' ')]
	y = nums[1]
	x = nums[0]
	stri = input()
	stri1 = [int(n) for n in stri.split(' ')]
	pos = stri1[0] - 1
	i = 0
	while i < y - 1:
		if stri1[i + 1] >= stri1[i]:
			pos += stri1[i + 1] - stri1[i]
		else:
			pos += x + stri1[i + 1] - stri1[i]
		i += 1
	print(pos)
ad1()


a = [int(x) for x in input().split()]
house = a[0]
things = a[1]
b = [int(y) for y in input().split()]
time = 0
start = 1
for i in range(0, things):
	if b[i] >= start:
		time = time + b[i] - start
		start = b[i]
	else:
		time = time + house - (start - b[i])
		start = b[i]
print(time)


(n, m) = [int(x) for x in input().split()]
ipt = [int(x) - 1 for x in input().split()]
pointer = 1
time = ipt[0]
last_home = ipt[0]
while pointer < len(ipt):
	if ipt[pointer] < last_home:
		time = time + (n - last_home) + ipt[pointer]
	elif ipt[pointer] > last_home:
		time = time + ipt[pointer] - last_home
	last_home = ipt[pointer]
	pointer += 1
print(time)


(n, m) = map(int, input().split())
ar = list(map(int, input().split()))
curr_task = 0
ans = 0
for i in range(m):
	if curr_task <= ar[i]:
		ans += ar[i] - curr_task
		curr_task = ar[i]
	else:
		ans += n - curr_task + ar[i]
		curr_task = ar[i]
print(ans - 1)


(n, m) = map(int, input().split())
lis = [int(x) for x in input().split()]
cnt = 1
for i in range(m - 1):
	if lis[i] > lis[i + 1]:
		cnt += 1
print(n * (cnt - 1) + lis[-1] - 1)


count = 0
index = 1
(n, m) = map(int, input().split())
mt = list(map(int, input().split()))
for obj in mt:
	if obj > index:
		count += obj - index
		index = obj
	if index > obj:
		count += n - index + obj
		index = obj
	if index == object:
		continue
print(count)


(n, m) = input().split()
(n, m) = [int(n), int(m)]
a = [int(i) for i in input().split()]
c = 0
ip = 1
j = 0
while j < m:
	if a[j] >= ip:
		c += a[j] - ip
		ip = a[j]
	else:
		c += n - ip + a[j]
		ip = a[j]
	j += 1
print(c)


(n, numList, curPos, res) = (int(input().split()[0]), map(int, input().split()), 1, 0)
for i in numList:
	if i > curPos:
		res += i - curPos
	elif i < curPos:
		res += i + n - curPos
	curPos = i
print(res)


def main():
	(a, b) = map(int, input().split())
	task = list(map(int, input().split()))
	hours = []
	initial = 1
	pos = 1
	final = a
	for i in range(b):
		if pos < task[i]:
			hours.append(task[i] - pos)
			pos = task[i]
		elif pos == task[i]:
			pass
		elif pos > task[i]:
			hours.append(final - pos + 1 + (task[i] - initial))
			pos = task[i]
	print(sum(hours))
main()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
t = []
t.append(a[0] - 1)
for i in range(1, m):
	if a[i] >= a[i - 1]:
		t.append(a[i] - a[i - 1])
	else:
		ti = n - a[i - 1] + 1 + (a[i] - 1)
		t.append(ti)
print(sum(t))


first = list(input().split())
n_house = int(first[0])
task = list(map(int, list(input().split())))
house = []
for i in range(1, n_house + 1):
	house.append(i)
curr_index = 0
time = 0
for i in task:
	house_ind = i - 1
	if house_ind >= curr_index:
		time += house_ind - curr_index
	else:
		time += len(house) - curr_index + house_ind
	curr_index = house_ind
print(time)


(houseNum, taskHouse) = map(int, input().split())
initPos = 1
tasks = list(map(int, input().split()))
move = 0
for i in range(taskHouse):
	if initPos == tasks[i]:
		continue
	elif initPos > tasks[i]:
		move = move + (houseNum - initPos) + tasks[i]
		initPos = tasks[i]
	else:
		move = move + (tasks[i] - initPos)
		initPos = tasks[i]
print(move)


line = input().split()
(n, m) = (int(line[0]), int(line[1]))
targets = [int(i) for i in input().split()]
current = 1
moves = 0
for i in targets:
	moves += i - current if i >= current else i + n - current
	current = i
print(moves)


def mi():
	return map(int, input().split())

def li():
	return list(mi())
(n, m) = mi()
a = li()
c = a[0] - 1
for i in range(1, m):
	if a[i] < a[i - 1]:
		c += n - (a[i - 1] - a[i])
	else:
		c += a[i] - a[i - 1]
print(c)


(n, m) = map(int, input().split())
s = [int(x) for x in input().split()]
summ = 0
i = 1
for x in s:
	if x > i:
		summ += x - i
		i = x
	elif x == i:
		continue
	else:
		summ += n - i + x
		i = x
print(summ)


(n, m) = map(int, input().split())
l = [1]
l.extend(list(map(int, input().split())))
time = 0
for num in range(1, m + 1):
	if l[num] >= l[num - 1]:
		time += abs(l[num] - l[num - 1])
	else:
		time += abs(l[num - 1] - (n + 1)) + abs(l[num] - 1)
print(time)


(n, m) = [int(i) for i in input().split()]
a = [int(j) for j in input().split()]
b = [a[0] - 1]
for i in range(0, m - 1):
	b.append(a[i + 1] - a[i])
output = 0
for j in range(0, m):
	if b[j] >= 0:
		output += b[j]
	else:
		output += n + b[j]
print(output)


(n, m) = [int(x) for x in input().split()]
L = input().split()
a = 0
if L.count('1') != m:
	a = int(L[0]) - 1
	for i in range(0, m - 1):
		if int(L[i]) < int(L[i + 1]):
			a = a + int(L[i + 1]) - int(L[i])
		elif int(L[i]) > int(L[i + 1]):
			a = a + n - int(L[i]) + int(L[i + 1])
print(a)


def time(start, dest, n):
	if start < dest:
		t = dest - start
	elif start > dest:
		t = n - start + 1
		start = 1
		t = t + dest - start
	else:
		t = 0
	return t
g = input()
(N, M) = [int(x) for x in g.split()]
task = input()
listing = []
for x in task.split():
	listing.append(int(x))
now = 1
result = 0
for m in listing:
	destination = m
	l = time(now, destination, N)
	result = result + l
	now = destination
print(result)


x = input()
(n, m) = (int(x.split()[0]), int(x.split()[1]))
y = input()
values = [int(i) for i in y.split()]
count = 0
current = 1
for i in values:
	if i >= current:
		count += i - current
		current = i
	else:
		count += n - current + i
		current = i
print(count)


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
c = 0
t = 1
for i in range(m):
	x = A[i] - t
	x = x % n
	c += x
	t += x
print(c)


import sys
import math
input = sys.stdin.readline

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()

def lower_letters():
	lowercase = []
	for i in range(97, 97 + 26):
		lowercase.append(chr(i))
	return lowercase

def upper_letters():
	uppercase = []
	for i in range(65, 65 + 26):
		uppercase.append(chr(i))
	return uppercase
(n, m) = int_array()
arr = int_array()
sumi = 0
for i in range(len(arr)):
	if i > 0:
		if arr[i - 1] > arr[i]:
			sumi += n - arr[i - 1] + arr[i]
		if arr[i - 1] < arr[i]:
			sumi += arr[i] - arr[i - 1]
	else:
		sumi += arr[i]
print(sumi - 1)


def main():
	(n, m) = [int(i) for i in input().split()]
	count = 0
	last = 1
	for i in input().split():
		k = int(i)
		if k >= last:
			count += k - last
		else:
			count += n - last + k
		last = k
	print(count)
	return 0
main()


(n, m) = [int(x) for x in input().split()]
L = [int(x) for x in input().split()]
s = 1
c = 0
for i in L:
	if s != i and i < s:
		c += n - s + i
	elif s != i and i > s:
		c += i - s
	s = i
print(c)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
curpos = 1
res = 0
for i in l:
	if curpos <= i:
		k = i - curpos
		res += k
		curpos = i
	else:
		f = n - curpos + 1
		k = f + i - 1
		res += k
		curpos = i
print(res)


(n, _) = [int(x) for x in input().strip().split()]
(list_h, cnt) = ([int(x) for x in input().strip().split()], 0)
for x in range(len(list_h) - 1):
	if list_h[x] > list_h[x + 1]:
		cnt += 1
print(cnt * n + list_h[-1] - 1)


(n, m) = list(map(int, input().split()))
l = list(map(int, input().split()))
time = 0
i = 1
while m:
	if l[0] == i:
		m -= 1
		l.remove(l[0])
	elif l[0] < i:
		time += n - i + l[0]
		i = l[0]
	else:
		time += l[0] - i
		i = l[0]
print(time)


import math
(n, m) = [int(i) for i in input().split()]
task = [int(i) for i in input().split()]
step = int(math.fabs(task[0] - 1))
for i in range(1, m):
	x = task[i] - task[i - 1]
	if x >= 0:
		step += x
	if x < 0:
		step += n + x
print(step)


(s, t) = (1, 0)
l = input().split(' ')
(n, m) = (int(l[0]), int(l[1]))
l = input().split(' ')
for i in range(m):
	l[i] = int(l[i])
for i in range(m):
	if l[i] > s:
		t += l[i] - s
		s = l[i]
	elif l[i] < s:
		t += n - s + l[i]
		s = l[i]
print(t)


[n, m] = [int(i) for i in input().split(' ')]
a = [int(j) for j in input().split(' ')]
dist = 1
count = 0
for k in range(m):
	if dist < a[k]:
		temp = a[k]
		count += a[k] - dist
		dist = temp
	elif dist > a[k]:
		sub = a[k]
		count += n - (dist - a[k])
		dist = sub
print(count)


from sys import stdin as inp
from sys import stdout as out
from sys import maxsize as max
(n, m) = list(map(int, inp.readline().strip().split()))
a = list(map(int, inp.readline().strip().split()))
(t, c) = (0, 1)
for i in a:
	if i - c < 0:
		t += i - c + n
	else:
		t += i - c
	c = i
out.write(str(t))


(n, m) = [int(x) for x in input().split()]
ls = [int(x) for x in input().split()]
cnt = ls[0] - 1
for i in range(1, m):
	if ls[i] < ls[i - 1]:
		cnt += n - ls[i - 1] + ls[i]
	elif ls[i] > ls[i - 1]:
		cnt += ls[i] - ls[i - 1]
print(cnt)


def movea(n):

	def move(loc, task):
		ans = task - loc
		if ans >= 0:
			return ans
		else:
			return n + ans
	return move
n = int(input().split()[0])
move = movea(n)
a = [int(i) for i in input().split()]
loc = 1
ans = 0
while a:
	task = a.pop(0)
	ans += move(loc, task)
	loc = task
print(ans)


(n, m) = [int(s) for s in input().split()]
mass = [int(s) for s in input().split()]
current_house = 1
actions = 0
for i in range(len(mass)):
	if current_house == mass[i]:
		actions += 0
	elif current_house < mass[i]:
		actions += mass[i] - current_house
		current_house = mass[i]
	else:
		actions += n - current_house + mass[i]
		current_house = mass[i]
print(actions)


(a, b) = [int(i) for i in input().split()]
m = [int(i) for i in input().split()]
x = 0
for i in range(b - 1):
	if m[i + 1] < m[i]:
		x += 1
p = a * x + m.pop() - 1
print(p)


(m, n) = [int(i) for i in input().split()]
list = [int(i) for i in input().split()]
t = 0
c = 0
while t < n - 1:
	if list[t] > list[t + 1]:
		c = c + m
	t = t + 1
c = c + list[-1] - 1
print(c)


(n, m) = [int(i) for i in input().split()]
line = [int(i) for i in input().split()]
way = line[0] - 1
for i in range(m - 1):
	if line[i] <= line[i + 1]:
		way += line[i + 1] - line[i]
	else:
		way += n + line[i + 1] - line[i]
print(way)


(n, m) = map(int, input().split())
A = input().split()
tasks = []
for i in range(m):
	tasks.append(int(A[i]))
T = 0
location = 1
while len(tasks) >= 2:
	T = T + tasks[0] - location
	location = tasks[0]
	if tasks[1] < tasks[0]:
		T = T + n - tasks[0] + 1
		location = 1
	del tasks[0]
T = tasks[0] - location + T
print(T)


(n, m) = map(int, input().split(' '))
pos = 1
result_time = 0
temp_tasks = list(map(int, input().split(' ')))
for temp_task in temp_tasks:
	if temp_task < pos:
		result_time += n - pos + temp_task
	else:
		result_time += temp_task - pos
	pos = temp_task
print(result_time)


(n, m) = map(int, input().split())
l1 = list(map(int, input().split()))
w = [1]
l = w + l1
s = 0
s1 = 0
for i in range(0, m):
	if l[i] <= l[i + 1]:
		s = s + (l[i + 1] - l[i])
	else:
		s1 = s1 + (n - l[i]) + l[i + 1]
print(s + s1)


(n, m) = map(int, input().split())
last = 1
ans = 0
for x in input().split():
	cur = int(x)
	ans += (cur - last) % n
	last = cur
print(ans)


(n, d) = input().split()
n = int(n)
d = int(d)
a = list(map(int, input().split()))
c = 0
cu = 1
for i in range(d):
	if a[i] >= cu:
		c += a[i] - cu
	else:
		c += (a[i] - cu) % n
	cu = a[i]
print(c)


(n, m) = input().split()
a = []
temp = input().split()
for i in range(int(m)):
	a.append(int(temp[i]))
answer = 0
current_index = 1
for i in range(int(m)):
	if a[i] >= current_index:
		answer += a[i] - current_index
	else:
		answer += int(n) - current_index + a[i]
	current_index = a[i]
print(answer)


(n, m) = map(int, input().split())
ml = [1] + list(map(int, input().split()))
c = 0
t = lambda x, y: n - x + y if x > y else y - x
for i in range(m):
	c += t(ml[i], ml[i + 1])
print(c)


s = input()
n = int(s.split()[0])
m = int(s.split()[1])
loc = 1
ans = 0
s = input().split()
for i in range(0, m):
	now = int(s[i])
	if now >= loc:
		ans = ans + (now - loc)
	else:
		ans = ans + (n + now - loc)
	loc = now
print(ans)


p = [i for i in input().split()]
n = int(p[0])
l = [i for i in input().split()]
q = 0
for i in range(int(len(l)) - 1):
	if int(l[i]) > int(l[i + 1]):
		q = q + 1
	else:
		q = q
s = n * q + int(l[-1]) - 1
print(s)


from sys import stdin, stdout
x = lambda : list(map(int, stdin.readline().strip().split()))
(n, m) = x()
arr = x()
start = 1
ans = 0
for i in arr:
	if i < start:
		ans = ans + n - start + i
		start = i
	else:
		ans = ans + i - start
		start = i
print(ans)


a = list(map(int, input().split()))
a1 = a[0]
a2 = a[1]
b = list(map(int, input().split()))
c = 0
d = 1
for i in range(a2 - 1):
	if b[i + 1] < b[i]:
		c += b[i] - d
		d = b[i] - a1
c += b[-1] - d
print(c)


(n, m) = map(int, input().split())
place = list(map(int, input().split()))
pace = 0
pace += place[0] - 1
for i in range(0, len(place) - 1):
	if not place[i] > place[i + 1]:
		pace += place[i + 1] - place[i]
	if place[i] > place[i + 1]:
		pace += n - place[i] + place[i + 1]
print(pace)


def ms(x, y):
	if y <= x:
		return x - y
	return x + n - y
(n, m) = [int(x) for x in input().split()]
d = [int(x) for x in input().split()]
a = 0
c = 1
for x in d:
	a += ms(x, c)
	c = x
print(a)


(n, m) = map(int, input().split())
tasks = [int(t) for t in input().split()]
step = 0
current_house = 1
for t in tasks:
	if t == current_house:
		step += 0
	elif t > current_house:
		step += t - current_house
		current_house = t
	else:
		step += n - current_house + t
		current_house = t
print(step)


__author__ = 'Bian'
(n, m) = [int(x) for x in input().split()]
i = 1
time = 0
task = [int(x) for x in input().split()]

def go(x, y):
	global time, n, i
	if y == x:
		return
	elif y > x:
		i = y
		time += y - x
	else:
		i = y
		time += y + n - x
for j in task:
	go(i, j)
print(time)


import sys
(n, m) = input().split(' ')
dos = input().split(' ')
current = 1
result = 0
circle = range(1, int(n))
for do in dos:
	tmp = int(do) - current
	if tmp < 0:
		result += int(n) + tmp
	else:
		result += tmp
	current = int(do)
print(result)


def solve(arr, n, m):
	curr = arr[0]
	time = curr - 1
	for i in range(1, m):
		job = arr[i]
		if job < curr:
			time += n - curr + job
		else:
			time += job - curr
		curr = job
	return time
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
print(solve(arr, n, m))


a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
present = b[0]
count = b[0] - 1
del b[0]
for i in b:
	if i >= present:
		count = count + (i - present)
	else:
		count = count + (a[0] - present) + i
	present = i
print(count)


l = list(map(int, input().split()))
t = list(map(int, input().split()))
ans = t[0] - 1
for i in range(1, len(t)):
	if t[i - 1] > t[i]:
		ans += l[0] - t[i - 1] + (t[i] - 1) + 1
		continue
	ans += t[i] - t[i - 1]
print(ans)


x = input().split()
a = int(x[0])
b = int(x[1])
l = [int(i) for i in input().split()]
l.insert(0, 1)
t = 0
for i in range(b):
	if l[i + 1] < l[i]:
		t += a - l[i] + l[i + 1]
	elif l[i + 1] == l[i]:
		t += 0
	elif l[i] < l[i + 1] <= a:
		t += l[i + 1] - l[i]
print(t)


import math
(n, o) = map(int, input().split())
lst = [int(x) for x in input().split()]
curr = 1
opr = 0
mid = math.ceil(n // 2)
for x in lst:
	if x >= curr:
		opr += x - curr
	elif mid < x:
		if curr <= mid:
			opr += curr - x
		else:
			opr += n - curr + x
	elif curr <= x:
		opr += x - curr
	else:
		opr += n - curr + x
	curr = x
print(opr)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.insert(0, 1)
print(sum([(a[i] - a[i - 1] + n) % n for i in range(1, len(a))]))


(n, m) = map(int, input().split())
time = 0
house = 1
for x in map(int, input().split()):
	time += [n - house + x, x - house][house <= x]
	house = x
print(time)


(n, k) = input().split()
ls = list(map(int, input().split()))
pos = 1
ans = 0
for i in range(int(k)):
	if pos > ls[i]:
		ans = ans + int(n) - pos + ls[i]
		pos = ls[i]
	elif pos == ls[i]:
		continue
	else:
		ans = ans + ls[i] - pos
		pos = ls[i]
print(ans)


string = input().split()
n = int(string[0])
m = int(string[1])
a = list(map(lambda x: int(x), input().split()))
ans = 0
pos = 1
for ii in a:
	if ii >= pos and ii <= n:
		ans += ii - pos
	else:
		ans += n - pos + ii
	pos = ii
print(ans)


inp1 = str(input())
inp = inp1.split()
n = int(inp[0])
m = int(inp[1])
arr = list(map(int, input().split()))
count = arr[0] - 1
for i in range(1, m):
	if arr[i] >= arr[i - 1]:
		count += arr[i] - arr[i - 1]
	else:
		a = n - arr[i - 1]
		count += a + arr[i]
print(count)


temp = list(map(int, input().rstrip().split()))
n = temp[0]
m = temp[1]
a = list(map(int, input().rstrip().split()))
prev = 1
total = 0
for i in a:
	diff = i - prev
	if diff < 0:
		diff = n - prev + i
	total += diff
	prev = i
print(total)


(a, b) = map(int, input().split())
c = list(map(int, input().split()))
count = c[0] - 1
for i in range(0, b - 1):
	if c[i + 1] >= c[i]:
		d = c[i + 1] - c[i]
	else:
		d = c[i + 1] + a - c[i]
	count += d
print(count)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
total_time = tasks[0] - 1
curr = tasks[0]
for i in range(1, m):
	if tasks[i] < curr:
		total_time += n - curr + tasks[i]
	elif tasks[i] == curr:
		total_time += 0
	else:
		total_time += tasks[i] - curr
	curr = tasks[i]
print(total_time)


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from sys import maxsize
from operator import *
from itertools import *
from collections import Counter
import math
(n, m) = map(int, input().split(' '))
l = list(map(int, input().split(' ')))
c = 0
t = 1
for i in range(m):
	if l[i] >= t:
		c += l[i] - t
		t = l[i]
	else:
		c += n - t + l[i]
		t = l[i]
print(c)


(n, m) = [int(i) for i in input().split()]
line = input().split()
s = int(line[0]) - 1
st = int(line[0])
le = n
for l in line[1:]:
	if int(l) >= st:
		s += int(l) - st
		st = int(l)
	else:
		s += le - st + int(l)
		st = int(l)
print(s)


I = lambda : map(int, input().split())
(n, _) = I()
(a, t) = (1, 0)
for b in I():
	t += n - a + b if a > b else b - a
	a = b
print(t)


(n, m) = map(int, input().split())
c = list(map(int, input().split()))
tot = c[0]
state_now = c[0]
for i in range(1, len(c)):
	if c[i] < c[i - 1]:
		tot += n - c[i - 1] + c[i]
	else:
		tot += c[i] - c[i - 1]
print(tot - 1)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
mv = b[-1]
n = 0
b0 = b[0]
for bb in b[1:]:
	if bb < b0:
		n += 1
	b0 = bb
print(n * a[0] + mv - 1)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
t = 1
cnt = 0
for x in l:
	if t <= x:
		cnt = cnt + x - t
	else:
		cnt = cnt + (n - t + x)
	t = x
print(cnt)


(n, m) = map(int, input().split())
lt = [int(i) for i in input().split()]
x = 0
y = 1
for i in range(m):
	x += [lt[i] - y, n - y + lt[i]][lt[i] < y]
	y = lt[i]
print(x)


nm = input().split(' ')
x = input().split(' ')
j = 1
c = 0
for i in x:
	if int(i) > j:
		c += int(i) - j
		j = int(i)
	if int(i) < j:
		c += int(nm[0]) - (j - int(i))
		j = int(i)
print(c)


(n, m) = input().split()
m = int(m)
n = int(n)
Ar = []
for x in input().split():
	Ar.append(int(x))
Cl = 1
T = 0
for i in Ar:
	t = i - Cl
	if t < 0:
		t += n
	Cl = i
	T += t
print(T)


a = input().split()
n = int(a[0])
m = int(a[1])
b = input().split()
x = 0
while x < len(b):
	b[x] = int(b[x])
	x = x + 1
t = 0
y = 0
z = 1
task = m
nn = n
while task > 0 and z < len(b):
	if b[y] < b[z]:
		if y == 0:
			t = b[0] - 1
		t = t + b[z] - b[y]
		nn = n - b[z] + 1
		y = y + 1
		z = z + 1
	elif b[y] == b[z]:
		y = z
		z = z + 1
	elif b[y] > b[z]:
		t = t + nn + b[z] - 1
		nn = n - b[z] + 1
		y = y + 1
		z = z + 1
	task = task - 1
print(t)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
c = 0
for i in range(k - 1):
	if l[i + 1] < l[i]:
		c += 1
print(c * n + l[-1] - 1)


(n, m) = [int(i) for i in input().split()]
data = [int(i) for i in input().split()]
pos = 1
dist = 0
for x in data:
	if x >= pos:
		dist += x - pos
	else:
		dist += n - pos
		dist += x
	pos = x
print(dist)


str1 = input()
n = int(str1.split()[0])
m = int(str1.split()[1])
str2 = input()
list1 = [int(k) for k in str2.split()]
r = 0
for i in range(1, len(list1)):
	if list1[i] < list1[i - 1]:
		r = r + 1
sum = r * n + list1[-1] - 1
print(sum)


(domov, uloh) = map(int, input().split())
ulohy = list(map(int, input().split()))
pozicia = 1
pohybov = 0
for uloha in ulohy:
	if uloha < pozicia:
		pohybov += domov - pozicia + uloha
	else:
		pohybov += uloha - pozicia
	pozicia = uloha
print(pohybov)


(n, m) = map(int, input().split())
res = 0
num = 1
for e in input().split():
	i = int(e)
	if i >= num:
		res += i - num
	else:
		res += n - num + i
	num = i
print(res)


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
x = 0
t = 0
for i in a:
	if x == 0:
		t += i - 1
	elif i >= a[x - 1]:
		t += i - a[x - 1]
	else:
		t += n - a[x - 1] + i
	x += 1
print(t)


n_m = list(input().split(' '))
takenlijst = list(input().split(' '))
n = int(n_m[0])
m = int(n_m[1])
taak = 0
huis = 1
tijd = 0
while taak < len(takenlijst):
	if int(takenlijst[taak]) == huis:
		taak += 1
	elif int(takenlijst[taak]) > huis:
		tijd += int(takenlijst[taak]) - huis
		huis = int(takenlijst[taak])
		taak += 1
	else:
		tijd += n - huis + int(takenlijst[taak])
		huis = int(takenlijst[taak])
		taak += 1
print(tijd)


(n, m) = map(int, input().split())
taskList = []
lenght = 0
sum = 0
for num in input().split():
	var = int(num)
	if lenght == 0:
		taskList.append(var)
		sum += var - 1
		lenght += 1
	elif taskList[lenght - 1] != var:
		taskList.append(var)
		if var < taskList[lenght - 1]:
			sum += n - taskList[lenght - 1] + var
		else:
			sum += var - taskList[lenght - 1]
		lenght += 1
print(sum)


(n, m) = map(int, input().split())
ais = list(map(int, input().split()))
count = 0
curr = 0
for ai in ais:
	dest = ai - 1
	if dest - curr >= 0:
		count += dest - curr
	else:
		count += n - abs(dest - curr)
	curr = dest
print(count)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007
(n, m) = In()
arr = L()
sm = arr[0] - 1
for i in range(1, m):
	if arr[i] == arr[i - 1]:
		continue
	elif arr[i] < arr[i - 1]:
		sm += n - arr[i - 1] + arr[i]
	else:
		sm += arr[i] - arr[i - 1]
print(sm)


(n, m) = map(int, input().split())
l = [int(x) for x in input().split()]
time = l[0] - 1
i = 1
while i < m:
	if l[i] >= l[i - 1]:
		time += l[i] - l[i - 1]
	else:
		time += n + l[i] - l[i - 1]
	i += 1
print(time)


S = input().split()
n = int(S[0])
m = int(S[1])
a = list(map(int, input().split()))
s = 1
t = 0
for i in range(m):
	if a[i] > s:
		t = t + a[i] - s
		s = a[i]
	elif a[i] < s:
		t = t + n + a[i] - s
		s = a[i]
print(t)


(n, m) = map(int, input().split(' '))
task = list(map(int, input().split(' ')))
h = [i for i in range(0, n)]
t = 0
i = 1
t = task[0]
while i < len(task):
	if task[i] < task[i - 1]:
		a = n - task[i - 1]
		t += a
		t += task[i]
	if task[i] > task[i - 1]:
		t = t + task[i] - task[i - 1]
	i += 1
print(t - 1)


a = input()
b = input()
a = a.split(' ')
b = b.split(' ')
a = [int(i) for i in a]
bb = []
bb.append(1)
for i in b:
	bb.append(int(i))
res = 0
for i in range(len(bb) - 1):
	if bb[i + 1] > bb[i]:
		res += bb[i + 1] - bb[i]
	elif bb[i + 1] < bb[i]:
		res += a[0] + bb[i + 1] - bb[i]
	else:
		res += 0
print(res)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
now = 0
for el in a:
	ans += (n + el - 1 - now) % n
	now = el - 1
print(ans)


(n, ni) = input().split()
n = int(n)
ni = int(ni)
x = input()
A = list(map(int, x.split()))
curr = 1
ans = 0
for i in range(0, ni):
	if A[i] >= curr:
		ans = ans + A[i] - curr
		curr = A[i]
	else:
		ans = ans + n - curr + A[i]
		curr = A[i]
print(ans)


import sys
(n, m) = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
r = 0
l = 1
for i in a:
	if i < l:
		r += n - (l - i)
	else:
		r += i - l
	l = i
print(r)


L1 = [int(i) for i in input().split()]
L2 = [int(i) for i in input().split()]
a = 0
L2.insert(0, 1)
for i in range(len(L2) - 1):
	if L2[i + 1] - L2[i] >= 0:
		a += L2[i + 1] - L2[i]
	else:
		a += L1[0] - abs(L2[i + 1] - L2[i])
print(a)


(n, m) = map(int, input().split())
ai = list(map(int, input().split()))
n_now = 1
count = 0
for k in range(m):
	if ai[k] >= n_now:
		count += ai[k] - n_now
		n_now = ai[k]
	else:
		count += n - (n_now - ai[k])
		n_now = ai[k]
print(count)


(n, m) = [int(i) for i in input().split()]
lst = [int(i) for i in input().split()]
(count, s) = (1, 0)
for i in range(m):
	if lst[i] > count:
		s += lst[i] - count
		count = lst[i]
	if lst[i] < count:
		s += n - count + lst[i]
		count = lst[i]
print(s)


(n, m) = [int(i) for i in input().split()]
location = [int(i) for i in input().split()]
diff = [location[i + 1] - location[i] for i in range(m - 1)]
negative = 0
for i in range(len(diff)):
	if diff[i] < 0:
		negative += 1
print(negative * n + location[-1] - 1)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
circle = 0
for i in range(1, m):
	if a[i] < a[i - 1]:
		circle += 1
total = circle * n + a[m - 1] - 1
print(total)


(n, m) = map(int, input().split())
cost = 0
begin = 1
for house_no in map(int, input().split()):
	cost += (house_no - begin) % n
	begin = house_no
print(cost)


(n, m) = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
houseNo = 1
ans = 0
for e in arr:
	if houseNo <= e:
		ans += e - houseNo
		houseNo = e
	elif houseNo > e:
		ans += n - houseNo + e
		houseNo = e
print(ans)


(n, m) = list(map(int, input().split()))
task = list(map(int, input().split()))
count = 0
cur = 0
for i in task:
	if i - 1 >= cur:
		count += i - 1 - cur
	else:
		count += n - cur + i - 1
	cur = i - 1
print(count)


def main(n, m, lst):
	count = 0
	lst = [1] + lst
	for i in range(1, len(lst)):
		if lst[i] >= lst[i - 1]:
			count += lst[i] - lst[i - 1]
		else:
			count += n - lst[i - 1] + lst[i]
	return count
(n, m) = [int(x) for x in input().split()]
lst = list(map(int, input().split()))
print(main(n, m, lst))


(a, b) = [int(s) for s in input().split()]
n = [int(s) for s in input().split()]
(c, t) = (1, 0)
for i in n:
	t += (a + i - c) % a
	c = i
print(t)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
z = [b[0] - 1]
for i in range(1, len(b)):
	if b[i - 1] > b[i]:
		z.append(a[0] - b[i - 1] + b[i])
	elif b[i - 1] < b[i]:
		z.append(b[i] - b[i - 1])
	else:
		z.append(0)
print(sum(z))


import math
INF = 1000000000.0

def main():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	cnt = a[0] - 1
	for i in range(1, m):
		if a[i] >= a[i - 1]:
			cnt += a[i] - a[i - 1]
		else:
			cnt += n - a[i - 1] + a[i]
	print(cnt)
main()


(n, q) = map(int, input().split(' '))
count = 0
l = list(map(int, input().split(' ')))
count = count + (l[0] - 1)
for j in range(len(l) - 1):
	if l[j] > l[j + 1]:
		count = count + (n - (l[j] - l[j + 1]))
	elif l[j] < l[j + 1]:
		count = count + (l[j + 1] - l[j])
print(count)


(n, m) = list(map(int, input().split()))
(k, r) = (1, 0)
for i in list(map(int, input().split())):
	if k > i:
		r += n - k + i
	elif k < i:
		r += i - k
	k = i
print(r)


(n, q) = map(int, input().split())
l = list(map(int, input().split()))
r = 0
p = 1
for i in l:
	r += (i - p) % n
	p = i
print(r)


(m, n) = map(int, input().split())
l_0 = list(map(int, input().split()))
(t, b) = (0, 0)
while l_0:
	a = l_0.pop(0)
	if a < b:
		t += 1
	b = a
print(t * m + b - 1)


(n, m) = [int(x) for x in input().split(' ')]
l = [int(x) for x in input().split(' ')]
c = 0
for i in range(1, m):
	if l[i] >= l[i - 1]:
		c += l[i] - l[i - 1]
	else:
		c += n - l[i - 1] + l[i]
print(c + l[0] - 1)


import math
import os
import random
import re
import sys
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
s = l[0] - 1
for i in range(1, m):
	if l[i] >= l[i - 1]:
		s += l[i] - l[i - 1]
	else:
		s += n - l[i - 1] + (l[i] - 1) + 1
print(s)


(n, m) = [int(s) for s in input().split()]
ar = [int(k) for k in input().split()]
(pos, count) = (1, 0)
for i in ar:
	if i >= pos:
		count += i - pos
		pos = i
	else:
		count += n - pos + i
		pos = i
print(count)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
time = a[0] - 1
if len(a) == 1:
	print(time)
	exit(0)
for (index, elem) in enumerate(a):
	if a[index] < a[index + 1]:
		time += a[index + 1] - a[index]
	elif a[index] > a[index + 1]:
		time += n - a[index] + a[index + 1]
	if index == m - 2:
		break
print(time)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
time = 0
pre = 1
for i in a:
	if i == pre:
		continue
	if i > pre:
		time += i - pre
	else:
		time += i + n - pre
	pre = i
print(time)


a = int(input().split()[0])
b = list(map(int, input().split()))
c = b[0] - 1
for i in range(1, len(b)):
	c += b[i] - b[i - 1]
	if b[i] < b[i - 1]:
		c += a
print(c)


(m, n) = input().split()
m = int(m)
n = int(n)
list = input().split()
sumt = 0
curr = 1
sumt1 = 0
for next in list:
	next = int(next)
	if next > curr:
		res = next - curr
		sumt += res
	elif next == curr:
		res = 0
		sumt += res
	else:
		res = m - (curr - next)
		sumt += res
	curr = next
print(sumt)


from typing import List, Tuple

def parse_input() -> Tuple[int, int, List[int]]:
	(n, m) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	return (n, m, arr)

def solve(n: int, m: int, arr: List[int]) -> int:
	current = 1
	time = 0
	for elem in arr:
		if current <= elem:
			time += elem - current
		else:
			time += n - current + elem
		current = elem
	return time
(n, m, arr) = parse_input()
print(solve(n, m, arr))


import math
(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
counter = 0
for i in range(1, len(a)):
	if a[i] < a[i - 1]:
		counter += 1
print(a[-1] - 1 + counter * n)


(n, m) = map(int, input().split())
val = [int(x) for x in input().split()]
start = 1
ans = 0
ans = 0
for end in val:
	d = end - start
	if d < 0:
		d += n
	ans += d
	start = end
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
h = 1
x = 0
s = 0
while x < len(a):
	if h < a[x]:
		s += a[x] - h
	elif h > a[x]:
		s += n - h + a[x]
	h = a[x]
	x += 1
print(s)


d = [int(x) for x in input().split(' ')]
t = [int(x) for x in input().split(' ')]
ti = 0
p = 1
for x in t:
	if x >= p:
		ti += x - p
	else:
		ti += d[0] - p + x
	p = x
print(ti)


(n, m) = map(int, input().split())
a = [int(w) for w in input().split()]
Ans = -1
for i in range(1, len(a)):
	if a[i] < a[i - 1]:
		Ans += n
Ans += a[-1]
print(Ans)


sa = input().split(' ')
ringroed = int(sa[0])
total = 0
sa2 = input().split(' ')
sa3 = [int(x) for x in sa2]
for x in range(len(sa3) - 1):
	fst = sa3[x]
	snd = sa3[x + 1]
	if fst <= snd:
		total += snd - fst
	else:
		total += ringroed - abs(snd - fst)
print(total + abs(sa3[0] - 1))


__author__ = 'ratnesh.mishra'
(n, m) = map(int, input().split())
tasks = map(int, input().split())
(init, sumu) = (1, 0)
for val in tasks:
	if val >= init:
		sumu += val - init
	else:
		sumu += n - init + val
	init = val
print(sumu)


(x, y) = map(int, input().split())
p = [int(i) for i in input().split()]
p.append(1000000)
s = 0
b = 0
for i in range(y):
	if p[i] > p[i + 1]:
		s = s + 1
b = p[y - 1] - 1 + x * s
print(b)


(n, m) = map(int, input().split())
tasks = [int(x) - 1 for x in input().split()]
total = tasks[0]
prev = tasks[0]
for task in tasks[1:]:
	if task >= prev:
		total += task - prev
	else:
		total += n - prev + task
	prev = task
print(total)


(n, m) = map(int, input().split())
x = [int(x) for x in input().split()]
k = len(x)
d = 1
ttl = 0
for i in range(m):
	a = x[i] - d
	if a < 0:
		a += n
	d = x[i]
	ttl += a
print(ttl)


import math
n = list(map(int, input().split()))
x = list(map(int, input().split()))
t1 = 0
t2 = 0
t3 = 0
for i in range(n[1]):
	if i == 0:
		t1 += x[0] - 1
		continue
	elif x[i - 1] > x[i]:
		t2 += n[0] - x[i - 1] + x[i]
		pass
	else:
		t3 += abs(x[i - 1] - x[i])
		pass
t = t1 + t2 + t3
print(t)


from collections import deque, Counter, OrderedDict
from heapq import nsmallest, nlargest
from math import ceil, floor, log, log2, sqrt

def binNumber(n, size=1):
	return bin(n)[2:].zfill(size)

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def iar():
	return list(map(int, input().split()))

def ini():
	return int(input())

def isp():
	return map(int, input().split())

def sti():
	return str(input())
(n, m) = isp()
a = iar()
g = 1
k = 0
for i in a:
	if i >= g:
		k += i - g
		g = i
	else:
		k += n - g + i
		g = i
print(k)


import sys
nm = [int(x) for x in sys.stdin.readline().split()]
in_str = [int(x) for x in sys.stdin.readline().split()]
t = 1
result = 0
for c in in_str:
	if c - t >= 0:
		result += c - t
	else:
		result += nm[0] + (c - t)
	t = c
print(result)


(n, m) = map(int, input().split())
a1 = list(map(int, input().split()))
sum = a1[0]
for i in range(len(a1) - 1):
	if a1[i + 1] < a1[i]:
		sum += n - (a1[i] - a1[i + 1])
	else:
		sum += a1[i + 1] - a1[i]
print(sum - 1)


a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
c = 1
sum = 0
for k in b:
	if c > k:
		sum += k + a[0] - c
	else:
		sum += k - c
	c = k
print(sum)


while True:
	try:
		(n, m) = map(int, input().split())
		l = input().split()
		s = 0
		for i in range(m):
			if i == 0:
				s = s + int(l[0]) - 1
			else:
				l[i] = int(l[i])
				l[i - 1] = int(l[i - 1])
				if l[i] >= l[i - 1]:
					s = s + l[i] - l[i - 1]
				else:
					s = s + l[i] + n - l[i - 1]
		print(s)
	except EOFError:
		break


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
currentPosition = 1
moves = 0
for i in a:
	if i >= currentPosition:
		moves += i - currentPosition
		currentPosition = i
	else:
		moves += n + i - currentPosition
		currentPosition = i
print(moves)


(n, m) = [int(i) for i in input().split()]
current = 1
ans = 0
for des in input().split():
	des = int(des)
	if des >= current:
		ans += des - current
	else:
		ans += des - current + n
	current = des
print(ans)


line = input().split()
n = int(line[0])
m = int(line[1])
answer = 0
line = list(map(int, input().split()))
last = 1
l = len(line)
for i in range(l):
	if line[i] > last:
		answer += line[i] - last
	elif line[i] == last:
		continue
	else:
		answer += n - last + line[i]
	last = line[i]
print(answer)


(n, m) = input().split(' ')
(n, m) = (int(n), int(m))
t = 0
p = 1
a = input()
z = ''
for i in range(0, len(a)):
	if a[i] != ' ' and i != len(a) - 1:
		z += a[i]
	else:
		z += a[i]
		if int(z) >= p:
			t += int(z) - p
		else:
			t += n - p + int(z)
		p = int(z)
		z = ''
print(t)


(n, m) = map(int, input().split(' '))
l1 = list(map(int, input().split(' ')))
l = [1]
l.extend(l1)
s = 0
for k in range(1, m + 1):
	if l[k] < l[k - 1]:
		s += n - l[k - 1] + l[k]
	else:
		s += l[k] - l[k - 1]
print(s)


li = [int(i) for i in input().split()]
(n, m) = li
li = [int(i) for i in input().split()]
(t, c) = (0, 1)
for i in li:
	if i < c:
		t += i - 1 + (n - c + 1)
		c = i
	elif i > c:
		t += i - c
		c = i
print(t)


(n, m) = map(int, input().split())
ai = list(map(int, input().split()))
num = 1
ans = 0
for i in range(m):
	temp = ai[i] - num
	ans += temp + n * int(temp < 0)
	num = ai[i]
print(ans)


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
(N, M) = map(int, input().split())
A = [int(x) for x in input().split()]
ans = 0
pre = 1
for (i, v) in enumerate(A):
	if v >= pre:
		ans += v - pre
	else:
		ans += N - pre + v
	pre = v
print(ans)


(n, l) = [int(i) for i in input().split()]
list = [int(i) for i in input().split()]
sum = 0
m = 1
for i in list:
	if i >= m:
		sum = sum + i - m
	else:
		sum = sum + n + i - m
	m = i
print(sum)


(n, t) = map(int, input().split())
l = list(map(int, input().split()))
(sum, pos) = (0, 1)
for i in range(t):
	if l[i] > pos:
		sum = sum + l[i] - pos
	elif pos == l[i]:
		pass
	elif l[i] < pos:
		sum = sum + n - pos + l[i]
	pos = l[i]
print(sum)


(n, m) = input().split()
n = int(n)
time = 0
h = 1
t = [0] * int(m)
t = input().split()
for i in t:
	if int(i) > h:
		time = time + (int(i) - h)
		h = int(i)
	elif int(i) < h:
		time = time + (n - (h - int(i)))
		h = int(i)
	else:
		pass
print(time)


(n, m) = map(int, input().split())
arr = [int(x) - 1 for x in input().split()]
total = 0
cur = 0
for a in arr:
	total += (a - cur) % n
	cur = a
print(total)


(n, m) = map(int, input().split())
places = [int(i) for i in input().split()]
p = places[0]
l = places[0] - 1
for i in range(1, m):
	if p > places[i]:
		l += places[i] + n - p
	else:
		l += places[i] - p
	p = places[i]
print(l)


(n, m) = map(int, input().split())
a = [int(i) for i in input().split()]
k = a[0] - 1
for i in range(1, m):
	if a[i - 1] < a[i]:
		k += a[i] - a[i - 1]
	elif a[i - 1] > a[i]:
		k += n - a[i - 1] + a[i]
print(k)


(houses, num_tasks) = map(int, input().split())
tasks = list(map(int, input().split()))
result = tasks[0] - 1
i = 0
for task in tasks[1:]:
	if task == tasks[i]:
		i += 1
	if task > tasks[i]:
		result += task - tasks[i]
		i += 1
	if task < tasks[i]:
		result += houses - tasks[i] + task
		i += 1
print(result)


(n, m) = map(int, input().strip().split(' '))
case = list(map(int, input().strip().split(' ')))
time = case[0] - 1
for i in range(m - 1):
	if case[i + 1] < case[i]:
		time += n + case[i + 1] - case[i]
	else:
		time += case[i + 1] - case[i]
print(time)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
move = 0
curr = 1
for i in a:
	if i >= curr:
		move += i - curr
	else:
		move += n - curr + i
	curr = i
print(move)


(n, m) = map(int, input().split())
c = 1
ans = 0
for i in map(int, input().split()):
	ans += (i - c) % n
	c = i
print(ans)


(n, m) = map(int, input().split())
li = [int(i) for i in input().split()]
t = li[0] - 1
for i in range(1, m):
	if li[i - 1] <= li[i]:
		t = t + (li[i] - li[i - 1])
	else:
		t = t + (n - (li[i - 1] - li[i]))
print(t)


def function1(n, m, a):
	t = 0
	i = 0
	r = 1
	circus = 1
	if m == 1:
		return a[0] - 1
	for i in range(1, m):
		if a[i] < a[i - 1]:
			t += n
			if i + 1 == m:
				t += a[i] - 1
		elif a[i] >= a[i - 1] and i + 1 == m:
			t += a[i] - 1
	return t

def function2(n, m, a):
	r = 1
	t = 0
	i = 0
	while i <= len(a) - 1:
		if (r - 1) % n == 0 and r != 0:
			r = 1
		if a[i] == r:
			i += 1
			if len(a) == 1 or len(a) < i + 1:
				break
			if a[i] == a[i - 1]:
				r += 0
				t += 0
				continue
		r += 1
		t += 1
	return t

def main():
	(n, m) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	print(function1(n, m, a))
main()


(n, m) = input().split()
(n, m) = (int(n), int(m))
a = [int(i) for i in input().split()]
ans = 0
p = 1
for i in range(m):
	destination = a[i]
	if destination >= p:
		ans = ans + destination - p
	else:
		ans = ans + n - p + destination
	p = destination
print(ans)


t = input()
temp = t.split()
n = int(temp[0])
m = int(temp[1])
s = input()
temp = s.split()
List = [int(i) for i in temp]
time = 0
for i in range(m):
	if i == 0:
		time = time + (List[i] - 1)
	elif List[i] < List[i - 1]:
		time = time + (n - List[i - 1]) + (List[i] - 1) + 1
	elif List[i] > List[i - 1]:
		time = time + (List[i] - List[i - 1])
print(time)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
act = 0
ans = 0
for temp in a:
	temp -= 1
	if temp >= act:
		ans += temp - act
	else:
		ans += n - (act - temp)
	act = temp
print(ans)


a = input().split(' ', 2)
n = int(a[0])
m = int(a[1])
b = [int(i) for i in input().split(' ', m)]
i = 1
ans = b[0] - 1
while i < m:
	if b[i] >= b[i - 1]:
		ans = ans + b[i] - b[i - 1]
	else:
		ans = ans + n - b[i - 1] + b[i]
	i = i + 1
print(ans)


(n, m) = [int(_) for _ in input().split()]
a = [int(_) for _ in input().split()]
cnt = 0
cur = 1
for i in range(m):
	dist = a[i] - cur
	if dist < 0:
		dist += n
	cnt += dist
	cur = a[i]
print(cnt)


(n, m) = input().split()
n = int(n)
m = int(m)
s = 0
list1 = input().split()
for i in range(m):
	list1[i] = int(list1[i])
x = 1
for i in range(m):
	if x <= list1[i]:
		s = s + list1[i] - x
	else:
		s = s + n + list1[i] - x
	x = list1[i]
print(s)


(n, m) = map(int, input().split())
place = [int(x) for x in input().split()]
step = place[0] - 1
for i in range(1, m):
	if place[i] < place[i - 1]:
		step += n + place[i] - place[i - 1]
	else:
		step += place[i] - place[i - 1]
print(step)


(n, m) = map(int, input().split())
arr = [int(i) for i in input().split()]
position = 1
time = 0
for i in arr:
	if i >= position:
		time += i - position
	else:
		time += n - abs(i - position)
	position = i
print(time)


(n, t) = map(int, input().split())
s = list(map(int, input().split()))
count = 0
for i in range(t - 1):
	if i == 0:
		count += s[0] - 1
	if s[i] > s[i + 1]:
		count += n - s[i] + s[i + 1]
	else:
		count += s[i + 1] - s[i]
print(count)


line = input()
(N, M) = line.split()
n = int(N)
m = int(M)
list1 = [int(i) for i in input().split()]
t = 0
x = 1
for i in list1:
	if i == x:
		continue
	elif i > x:
		t = t + i - x
		x = i
	else:
		t = t + n + i - x
		x = i
print(t)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [1]
l = 0
for i in a:
	b.append(i)
for i in range(m):
	if b[i] < b[i + 1]:
		l += b[i + 1] - b[i]
	elif b[i] == b[i + 1]:
		l += 0
	else:
		l += n - (b[i] - b[i + 1])
print(l)


line = input()
n = int(line.split(' ')[0])
m = int(line.split(' ')[1])
ans = 0
s = [1]
s = s + [int(k) for k in input().split(' ')]
i = 1
while i <= m:
	if int(s[i]) >= int(s[i - 1]):
		ans = ans + int(s[i]) - int(s[i - 1])
	else:
		ans = ans + int(s[i]) + n - int(s[i - 1])
	i = i + 1
print(ans)


(n, m) = map(int, input().split(' '))
number = map(int, input().split(' '))
result = 0
last_point = 1
for i in number:
	if i > last_point:
		result += i - last_point
	elif i < last_point:
		result += n - (last_point - i)
	else:
		result += 0
	last_point = i
print(result)


(n, l) = map(int, input().split())
li = list(map(int, input().split()))
count = li[0] - 1
for i in range(1, l):
	if li[i] < li[i - 1]:
		count += n - li[i - 1] + li[i]
	else:
		count += li[i] - li[i - 1]
print(count)


(n, m) = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]
ans = 0
temp = 1
for x in q:
	ans += (x + n - temp) % n
	temp = x
print(ans)


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write()
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(n, m) = map(int, input().rstrip().split())
l = list(map(int, input().rstrip().split()))
t = 0
curr = 1
j = 0
while j < m:
	if curr == l[j]:
		j += 1
		continue
	elif l[j] > curr:
		t += l[j] - curr
		curr = l[j]
		j += 1
	else:
		t += n - curr + l[j]
		curr = l[j]
		j += 1
print(t)


(n, m) = map(int, input().split())
serial = list(map(int, input().split()))
t = serial[0] - 1
for i in range(0, len(serial) - 1):
	t = t - serial[i] + serial[i + 1] + n * (serial[i] > serial[i + 1])
print(t)


(n, m) = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
c = arr[0] - 1
for i in range(1, len(arr)):
	if arr[i] >= arr[i - 1]:
		c += arr[i] - arr[i - 1]
	else:
		c += n - arr[i - 1] + arr[i]
print(c)


(g, h) = map(int, input().split())
a = [int(x) - 1 for x in input().split()]
(j, k) = (0, 0)
for x in a:
	if x < j:
		k += g - abs(x - j)
	else:
		k += abs(x - j)
	j = x
print(k)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
b = []
b.append(a[0] - 1)
for i in range(1, len(a)):
	if a[i] < a[i - 1]:
		b.append(n - a[i - 1])
		b.append(1)
		b.append(a[i] - 1)
	else:
		b.append(a[i] - a[i - 1])
print(sum(b))


(n, m) = map(int, input().split())
l = [1] + list(map(int, input().split()))
print(sum([l[i] - l[i - 1] + n if l[i] < l[i - 1] else l[i] - l[i - 1] for i in range(1, m + 1)]))


from sys import stdin, stdout
from collections import Counter

def ai():
	return list(map(int, stdin.readline().split()))

def ei():
	return map(int, stdin.readline().split())

def ip():
	return int(stdin.readline().strip())

def op(ans):
	return stdout.write(str(ans) + '\n')
from math import ceil
(n, m) = ei()
li = ai()
ans = li[0] - 1
for i in range(0, m):
	try:
		if li[i] == li[i + 1]:
			continue
		elif li[i] < li[i + 1]:
			ans += abs(li[i] - li[i + 1])
		elif li[i] > li[i + 1]:
			ans += abs(li[i] - n) + li[i + 1]
	except:
		pass
print(ans)


(n, m) = [int(x) for x in input().split()]
k = [int(y) for y in input().split()]
move = 0
for i in range(len(k)):
	if i == 0:
		move += k[i] - 1
	elif k[i] == k[i - 1]:
		move += 0
	elif k[i] > k[i - 1]:
		a = k[i] - k[i - 1]
		move += a
	else:
		a = n - k[i - 1] + k[i]
		move += a
print(move)


(n, m) = map(int, input().split())
a = [0] + list(map(lambda x: int(x) - 1, input().split()))
print(sum(((a[i] - a[i - 1]) % n for i in range(1, len(a)))))


t = input().split()
n = int(t[1])
m = int(t[0])
slon = 0
lis = [int(x) for x in input().split()]
current = 1
for x in lis:
	if current <= x:
		slon += x - current
		current = x
	else:
		slon += m - current + x
		current = x
print(slon)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
sum = 0
pos = 1
for i in range(len(a)):
	if pos > a[i]:
		sum += n - abs(pos - a[i])
	else:
		sum += a[i] - pos
	pos = a[i]
print(sum)


(n, k) = list(map(int, input().split()))
arr = list(map(int, input().split()))
arr.insert(0, 1)
sum = 0
for k in range(k):
	diff = arr[k + 1] - arr[k]
	if diff < 0:
		diff += n
	sum += diff
print(sum)


n = [int(i) for i in input().split()]
line = [int(i) for i in input().split()]
total = 0
for i in range(n[1] - 1):
	if line[i] > line[i + 1]:
		total += 1
print(n[0] * total + line[n[1] - 1] - 1)


a = input().split()
b = int(a[0])
c = input().split()
d = 1
f = 0
for i in range(0, int(a[1])):
	if int(c[i]) >= d:
		f = f + int(c[i]) - d
	if int(c[i]) < d:
		f = f + b - d + int(c[i])
	d = int(c[i])
print(f)


(m, n) = input().split()
list = input().split()
cur = 1
sumt = 0
r = 0
for next1 in list:
	if int(cur) < int(next1):
		sumt = int(next1) - int(cur)
		r += sumt
	elif int(cur) > int(next1):
		sumt = int(m) - (int(cur) - int(next1))
		r += sumt
	cur = int(next1)
print(r)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
t = 0
k = 1
for i in a:
	t += (i - k) % n
	k = i
print(t)


from functools import reduce

def kek(x, y):
	global res
	res += y - x if y >= x else y - x + n
	return y
(n, m) = map(int, input().split())
ar = list(map(int, input().split()))
res = ar[0] - 1
reduce(kek, ar)
print(res)


def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def solve():
	(N, M) = getInts()
	A = [1] + getInts()
	T = 0
	for j in range(1, M + 1):
		(Y, X) = (A[j], A[j - 1])
		T += (Y - X) % N
	return T
ans = solve()
print(ans)


[num, m] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
Round = 0
n = 0
for i in range(m):
	if n > a[i]:
		Round += 1
	n = a[i]
print(Round * num + n - 1)


(n, m) = map(int, input().split())
m_posts = [int(x) for x in input().split()]
m_posts.insert(0, 1)
t = 0
for i in range(m):
	if m_posts[i + 1] - m_posts[i] >= 0:
		t += m_posts[i + 1] - m_posts[i]
	else:
		t += n + (m_posts[i + 1] - m_posts[i])
print(t)


arr1 = str(input())
arr = arr1.split()
house = int(arr[0])
task = int(arr[1])
arr1 = str(input())
arr2 = arr1.split()
arr = []
for i in arr2:
	arr.append(int(i))
count = 0
cur = arr[0]
count += cur - 1
n = len(arr)
for i in range(1, n):
	nexts = arr[i]
	if nexts == cur:
		continue
	elif nexts > cur:
		count += nexts - cur
		cur = nexts
	else:
		diff = house - cur
		diff += nexts
		count += diff
		cur = nexts
print(count)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
x = 1
r = 0
for i in a:
	if x <= i:
		r += i - x
		x = i
	else:
		r += n - x + i
		x = i
print(r)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
d = 0
c = 1
for i in l:
	if i >= c:
		d += i - c
		c = i
	else:
		x = n - c
		x += i
		c = i
		d += x
print(d)


(num_houses, num_tasks) = [int(a) for a in input().strip().split()]
tasks = [int(a) for a in input().strip().split()]
time = 0
current_house = 1
for task in tasks:
	if task < current_house:
		time += num_houses - current_house + task
	else:
		time += task - current_house
	current_house = task
print(time)


(n, m) = [int(i) for i in input().split()]
task = [int(i) for i in input().split()]
time = 0
s = 1
for j in task:
	if j - s > 0:
		time += j - s
	elif j - s < 0:
		time += j + n - s
	s = j
print(time)


read = lambda : map(int, input().split())
(n, m) = read()
a = [1] + list(read())
ans = 0
for i in range(1, m + 1):
	cur = a[i] - a[i - 1]
	if a[i] < a[i - 1]:
		cur += n
	ans += cur
print(ans)


(a, b) = map(int, input().split())
l = [int(x) for x in input().split()]
n = l[0] - 1
for i in range(b - 1):
	if l[i] <= l[i + 1]:
		step = l[i + 1] - l[i]
		n += step
	if l[i] > l[i + 1]:
		step = a - l[i] + l[i + 1]
		n += step
print(n)


(n, m) = input().split()
n = int(n)
m = int(m)
s = input().split()
step = 0
posi = 1
for i in range(m):
	if int(s[i]) >= posi:
		step += int(s[i]) - posi
	if int(s[i]) < posi:
		step += n - posi + int(s[i])
	posi = int(s[i])
print(step)


numbers = input()
numbers = list(map(int, numbers.split()))
n = numbers[0]
m = numbers[1]
previousHouse = 1
answer = 0
houses = input()
houses = list(map(int, houses.split()))
for i in range(0, m):
	house = houses[i]
	if previousHouse > house:
		answer += n - previousHouse + house
	elif house > 1:
		answer += house - previousHouse
	previousHouse = house
print(answer)


from sys import stdin
from collections import defaultdict as dd
from collections import deque as dq
import itertools as it
from math import sqrt, log, log2
from fractions import Fraction
(n, m) = map(int, stdin.readline().split())
nums = list(map(int, stdin.readline().split()))
numsteps = 0
start = 1
for i in nums:
	if i < start:
		numsteps += n - start + 1 + (i - 1)
		start = i
	elif i > start:
		numsteps += i - start
		start = i
print(numsteps)


(n, m) = map(int, input().split())
works = list(map(int, input().split()))
res = works[0] - 1
for i in range(m - 1):
	if works[i] > works[i + 1]:
		res += n - works[i]
		res += works[i + 1]
	elif works[i] < works[i + 1]:
		res += works[i + 1] - works[i]
print(res)


(n, m) = map(int, input().split())
houses = list(map(int, input().split()))
previous = 1
distance = 0
for house in houses:
	diff = house - previous
	previous = house
	if diff >= 0:
		distance += diff
	else:
		distance += n + diff
print(distance)


(n, m) = list(map(int, input().split()))
work = list(map(int, input().split()))
stay = 1
ans = 0
for i in work:
	if i == stay:
		continue
	elif stay < i:
		ans += i - stay
		stay = i
	else:
		ans += n - stay + i
		stay = i
print(ans)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
h = 1
r = 0
for i in range(m):
	if a[i] > h:
		r += a[i] - h
	elif a[i] < h:
		r += n - (h - a[i])
	h = a[i]
print(r)


nm = input().split()
(n, m) = (int(nm[0]), int(nm[1]))
houses = map(int, input().split())
tot_time = 0
prev_house = 1
for house in houses:
	time = house - prev_house
	prev_house = house
	if time < 0:
		time += n
	tot_time += time
print(tot_time)


(n, m) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
k = 1
cnt = 0
for i in l:
	if i > k:
		cnt = cnt + i - k
	elif i < k:
		cnt = cnt + n - k + i
	k = i
print(cnt)


inp = input().split(' ')
n = int(inp[0])
m = int(inp[1])
foo = list(map(int, input().split(' ')))
house = 1
count = 0
for i in foo:
	if house <= i:
		count += i - house
	else:
		count += n - house + i
	house = i
print(count)


(duoshaofang, jijianshi) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
zouwanjiquan = 0
for i in range(jijianshi - 1):
	if a[i] > a[i + 1]:
		zouwanjiquan += 1
jieguo = zouwanjiquan * duoshaofang + a[-1] - 1
print(jieguo)


def time(arr, n):
	t = 0
	for i in range(1, len(arr)):
		if arr[i] >= arr[i - 1]:
			t = t + (arr[i] - arr[i - 1])
		else:
			t = t + (n - (arr[i - 1] - arr[i]))
	t = t + (arr[0] - 1)
	print(t)
nm = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
time(arr, nm[0])


(n, m) = input().split()
n = int(n)
m = int(m)
li = [int(i) for i in input().split()]
c = li[0] - 1
for i in range(m):
	if i == m - 1:
		break
	elif li[i] > li[i + 1]:
		x = li[i] - li[i + 1]
		y = n - x
		c = c + y
	elif li[i] < li[i + 1]:
		x = li[i + 1] - li[i]
		c = c + x
	elif li[i] == li[i + 1]:
		c = c
print(c)


(n, m) = map(int, input().split())
house_number = list(map(int, input().split()))
(time, init) = (0, 1)
for i in house_number:
	end = i
	time += (end - init) % n
	init = end
print(time)


def solve(n, house_list):
	curr_location = 1
	travelled = 0
	for i in range(len(house_list)):
		if curr_location == house_list[i]:
			pass
		elif curr_location < house_list[i]:
			travelled += house_list[i] - curr_location
			curr_location = house_list[i]
		else:
			travelled += n - curr_location
			travelled += house_list[i]
			curr_location = house_list[i]
	return travelled

def main():
	(n, m) = map(int, input().split(' '))
	house_list = list(map(int, input().split(' ')))
	print(solve(n, house_list))
main()


(n, m) = map(lambda x: int(x), input().split(' '))
a = list(map(lambda x: int(x), [1] + input().split(' ')))
s = 0
i = 1
while i < len(a):
	tmp = a[i] - a[i - 1]
	s += tmp if tmp >= 0 else n + tmp
	i += 1
print(s)


m = input()
m = m.split(' ')
n = int(m[0])
m = int(m[1])
path = input()
path = path.split(' ')
pos = 1
time = 0
for i in path:
	diff = int(i) - pos
	if diff >= 0:
		time = time + diff
	else:
		time = time + n + diff
	pos = int(i)
print(time)


from sys import stdin, stdout
(n, m) = map(int, stdin.readline().strip().split())
arr = [int(x) for x in stdin.readline().strip().split()]
l = 1
s = 0
for x in arr:
	if x >= l:
		s += x - l
	else:
		s += n - l + x
	l = 0 + x
print(s)


(n, m) = map(int, input().split())
n_list = [i for i in range(1, n + 1)]
previous = 1
r = 0
for i in map(int, input().split()):
	if i == previous:
		continue
	elif previous < i:
		r += i - previous
	else:
		r += n - previous + i
	previous = i
print(r)


(n, m) = [int(i) for i in input().split()]
listm = [int(i) for i in input().split()]
x = 0
for i in range(m - 1):
	if listm[i] > listm[i + 1]:
		x += 1
print(x * n + listm[m - 1] - 1)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
s = 0
p = 1
for v in l:
	s += (v - p) % n
	p = v
print(s)


(n, m) = map(int, input().split())
jobs = [int(i) for i in input().split()]
count = 0
last = 1
for i in range(0, m, 1):
	if jobs[i] == last:
		continue
	elif jobs[i] > last:
		count += jobs[i] - last
	else:
		count += n - last
		count += jobs[i]
	last = jobs[i]
print(count)


hs_no = input()
sep = hs_no.split()
house = []
task = []
for i in sep:
	house.append(int(i))
task_no = input()
seps = task_no.split()
for i in seps:
	task.append(int(i))
count = 0
initial = 1
for final in task:
	if final >= initial:
		count += final - initial
	else:
		count += house[0] - initial + final
	initial = final
print(count)


f = lambda : map(int, input().split())
(n, m) = f()
t = 0
h = 1
for x in f():
	t += (x - h) % n
	h = x
print(t)


(n, m) = [int(x) for x in input().split(' ')]
errands = [int(x) for x in input().split(' ')]
moves = 0
current_house = 1
for i in range(m):
	moves += (errands[i] - current_house) % n
	current_house = errands[i]
print(moves)


[n, m] = [int(item) for item in input().split(' ')]
tasks = [int(item) for item in input().split(' ')]
house = 1
time = 0
for destination in tasks:
	distance = destination - house
	if distance < 0:
		distance = n + distance
	house = destination
	time += distance
print(time)


def t(a, b, n):
	if b > a:
		m = n - b + a
	if a > b:
		m = a - b
	if a == b:
		m = 0
	return m
(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
ma = a[0] - 1
for i in range(1, m):
	ma = ma + t(a[i], a[i - 1], n)
print(ma)


(n, m) = list(map(int, input().split()))
houses = list(map(int, input().split()))
from itertools import zip_longest
houses = [i for (i, j) in zip_longest(houses, houses[1:]) if i != j]
ctr = houses[0] - 1
for i in range(1, len(houses)):
	ctr += (n + houses[i] - houses[i - 1], houses[i] - houses[i - 1])[houses[i] > houses[i - 1]]
print(ctr)


(x, y) = map(int, input().split())
it = 1
l = 0
k = list(map(int, input().split()))
for g in k:
	if it < g:
		l = l + g - it
		it = g
	elif g < it:
		l = l + x - it + g
		it = g
print(l)


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
(n, m) = invr()
a = inlt()
x = 1
t = 0
for i in a:
	if x <= i:
		t += i - x
		x = i
	else:
		t += n - x + i
		x = i
print(t)


n = map(int, input().split())
p = map(int, input().split())
n = list(n)
p = list(p)
l = n[1]
n = n[0]
c = 0
for i in range(l):
	if i == 0:
		c += p[i] - 1
	elif p[i] < p[i - 1]:
		c += n - p[i - 1] + p[i]
	elif p[i] == p[i - 1]:
		continue
	else:
		c += p[i] - p[i - 1]
print(c)


a = input()
a = list(map(int, a.split()))
b = input()
b = list(map(int, b.split()))
i = 1
num = 0
for j in b:
	if j > i:
		num = num + (j - i)
		i = j
	elif j < i:
		num = num + (a[0] - i) + j
		i = j
print(num)


(n, m) = input().split()
(n, m) = (int(n), int(m))
task = [int(x) for x in input().split()]
ans = 0
cur = 1
for i in range(m):
	if task[i] > cur:
		ans += task[i] - cur
	elif task[i] < cur:
		ans += task[i] + n - cur
	cur = task[i]
print(ans)


(n, m) = input().split()
(n, m) = (int(n), int(m))
s = input().split()
a = int(s[0])
t = a
per = a
for i in range(1, m):
	a = int(s[i])
	if a >= per:
		t += abs(a - per)
	else:
		t += n + a - per
	per = a
print(t - 1)


a = input()
a = a.split()
n = int(a[0])
m = int(a[1])
s = input()
s = s.split()
p = 1
cnt = 0
for i in s:
	j = int(i)
	if j < p:
		cnt = cnt + (n - (p - j))
	elif j > p:
		cnt = cnt + (j - p)
	p = j
print(cnt)


(n, m) = map(int, input().split())
l = [int(w) for w in input().split()]
q = 1
e = 1
t = 0
t += l[0] - e
e = l[0]
while q < m:
	if l[q] >= e:
		t += l[q] - e
	else:
		t += l[q] + n - e
	e = l[q]
	q = q + 1
print(int(t))


import sys
from random import choice, randint
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def MI():
	return map(int, sys.stdin.readline().strip().split())

def LI():
	return list(map(int, sys.stdin.readline().strip().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines().strip()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().strip().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().strip().split()]

def LS():
	return sys.stdin.readline().strip().split()

def I():
	return int(sys.stdin.readline().strip())

def F():
	return float(sys.stdin.readline().strip())

def S():
	return sys.stdin.readline().strip()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))
inp = sys.stdin.readline
out = sys.stdout.write

def main():
	(n, m) = MI()
	a = LI()
	c = a[0] - 1
	for i in range(1, m):
		if a[i - 1] > a[i]:
			c += n - a[i - 1] + a[i]
		else:
			c += a[i] - a[i - 1]
	pf(c)
main()


(n, m) = [int(x) for x in input().strip().split(' ')]
arr = map(lambda x: int(x), input().strip().split(' '))
state = 1
ans = 0
for j in arr:
	if j < state:
		ans += n - state + j
	else:
		ans += j - state
	state = j
print(ans)


(n, m) = [int(i) for i in input().split()]
g = input().split()
g = list(map(int, g))

def uu(i):
	if i < m - 1:
		if g[i + 1] < g[i]:
			zz = g[i + 1] + n - g[i]
		else:
			zz = g[i + 1] - g[i]
	else:
		zz = 0
	return zz
time = 0
for i in range(m - 1):
	time = time + uu(i)
time = time + g[0] - 1
print(time)


(n, m) = list(map(int, input().split(' ')))
tasks = list(map(int, input().split(' ')))
steps = 0
current_po = 1
for task in tasks:
	if task >= current_po:
		steps += task - current_po
	else:
		steps += task + n - current_po
	current_po = task
print(steps)


(n, m) = list(map(int, input().split()))
tasks = list(map(int, input().split()))
moves = 0
ind = 1
for t in tasks:
	if not t == ind:
		if t > ind:
			moves += t - ind
		else:
			moves += n - (ind - t)
		ind = t
print(moves)


import os
from sys import stdin, stdout

def solve(tc):
	(n, m) = map(int, stdin.readline().split())
	seq = list(map(int, stdin.readline().split()))
	ans = 0
	cur = 1
	for num in seq:
		ans += (num - cur + n) % n
		cur = num
	print(ans)
tcs = 1
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


a = [int(i) for i in input().split(' ')]
x = a[0]
b = [int(i) for i in input().split(' ')]
c = 1
t = 0
for i in range(0, len(b)):
	if b[i] > c:
		t = t + (b[i] - c)
		c = b[i]
	elif b[i] < c:
		t = t + (x - c) + (b[i] - 1) + 1
		c = b[i]
	else:
		t = t + 0
print(t)


n = int(input().split()[0])
actions = list(map(int, input().split()))
steps = 0
prev_act = 1
for act in actions:
	if prev_act > act:
		steps += n - prev_act + act
	else:
		steps += act - prev_act
	prev_act = act
print(steps)


from collections import Counter, defaultdict
BS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def to_base(s, b):
	res = ''
	while s:
		res += BS[s % b]
		s //= b
	return res[::-1] or '0'
alpha = 'abcdefghijklmnopqrstuvwxyz'
from math import floor, ceil, pi
(n, m) = list(map(int, input().split()))
t = list(map(int, input().split()))
pos = 1
tot = 0
for i in t:
	if i < pos:
		tot += n - pos + i
	else:
		tot += i - pos
	pos = i
print(tot)


(n, k) = [int(x) for x in input().split()]
places = [int(x) for x in input().split()]
sum = places[0] - 1
for i in range(1, len(places)):
	difference = places[i] - places[i - 1]
	if difference < 0:
		difference += n
	sum += difference
print(sum)


(n, m) = map(int, input().split())
M = list(map(int, input().split()))
now = 1
time = 0
for i in M:
	if i >= now:
		time += i - now
	else:
		time += n - now + i
	now = i
print(time)


n_m = input().split()
n = int(n_m[0])
m = int(n_m[1])
t = 0
tasks = input().split()
for i in range(len(tasks)):
	tasks[i] = int(tasks[i])
for i in range(len(tasks)):
	if i != len(tasks) - 1:
		if tasks[i + 1] >= tasks[i]:
			t += tasks[i + 1] - tasks[i]
		if tasks[i + 1] < tasks[i]:
			t += n - tasks[i] + tasks[i + 1]
	if i == 0:
		t += tasks[i]
if t > 0:
	print(t - 1)
if t == 0:
	print(t)


def main():
	(n, m) = [int(item) for item in input().split()]
	deals = [int(item) for item in input().split()]
	time = 0
	now = 1
	for i in range(0, m):
		deal = deals[i]
		if deal > now:
			time += deal - now
		elif deal < now:
			time += n - now + deal
		now = deal
	print(time)
main()


args = [int(x) for x in input().split(' ')]
n = args[0]
m = args[1]
aa = [int(x) for x in input().split(' ')]
start = 1
time = 0
for a in aa:
	if a >= start:
		time += a - start
		start = a
	else:
		time += n - start + a
		start = a
print(time)


def splitarray(a):
	li = []
	i = 0
	while i < len(a):
		en = i
		p = []
		p.append(a[i])
		for j in range(i + 1, len(a)):
			if a[j] >= a[j - 1]:
				en = j
				p.append(a[j])
			else:
				break
		i = en + 1
		li.append(p)
	return li
s = input().split()
h = int(s[0])
a = input().split()
a = [int(item) for item in a]
ans = []
i = 0
ans = splitarray(a)
res = (len(ans) - 1) * h + ans[-1][-1] - 1
print(res)


(x, y) = map(int, input().split())
a = list(map(int, input().split()))
d = 0
for i in range(0, y):
	if i == 0:
		d = d + a[i] - 1
	if i > 0 and a[i] < a[i - 1]:
		d = d + x - a[i - 1] + a[i]
	if i > 0 and a[i] > a[i - 1]:
		d = d + a[i] - a[i - 1]
print(d)


def main():
	(n, m) = [int(i) for i in input().split()]
	result = 0
	current = 1
	houses = (int(i) for i in input().split())
	for i in houses:
		result += (n + i - current) % n
		current = i
	print(result)
main()


def main():
	(n, k) = list(map(int, input().split()))
	lst = list(map(int, input().split()))
	ans = 0
	prev = 1
	for i in range(k):
		if lst[i] >= prev:
			ans += lst[i] - prev
			prev = lst[i]
		else:
			ans += n - prev + lst[i]
			prev = lst[i]
	print(ans)
t = 1
while t != 0:
	main()
	t -= 1


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 1)
steps = []
for i in range(m):
	if a[i] <= a[i + 1]:
		step = a[i + 1] - a[i]
	else:
		step = n - a[i] + a[i + 1]
	steps.append(step)
print(sum(steps))


def moves(n, lst):
	last_index = 1
	moves = 0
	for x in range(1, len(lst) + 1):
		if lst[x - 1] - last_index > 0:
			moves += lst[x - 1] - last_index
		elif lst[x - 1] - last_index < 0:
			moves += lst[x - 1] - last_index + n
		last_index = lst[x - 1]
	return moves
(n, m) = [int(x) for x in input().split()]
lst = [int(x) for x in input().split()]
print(moves(n, lst))


(n, m) = map(int, input().split())
lst = list(map(int, input().split()))
loc = 1
ans = 0
for i in range(len(lst)):
	if lst[i] >= loc:
		ans += lst[i] - loc
	else:
		ans += n - (loc - lst[i])
	loc = lst[i]
print(ans)


(n, m) = [int(x) for x in input().strip().split()]
a = [int(x) for x in input().strip().split()]
k = a[0] - 1
for i in range(1, m):
	if a[i] > a[i - 1]:
		k = k + a[i] - a[i - 1]
	elif a[i] < a[i - 1]:
		k = k + n - a[i - 1] + a[i]
print(k)


import math
(n, m) = map(int, input().split())
data = list(map(int, input().split()))
cur = 1
cnt = 0
for i in range(len(data)):
	if data[i] >= cur:
		cnt += data[i] - cur
	else:
		cnt += n - cur + data[i]
	cur = data[i]
print(cnt)


import sys
from os import path
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
(n, m) = input().split()
n = int(n)
m = int(m)
arr = list(map(int, input().split()))
start = 1
ans = 0
for i in arr:
	if i > start:
		ans += i - start
		start = i
	elif i == start:
		continue
	else:
		ans += n - start + i
		start = i
print(ans)


(n, m) = map(int, input().split())
ans = 0
arr = [int(x) for x in input().split()]
pos = 1
for i in range(m):
	if arr[i] >= pos:
		ans += arr[i] - pos
	else:
		ans += n - (pos - arr[i])
	pos = arr[i]
print(str(ans))


n = input().split()
n = [int(x) for x in n]
arr = input().split()
arr = [int(x) for x in arr]
ans = arr[0]
for i in range(1, len(arr)):
	ans += abs(arr[i - 1] - arr[i]) if arr[i - 1] <= arr[i] else abs(n[0] - arr[i - 1]) + arr[i]
print(f'{ans - 1}')


(n, m) = map(int, input().split())
sl = list(map(int, input().split()))
mylocation = 1
time = 0
for i in sl:
	if i > mylocation:
		time += i - mylocation
		mylocation = i
	elif i < mylocation:
		time += n - (mylocation - i)
		mylocation = i
print(time)


(n, tasks) = map(int, input().split())
lis = list(map(int, input().split()))
print(n * sum((lis[i] < lis[i - 1] for i in range(1, tasks))) + lis[-1] - 1)


(n, m) = map(int, input().split())
A = [int(x) for x in input().split()]
num = A[0] - 1
for i in range(m - 1):
	if A[i + 1] < A[i]:
		num += n + A[i + 1] - A[i]
	else:
		num += A[i + 1] - A[i]
print(num)


(n, m) = map(int, input().split())
(a, q) = ([1] + list(map(int, input().split())), 0)
for i in range(1, m + 1):
	q += n * int(a[i] < a[i - 1]) + a[i] - a[i - 1]
print(q)


import os
import sys
from io import BytesIO, IOBase

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True
(n, m) = [int(x) for x in input().split()]
a = list(map(int, input().split()))
a.append(-1)
a.append(-1)
ind = 0
time = a[0] - 1
for i in a:
	if a[ind] == -1 or a[ind + 1] == -1:
		break
	elif i < a[ind + 1]:
		time = time + a[ind + 1] - a[ind]
		ind += 1
	elif i == a[ind + 1]:
		ind += 1
	else:
		time = time + n - a[ind] + a[ind + 1]
		ind += 1
print(time)


(n, m) = input().split(' ')
n = int(n)
m = int(m)
arr = list(map(int, input().rstrip().split()))
Sum = arr[0] - 1
for i in range(1, m):
	if arr[i] >= arr[i - 1]:
		s = arr[i] - arr[i - 1]
		Sum += s
	else:
		c = n - arr[i - 1]
		Sum = Sum + c + arr[i]
print(Sum)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
count = 1
ans = 0
while count < m:
	if a[count] < a[count - 1]:
		ans += n - a[count - 1] + a[count]
	else:
		ans += a[count] - a[count - 1]
	count += 1
print(a[0] + ans - 1)


(n, m) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
total = l[0] - 1
for i in range(1, len(l)):
	if l[i] >= l[i - 1]:
		total += l[i] - l[i - 1]
	else:
		total += n - l[i - 1] + l[i]
print(total)


(n, m) = map(int, input().split())
task_list = list(map(int, input().split()))[:m]
time = abs(task_list[0] - 1)
for task in range(1, m):
	if task_list[task] > task_list[task - 1]:
		t = task_list[task] - task_list[task - 1]
		time += t
	elif task_list[task] < task_list[task - 1]:
		t = n - (task_list[task - 1] - task_list[task])
		time += t
print(time)


line0 = input().split()
n = int(line0[0])
m = int(line0[1])
line1 = input().split()
s = 1
for i in range(m):
	line1[i] = int(line1[i])
for i in range(1, m):
	if line1[i] - line1[i - 1] < 0:
		s = s + 1
print((s - 1) * n - 1 + line1[i])


I = lambda : map(int, input().split())
(n, _) = I()
a = [1, *I()]
print(sum(([y - x, n + y - x][x > y] for (x, y) in zip(a, a[1:]))))


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.insert(0, 1)
t = 0
for i in range(m):
	d = a[i + 1] - a[i]
	if d >= 0:
		t += d
	else:
		t += n + d
print(t)


import sys
f = sys.stdin
(n, m) = map(int, f.readline().strip().split())
p = [int(i) for i in f.readline().strip().split()]
prev = 1
count = 0
for i in p:
	if i > prev:
		count += i - prev
	elif i < prev:
		count += n + i - prev
	prev = i
print(count)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
res = a[0] - 1
for i in range(len(a) - 1):
	dif = a[i + 1] - a[i]
	if a[i] < a[i + 1]:
		res += dif
	elif a[i] > a[i + 1]:
		res += n + dif
print(res)


s = input()
k = list(map(int, s.split()))
n = k[0]
m = k[1]
ss = input()
kk = list(map(int, ss.split()))
pos = 1
route = 0
for i in range(0, m):
	if kk[i] >= pos:
		route += kk[i] - pos
	else:
		route += kk[i] + n - pos
	pos = kk[i]
print(route)


(n, m) = map(int, input().rstrip().split())
route = list(map(int, input().rstrip().split()))
posi = 1
time = 0
for i in route:
	if posi == i:
		continue
	elif posi > i:
		time += n - (posi - i)
		posi = i
	elif posi < i:
		time += i - posi
		posi = i
print(time)


(a, b) = input().split()
a = int(a)
b = int(b)
m = input()
p = m.split()
p = [int(a) for a in p]
now = 1
time = 0
for i in p:
	if i >= now:
		time = time + (i - now)
	else:
		time = time + (a - now) + i
	now = i
print(time)


(n, m) = input().split()
l = list(map(int, input().split()))
house = 1
time = 0
for x in l:
	if house > x:
		time += int(n) - house + x
	else:
		time += abs(house - x)
	house = x
print(time)


a = input().split()
n = int(a[0])
m = int(a[1])
z = 0
p = 1
b = input().split()
for i in range(m):
	b[i] = int(b[i])
while p < m:
	q = b[p]
	w = b[p - 1]
	if q >= w:
		z = z + q - w
	else:
		z = z + n + q - w
	p = p + 1
print(z + b[0] - 1)


TN = 1

def solution():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	cur = 1
	ans = 0
	for i in range(m):
		if a[i] < cur:
			ans += a[i] + n - cur
		else:
			ans += a[i] - cur
		cur = a[i]
	print(ans)
while TN != 0:
	solution()
	TN -= 1


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
a.insert(0, 0)
current = 1
t = 0
for i in range(1, m + 1):
	if current == a[i]:
		continue
	elif current < a[i]:
		t = t + a[i] - current
		current = a[i]
	elif current > a[i]:
		t = t + n - current + a[i]
		current = a[i]
print(t)


(n, m) = map(int, input().split())
task = list(map(int, input().split()))
ans = task[0] - 1
for i in range(m - 1):
	if task[i + 1] < task[i]:
		ans = ans + task[i + 1] + n - task[i]
	if task[i + 1] > task[i]:
		ans = ans + task[i + 1] - task[i]
print(str(ans))


i1 = input().split()
n = int(i1[0])
m1 = int(i1[1])
a = input().split()
ans = int(a[0]) - 1
for x in range(m1 - 1):
	pr = int(a[x])
	ne = int(a[x + 1])
	if pr > ne:
		tem = n - pr
		ans = ans + tem + ne
	else:
		tem = ne - pr
		ans = ans + tem
print(ans)


(n, m) = map(int, input().split())
l1 = [int(x) for x in input().split()]
k = 0
for i in range(len(l1) - 1):
	if l1[i] > l1[i + 1]:
		k += 1
print(n * k + l1[-1] - 1)


(s, n) = [int(i) for i in input().split()]
list = [int(i) for i in input().split()]
k = 1
sum = list[0] - 1
while k < n:
	if list[k] == list[k - 1]:
		sum = sum
	elif list[k] > list[k - 1]:
		sum = sum + list[k] - list[k - 1]
	else:
		sum = sum + s + list[k] - list[k - 1]
	k = k + 1
print(sum)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
time = 0
b = 1
for a in arr:
	if b <= a:
		time += abs(a - b)
		b = a
	elif b > a:
		time += abs(n - b + a)
		b = a
print(time)


(n, m) = map(int, input().split())
a = list(map(int, input().strip().split()))[:m]
gap = 0
curr = 1
for i in range(len(a)):
	if a[i] >= curr:
		gap += a[i] - curr
	else:
		gap += n - curr + a[i]
	curr = a[i]
print(gap)


import sys
import math
Input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
(counter, tmp) = (0, 1)
for i in a:
	counter += (i - tmp) % n
	tmp = i
print(counter)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
house = 1
t = 0
for i in a:
	if house == i:
		continue
	elif house < i:
		t += i - house
	else:
		t += i - house + n
	house = i
print(t)


(m, n) = map(int, input().split())
list1 = [int(i) for i in input().split()]
t = 0
for i in range(1, n):
	if list1[i] < list1[i - 1]:
		t = t + m
t = t + list1[n - 1] - 1
print(t)


(n, m) = map(int, input().split())
l = input().split()
ans = 0
last = 1
for i in l:
	ans += (int(i) - last, n - last + int(i))[int(i) < last]
	last = int(i)
print(ans)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
next_tasks = tasks.copy()
k = 0
del next_tasks[0]
for (task, next_task) in zip(tasks, next_tasks):
	if task > next_task:
		k += 1
last_task = tasks[m - 1]
time = k * n + (last_task - 1)
print(time)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
ans = arr[0] - 1
for i in range(1, m):
	ans = ans + (arr[i] - arr[i - 1] + n) % n
print(ans)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
k1 = 1
count = 0
for k2 in l:
	if k1 < k2:
		count = count + (k2 - k1)
		k1 = k2
	elif k1 > k2:
		count = count + (n - k1 + k2)
		k1 = k2
	elif k1 == k2:
		pass
print(count)


(n, _) = map(int, input().split())
lst = input().split()
t = 0
where = 1
for i in lst:
	i = int(i)
	if i > where:
		t += i - where
	elif where > i:
		t += n - where + i
	where = i
print(t)


(a, b) = map(int, input().split())
arr = list(map(int, input().split()))
sum = 0
sum = sum + arr[0] - 1
for i in range(1, b):
	if arr[i] < arr[i - 1]:
		sum = sum + a - arr[i - 1] + arr[i]
	elif arr[i - 1] == arr[i]:
		sum = sum + 0
	else:
		sum = sum + arr[i] - arr[i - 1]
print(sum)


(n, size) = map(int, input().split())
l = list(map(int, input().split()))
k = 1
c = 0
for item in l:
	if item > k:
		c += item - k
		k = item
	elif item < k:
		c += n - k + 1
		k = 1
		c += item - k
		k = item
print(c)


(n, m) = list(map(int, input().split()))
tasks = list(map(int, input().split()))
position = 1
score = 0
for i in tasks:
	if position < i:
		score += i - position
		position = i
	elif position > i:
		score += n - position + i
		position = i
print(score)


def dis(a, b, n):
	if a >= b:
		return a - b
	else:
		return a - b + n
(n, m) = input().split()
(n, m) = (int(n), int(m))
a = input().split()
x = 0
c = 1
for i in a:
	x += dis(int(i), c, n)
	c = int(i)
print(x)


(x, y) = input().split()
x = int(x)
y = int(y)
count = 0
temp = 1
c = list(map(int, input().split()))
for i in c:
	count = count + (i - temp) % x
	temp = i
print(count)


(n, m) = list(map(int, input().split(' ')))
pos = list(map(int, input().split(' ')))
current = 1
steps = 0
for i in range(m):
	if current < pos[i]:
		steps += pos[i] - current
		current = pos[i]
	elif current > pos[i]:
		steps += pos[i] + n - current
		current = pos[i]
print(steps)


def dis(n, x, y):
	if x <= y:
		return y - x
	else:
		return n + y - x
s = input().split()
n = int(s[0])
m = int(s[1])
a = [int(i) for i in input().split()]
su = a[0] - 1
for i in range(1, m):
	su += dis(n, a[i - 1], a[i])
print(su)


(n, t, c) = (int(input().split()[0]), 0, 1)
for x in map(int, input().split()):
	t += [n - c + x, x - c][c <= x]
	c = x
print(t)


(n, m) = [int(x) for x in input().split()]
busy = [int(x) for x in input().split()]
s = 0
cur = 1
for el in busy:
	if el == cur:
		dif = 0
	elif el > cur:
		dif = el - cur
	else:
		dif = n - cur + el
	s += dif
	cur = el
print(s)


(n, m) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
houses = 0
if a[0] != 1:
	houses += a[0] - 1
for i in range(1, m):
	if a[i] > a[i - 1]:
		houses += a[i] - a[i - 1]
	elif a[i] == a[i - 1]:
		continue
	else:
		houses += n - a[i - 1] + a[i]
print(houses)


(n, m) = map(int, input().split())
task = list(map(int, input().split()))
t = 0
curr = 1
for i in range(m):
	if task[i] < curr:
		t += n - curr + task[i]
	else:
		t += task[i] - curr
	curr = task[i]
print(t)


(n, m) = map(int, input().split())
if n >= 2 and n <= 100000 and (m >= 1) and (m <= 100000):
	a = list(map(int, input().split()))
	if len(a) == m:
		check = 1
		for i in a:
			if i < 1 or i > n:
				check = 0
				break
		if check == 1:
			house = 1
			time = 0
			for i in a:
				if i > house:
					time += i - house
					house = i
				elif i < house:
					time += n - house + i
					house = i
			print(time)


from sys import stdin, stdout
(num_houses, tasks) = map(int, stdin.readline().split())
House = [int(i) for i in stdin.readline().split()]
count = 0
for i in range(0, len(House)):
	try:
		if i == 0:
			count += House[i] - 1
		elif House[i] < House[i - 1]:
			count += num_houses - House[i - 1] + House[i]
		else:
			count += House[i] - House[i - 1]
	except IndexError:
		break
stdout.write(str(count))


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
s = 0
p = 1
for i in range(0, len(a)):
	s += (a[i] - p) % n
	p = a[i]
print(s)


line = input()
line = line.split()
n = int(line[0])
m = int(line[1])
line = input()
line = line.split()
transfers = 0
ch = 1
for i in range(0, m):
	line[i] = int(line[i])
	k = line[i]
	if k >= ch:
		transfers = transfers + k - ch
		ch = k
		continue
	else:
		transfers = transfers + n + k - ch
		ch = k
		continue
print(transfers)


list1 = [int(x) for x in input().split(' ')]
list2 = [int(x) for x in input().split(' ')]
l = list1[0]
t = list1[1]
r = 0
if t == 1:
	print(list2[0] - 1)
else:
	for i in range(1, t):
		if list2[i] < list2[i - 1]:
			r += 1
	print(r * l + list2[t - 1] - 1)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
time = a[0] - 1
i = 1
while i < m:
	if a[i] >= a[i - 1]:
		time += a[i] - a[i - 1]
	else:
		time += n - a[i - 1] + a[i]
	i += 1
print(time)


import re
(n, m) = [int(p) for p in input().split()]
r = input()
s = re.split('\\s+', r)
t = 0
for i in range(m):
	s[i] = int(s[i])
d = 0
k = 1
for i in range(m):
	if s[i] >= k:
		d += s[i] - k
		k = s[i]
	else:
		d += s[i] + n - k
		k = s[i]
print(d)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
previous_n = 1
s = 0
for e in a:
	if previous_n > e:
		s += e + n - previous_n
	else:
		s += e - previous_n
	previous_n = e
print(s)


def dist(a, b, n):
	if b >= a:
		return b - a
	return n + b - a
(n, m) = map(int, input().split())
l = [1] + [int(x) for x in input().split()]
print(sum([dist(l[i], l[i + 1], n) for i in range(m)]))


fistIn = input()
(n, m) = (int(x) for x in fistIn.split())
secIn = input()
houses = [int(x) for x in secIn.split()]
xania = 1
steps = 0
for x in range(m):
	if xania < houses[x]:
		steps = steps + (houses[x] - xania)
		xania = houses[x]
	elif xania > houses[x]:
		steps = steps + (n - xania + houses[x])
		xania = houses[x]
print(steps)


(n, m) = input().split()
n = int(n)
m = int(m)
A = input()
B = []
for i in A.split():
	B.append(int(i))
sum = B[0] - 1
for j in range(0, m - 1):
	if B[j] - B[j + 1] > 0:
		sum = sum + (n - B[j] + B[j + 1])
	elif B[j] - B[j + 1] < 0:
		sum = sum + B[j + 1] - B[j]
print(sum)


(n, m) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
t = 0
l.insert(0, 1)
for i in range(0, len(l) - 1):
	if l[i + 1] >= l[i]:
		t = l[i + 1] - l[i] + t
	else:
		t = t + n + l[i + 1] - l[i]
print(t)


(n, m) = list(map(int, input().split()))
num = list(map(int, input().split()))
(pos, res) = (1, 0)
for i in num:
	if i > pos:
		res += i - pos
		pos = i
	elif i != pos:
		res += n - pos + 1
		pos = 1
		res += i - pos
		pos = i
print(res)


(n, m) = map(int, input().split())
mi = input().split()
x = 0
i = 0
while i < m - 1:
	a = int(mi[i])
	b = int(mi[i + 1])
	if a > b:
		x = x + 1
	i = i + 1
print(int(x * n + int(mi[-1]) - 1))


(n, m) = [int(x) for x in input().split()]
issues = input().split()
timeSpent = int(issues[0]) - 1
pos = int(issues[0])
for x in issues[1:]:
	if int(x) < pos:
		timeSpent += n - pos + 1
		pos = 1
	timeSpent += int(x) - pos
	pos = int(x)
print(timeSpent)


(n, m) = map(int, input().split())
tasks = [int(t) for t in input().split()]
h = 1
total = 0
for t in tasks:
	d = t - h
	if d >= n:
		d -= n
	elif d < 0:
		d = n + d
	total += d
	h = t
print(total)


from collections import *
from bisect import *
from heapq import *
from fractions import *
import sys
input = sys.stdin.readline
t = 1
while t:
	t -= 1
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	s = 0
	ip = 1
	for i in range(m):
		if ip <= a[i]:
			s += a[i] - ip
			ip = a[i]
		else:
			s += n - ip
			s += a[i]
			ip = a[i]
	print(s)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
ans = arr[0] - 1
temp = arr[0]
current = arr[0]
for i in range(m):
	if temp != arr[i]:
		if current > arr[i]:
			ans = ans + (n - current) + arr[i]
		else:
			ans = ans + (arr[i] - current)
	temp = arr[i]
	current = arr[i]
print(ans)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
last = 1
for i in b:
	if last == i:
		pass
	elif i > last:
		ans = ans + (i - last)
		last = i
	elif i < last:
		ans = ans + a[0] - last + i
		last = i
print(ans)


(a, b) = input().split()
a = int(a)
b = int(b)
sums = 0
lists = input().split()
for i in range(0, b - 1):
	if int(lists[i]) > int(lists[i + 1]):
		sums += a
sums = sums + int(lists[b - 1]) - 1
print(sums)


line = input().split()
n = int(line[0])
m = int(line[1])
line = input().split()
list = [int(x) for x in line]
time = 0
list.insert(0, 1)
for i in range(m):
	if list[i + 1] >= list[i]:
		time += list[i + 1] - list[i]
	else:
		time += n + list[i + 1] - list[i]
print(time)


(n, m) = map(int, input().split())
pos = 1
ans = 0
l = input().split()
for i in l:
	a = int(i)
	if a >= pos:
		ans += a - pos
	else:
		ans += n - pos + a
	pos = a
print(ans)


(n, m) = map(int, input().split())
houses = [int(x) for x in input().split()]
ans = houses[0] - 1
for x in range(len(houses) - 1):
	if houses[x] == houses[x + 1]:
		continue
	if houses[x] < houses[x + 1]:
		ans += houses[x + 1] - houses[x]
	else:
		ans += n - (houses[x] - houses[x + 1])
print(ans)


(x, y) = input().split(' ')
x = int(x)
y = int(y)
list1 = input().split(' ')
list1 = [int(a) for a in list1]
prev = 0
count = 0
for i in range(0, len(list1)):
	if i == 0:
		count += list1[i] - 1
		prev = list1[i]
	elif prev < list1[i]:
		count += list1[i] - prev
		prev = list1[i]
	elif prev > list1[i]:
		count += x - (prev - list1[i])
		prev = list1[i]
print(count)


(n, m) = map(int, input().split())
s = list(map(int, input().split()))
c = s[0]
ans = c - 1
for i in range(1, len(s)):
	if s[i] > c:
		ans += s[i] - c
	elif s[i] < c:
		ans += n - c
		ans += s[i]
	c = s[i]
print(ans)


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
last = 1
out = 0
for i in a:
	if i >= last:
		out += i - last
	else:
		out += i + n - last
	last = i
print(out)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
answer = 0
current = 1
for i in a:
	if current <= i:
		answer += i - current
	else:
		answer += n - current + i
	current = i
print(answer)


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
s = 0
count = 0
for a in A:
	count += (a - 1 - s + N) % N
	s = a - 1
print(count)


(x, y) = map(int, input().split())
l = list(map(int, input().split()))
count = 0
prev = 1
for i in l:
	count += (i - prev) % x
	prev = i
print(count)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
(time, cur) = (0, 1)
for h in a:
	if cur <= h:
		time += h - cur
	else:
		time += n - cur + h
	cur = h
print(time)


(num_houses, num_tasks) = map(int, input().split())
tasks = list(map(int, input().split()))
pos = 1
steps = 0
for x in range(num_tasks):
	if tasks[x] > pos:
		steps += tasks[x] - pos
	elif tasks[x] < pos:
		steps += num_houses - (pos - tasks[x])
	else:
		continue
	pos = tasks[x]
print(steps)


a = list(map(int, input().strip().split(' ')))
b = list(map(int, input().strip().split(' ')))
c = 0
current = 1
for i in b:
	if current < i:
		c += i - current
		current = i
	elif current != i:
		c += a[0] + i - current
		current = i
print(c)


(n, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
i = 0
c = 0
p = 0
q = 0
while i < k:
	if i == 0:
		c = a[i] - 1
	elif a[i - 1] - a[i] <= 0:
		c = c + abs(a[i - 1] - a[i])
	else:
		p = n - a[i - 1]
		q = a[i] - 1
		c = c + 1 + p + q
	i += 1
print(c)


line = input()
(n, m) = line.split()
n = int(n)
m = int(m)
line2 = input()
task = line2.split()
for i in range(m):
	task[i] = int(task[i])
	ans = task[0] - 1
for i in range(1, m):
	if task[i - 1] > task[i]:
		ans = ans + n + task[i] - task[i - 1]
	else:
		ans = ans + task[i] - task[i - 1]
print(ans)


(n, m) = map(int, input().split())
house = list(map(int, input().split()))
ans = 0
i = 1
for h in house:
	diff = h - i
	if diff >= 0:
		ans += diff
	else:
		ans += n + diff
	i = h
print(ans)


(n, m) = input().split()
N = int(n)
M = int(m)
a = []
a = list(map(int, input().strip().split()))[:M]
T = a[0] - 1
for j in range(1, M):
	if a[j] >= a[j - 1]:
		T = T + (a[j] - a[j - 1])
	else:
		T = T + N + (a[j] - a[j - 1])
print(T)


(n, m) = map(int, input().split())
ans = 0
prev_house = 1
for curr_house in map(int, input().split()):
	ans += (curr_house - prev_house) % n
	prev_house = curr_house
print(ans)


line1 = input().split()
line2 = input().split()
m = []
for i in line1:
	m.append(int(i))
a1 = m[0]
a2 = m[1]
n = []
for x in line2:
	n.append(int(x))
s = 0
for y in range(len(n)):
	if y == a2 - int(1):
		break
	if n[y] <= n[y + 1]:
		s = int(s) + (n[y + 1] - n[y])
	if n[y] > n[y + 1]:
		s = int(s) + (n[y + 1] - n[y] + a1)
print(str(s + n[0] - int(1)))


n = int(input().split()[0])
prev = 1
s = 0
for v in map(int, input().split()):
	s += (v - prev) % n
	prev = v
print(s)


(a, b) = [int(i) for i in input().split(' ')]
l = [int(i) for i in input().split(' ')]
c = 0
c = l[0] - 1
for i in range(0, len(l) - 1):
	if l[i + 1] >= l[i]:
		c = l[i + 1] - l[i] + c
	else:
		c = c + (a - l[i]) + l[i + 1]
print(c)


def fastio():
	import sys
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
(n, m) = I()
l = I()
ans = 0
if l[0] != 1:
	ans += l[0] - 1
for i in range(m - 1):
	ans += (l[i + 1] - l[i]) % n
print(ans)


(n, m) = [int(i) for i in input().split()]
a = [int(j) for j in input().split()]
summ = a[0] - 1
for i in range(1, m):
	if a[i - 1] > a[i]:
		summ += n - a[i - 1] + a[i]
	else:
		summ += a[i] - a[i - 1]
print(summ)


a1 = list(map(int, input().split()))
a2 = list(map(int, input().split()))
dist = 0
dist = a2[0] - 1
for x in range(0, a1[1] - 1):
	if a2[x] < a2[x + 1]:
		dist = dist + a2[x + 1] - a2[x]
	elif a2[x] == a2[x + 1]:
		continue
	else:
		dist = dist + a1[0] - a2[x] + a2[x + 1]
print(dist)


(n, m) = input().split()
n = int(n)
m = int(m)
L = [int(i) for i in input().split()]
time = 0
for j in range(m):
	if j == 0:
		time = time + L[j] - 1
	elif L[j] >= L[j - 1]:
		time = time + L[j] - L[j - 1]
	else:
		time = time + n - L[j - 1] + L[j]
print(int(time))


(n, m) = [int(x) for x in input().split(' ')]
tasks = [int(x) for x in input().split(' ')]
pos = 1
time = 0
for task in tasks:
	if pos < task:
		time += task - pos
	elif pos > task:
		time += n - pos + task
	pos = task
print(time)


from sys import stdin, stdout
(n, m) = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
x = 1
ans = 0
for i in range(m):
	if a[i] > x:
		ans += a[i] - x
	elif a[i] < x:
		ans += n - x
		ans += a[i]
	x = a[i]
print(ans)


(n, m) = [int(i) for i in input().split()]
t = 0
l = list(input().split())
t += int(l[0]) - 1
for i in range(1, len(l)):
	li = int(l[i])
	li1 = int(l[i - 1])
	if li >= li1:
		t += li - li1
	else:
		t += n + li - li1
print(t)


def ia():
	return list(map(int, input().split()))

def ca():
	return input().split()

def ii():
	return int(input())
(n, m) = ia()
a = ia()
x = 0
y = 1
for i in range(m):
	x = x + (a[i] - y) % n
	y = a[i]
print(x)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
i = 1
t = 0
for _ in range(m):
	f = arr[_]
	if f >= i:
		t = t + f - i
	else:
		t = t + n - i + f
	i = f
print(t)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
(res, cur) = (0, 1)
for i in a:
	if cur <= i:
		res += i - cur
	else:
		res += n - cur + i
	cur = i
print(res)


s = input()
a = s.split()
n = int(a[0])
m = int(a[1])
s = input()
temp = s.split()
a = [1 for i in range(m + 1)]
for i in range(m):
	a[i + 1] = int(temp[i])
ans = 0
for i in range(1, m + 1):
	if a[i - 1] < a[i]:
		ans += a[i] - a[i - 1]
	if a[i - 1] > a[i]:
		ans += n - a[i - 1] + a[i]
print(ans)


p = input
i = int
n = i(p().split()[0])
k = 1
s = 0
for i in map(i, p().split()):
	s += [n - k + i, i - k][i >= k]
	k = i
print(s)


inStr = input()
n = int(inStr.split()[0])
m = int(inStr.split()[1])
inStr = input()
tasks = [int(x) for x in inStr.split()]

def move(source, dest):
	if source == dest:
		return 0
	elif dest > source:
		return dest - source
	else:
		return n - source + dest
cur = 1
ans = 0
for t in tasks:
	ans += move(cur, t)
	cur = t
print(ans)


def listofint(int1, int2):
	return list(range(int1, int2 + 1))
(n, m) = list(map(int, input().split()))
houses = list(map(int, input().split()))
my_list = listofint(1, houses[0])
count = 0
for i in range(1, m):
	if houses[i - 1] > houses[i]:
		count = count + 1
print(n * count + houses[-1] - 1)


__author__ = 'myduomilia'
(n, m) = list(map(int, input().split()))
ans = 0
arr = list(map(int, input().split()))
arr.insert(0, 1)
for i in range(m):
	if arr[i] <= arr[i + 1]:
		ans += arr[i + 1] - arr[i]
	else:
		ans += n - (arr[i] - arr[i + 1])
print(ans)


(n, m) = map(int, input().split())
house = list(map(int, input().split()))
house.insert(0, 1)
ans = 0
for i in range(1, m + 1):
	if house[i] >= house[i - 1]:
		ans += house[i] - house[i - 1]
	else:
		ans += n - house[i - 1] + house[i]
print(ans)


def to(a, b, n):
	if a <= b:
		return b - a
	else:
		return n - a + b
(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.insert(0, 1)
k = 0
for i in range(1, len(a)):
	k += to(a[i - 1], a[i], n)
print(k)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
count = 0
count += a[0] - 1
for i in range(1, len(a)):
	if a[i] >= a[i - 1] and a[i] <= n:
		count += a[i] - a[i - 1]
	else:
		count += n - a[i - 1] + a[i]
print(count)


(n, m) = [int(i) for i in input().split()]
k = [int(j) for j in input().split()]
(i, j) = (0, 0)
cnt = 0
for i in range(m - 1):
	if k[i + 1] < k[i]:
		cnt += n
cnt += k[m - 1]
cnt = cnt - 1
print(cnt)


def main():
	(n, m) = [int(i) for i in input().split(' ')]
	t = [int(i) - 1 for i in input().split()]
	ans = 0
	pos = 0
	for i in range(m):
		if t[i] > pos:
			ans += t[i] - pos
		elif t[i] < pos:
			ans += n - pos + t[i]
		pos = t[i]
	print(ans)
main()


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
i = 1
time = 0
for c in tasks:
	if i <= c:
		time += c - i
	else:
		time += c + (n - i)
	i = c
print(time)


(n, m) = list(map(int, input().split()))
t = list(map(int, input().split()))
t.insert(0, 1)
dist = 0
for i in range(1, len(t)):
	if t[i - 1] <= t[i]:
		dist = dist + t[i] - t[i - 1]
	else:
		dist = dist + n - t[i - 1] + t[i]
print(dist)


(n, m) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
i = 1
k = 0
c = 0
p = m
while m:
	if i < a[k]:
		c += a[k] - i
		i = a[k]
		k = k + 1
		m = m - 1
		if m == 0:
			break
	if i == a[k]:
		m = m - 1
		k = k + 1
		if m == 0:
			break
	if i > a[k]:
		o = (n - i) % n
		c += a[k] + o
		i = a[k]
		m = m - 1
		k = k + 1
		if m == 0:
			break
print(c)


(n, m) = map(int, input().split())
horario = list(map(int, input().split()))
soma = horario[0] - 1
for i in range(len(horario) - 1):
	if horario[i] <= horario[i + 1]:
		soma += horario[i + 1] - horario[i]
	else:
		soma += n - horario[i] + horario[i + 1]
print(soma)


a = [int(p) for p in input().split()]
b = [int(j) for j in input().split()]
n = a[0]
m = a[1]
total = 0
b.insert(0, '1')
for i in range(1, m + 1):
	if int(b[i]) >= int(b[i - 1]):
		total = total + int(b[i]) - int(b[i - 1])
	else:
		total = total + int(b[i]) - int(b[i - 1]) + n
print(total)


(n, m) = map(int, input().split())
current = 1
res = 0
for el in map(int, input().split()):
	if el >= current:
		res += el - current
	else:
		res += el + (n - current)
	current = el
print(res)


def proA(n, arr):
	cost = arr[0] - 1
	for i in range(len(arr) - 1):
		if arr[i + 1] - arr[i] < 0:
			cost += n + (arr[i + 1] - arr[i])
		else:
			cost += arr[i + 1] - arr[i]
	return cost
arr = list(map(int, input().split()))
(n, k) = arr
arr = list(map(int, input().split()))
print(proA(n, arr))


f = lambda : map(int, input().split())
(n, m) = f()
lst = list(f())
t = lst[0] - 1
for i in range(1, m):
	if lst[i] >= lst[i - 1]:
		t += lst[i] - lst[i - 1]
	else:
		t += n - (lst[i - 1] - lst[i])
print(t)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
(s, p) = (0, 1)
for i in arr:
	s += (i - p) % n
	p = i
print(s)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
(c, h) = (0, 1)
for i in l:
	if h < i:
		c += i - h
	elif h > i:
		c += n - h + i
	h = i
print(c)


(no_of_houses, number_of_tasks) = [int(i) for i in input().split(' ')]
tasks = [int(i) for i in input().split(' ')]
time = tasks[0] - 1
for i in range(1, number_of_tasks):
	if tasks[i] >= tasks[i - 1]:
		time += tasks[i] - tasks[i - 1]
	else:
		time += no_of_houses - tasks[i - 1] + tasks[i]
print(time)


(n, m) = [int(x) for x in input().split(' ')]
task = [int(x) for x in input().split(' ')]
task = [1] + task
s = 0
for i in range(len(task) - 1):
	if task[i] == task[i + 1]:
		continue
	elif task[i + 1] > task[i]:
		s += task[i + 1] - task[i]
	else:
		s += n - task[i] + 1
		s += task[i + 1] - 1
print(s)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
res = 0
pos = 1
for x in a:
	if pos <= x:
		res += x - pos
	else:
		res += x + n - pos
	pos = x
print(res)


a = input().strip().split()
a = [int(a[0]), int(a[1])]
b = input().strip().split()
b = [int(i) for i in b]
n = 0
l = 1
t = 0
for i in b:
	if i + n * a[0] >= l:
		t = t + i + n * a[0] - l
		l = i + n * a[0]
	else:
		n = n + 1
		t = t + i + n * a[0] - l
		l = i + n * a[0]
print(t)


s = input().split()
n = [int(i) for i in input().split()]
k = 0
a = 0
for i in range(len(n) - 1):
	if n[i + 1] >= n[i]:
		if k == 0:
			k += n[i + 1] - 1
		else:
			k += n[i + 1] - n[i]
	else:
		a += 1
		k = int(s[0]) * a - 1 + n[i + 1]
print(k)


(n, m) = [int(el) for el in input().split(' ')]
a = input().split(' ')
res = int(a[0]) - 1
for i in range(1, m):
	if int(a[i]) < int(a[i - 1]):
		res += n - int(a[i - 1]) + int(a[i])
	else:
		res += int(a[i]) - int(a[i - 1])
print(res)


def main():
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	L = [int(x) for x in input().split()]
	c = L[0] - 1
	for i in range(1, m):
		c += n - L[i - 1] + L[i] if L[i - 1] > L[i] else L[i] - L[i - 1]
	print(c)
main()


(n, m) = map(int, input().split())
points = [1] + list(map(int, input().split()))
s = 0
for i in range(1, len(points)):
	diff = points[i] - points[i - 1]
	s += diff if points[i] >= points[i - 1] else n + diff
print(s)


(n, m) = map(int, input().split())
task = input().split()
ans = int(task[0]) - 1
for i in range(m - 1):
	if int(task[i + 1]) > int(task[i]):
		ans += int(task[i + 1]) - int(task[i])
	elif int(task[i + 1]) == int(task[i]):
		ans += 0
	else:
		ans += int(task[i + 1]) - int(task[i]) + n
print(ans)


(n, m) = [int(var) for var in input().split()]
tasks = [int(var) for var in input().split()]
in_house = 1
steps = 0
for task_house in tasks:
	if in_house <= task_house:
		steps += task_house - in_house
	else:
		steps += n - in_house + task_house
	in_house = task_house
print(steps)


from sys import stdin
(n, t) = [int(x) for x in stdin.readline().split()]
s = [int(x) for x in stdin.readline().split()]
sumi = s[0] - 1
current_position = s[0]
for i in range(1, t):
	if s[i] == current_position:
		continue
	if s[i] > current_position:
		sumi += s[i] - current_position
	else:
		sumi += n - current_position + 1
		sumi += s[i] - 1
	current_position = s[i]
print(sumi)


q = input()
n = int(q.split()[0])
m = int(q.split()[1])
l = list(map(int, input().strip().split()))
t = 1
w = 0
for i in range(m):
	if l[i] < t:
		w += n - t + l[i]
		t = l[i]
	else:
		w += l[i] - t
		t = l[i]
print(w)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
r = 0
temptask = 1
for task in tasks:
	if task > temptask:
		r += task - temptask
	elif task < temptask:
		r += n + task - temptask
	temptask = task
print(r)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
last = 1
counter = 0
for item in a:
	if item >= last:
		counter += item - last
	else:
		counter += n - last + item
	last = item
print(counter)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
t = -1
i = 0
while i < m:
	if i == m - 1:
		t += a[m - 1]
		break
	elif a[i] <= a[i + 1]:
		i += 1
	elif a[i] > a[i + 1]:
		t += n
		i += 1
print(t)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
count = 0
pre = 1
for i in range(m):
	nex = arr[i]
	if nex < pre:
		v = abs(pre - nex)
		count += n - v
	elif nex > pre:
		count += nex - pre
	pre = nex
print(count)


(n, m) = [int(i) for i in input().split()]
jobs = [int(i) for i in input().split()]
total = 0
current = 0
for a in jobs:
	nextJob = a - 1
	if nextJob >= current:
		total += nextJob - current
	else:
		total += n - current + nextJob
	current = nextJob
print(total)


(n, k) = map(int, input().split())
lis = map(int, input().split())
ans = 0
last = 1
for i in lis:
	if i >= last:
		ans += i - last
	else:
		ans += n - last + 1 + i - 1
	last = i
print(ans)


(num_houses, num_tasks) = map(int, input().split())
tasks = list(map(int, input().split()))
current_house = 1
solution = 0
for task in tasks:
	if task >= current_house:
		solution += task - current_house
	else:
		solution += num_houses - current_house + 1
		solution += task - 1
	current_house = task
print(solution)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
x = 1
ans = 0
for j in range(m):
	v = x - a[j]
	if v > 0:
		ans = ans + n - v
		x = x - v
	else:
		ans = ans - v
		x = x - v
print(ans)


[n, m] = [int(a) for a in input().split(' ')]
groups = [int(a) for a in input().split(' ')]
count = 0
k = groups[count] - 1
while count < m - 1:
	i = groups[count]
	if i < groups[count + 1]:
		k = k + (groups[count + 1] - i)
	if i > groups[count + 1]:
		k = k + (n - i) + 1 + (groups[count + 1] - 1)
	count += 1
print(k)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
s = 0
a = 1
for i in l:
	if a <= i:
		s += i - a
		a = i
	else:
		s += n - a + i
		a = i
print(str(s))


v = [int(i) for i in input().split()]
s = [int(i) for i in input().split()]
diff = 1
r = 0
for i in range(len(s)):
	if s[i] >= diff:
		r += s[i] - diff
	else:
		r += v[0] - (diff - s[i])
	diff = s[i]
print(r)


(n, m) = map(int, input().strip().split())
arry = list(map(int, input().strip().split()))[:m]
k = -1
if m == 1:
	k = m - 1
for i in range(m - 1):
	if i == 0:
		k += arry[0]
	if arry[i] < arry[i + 1]:
		k += arry[i + 1] - arry[i]
	elif arry[i] > arry[i + 1]:
		k += n - arry[i] + arry[i + 1]
print(k)


import sys
values = [int(x) for x in sys.stdin.readline().split()]
(n_houses, n_needed) = (values[0], values[1])
destinations = [int(x) for x in sys.stdin.readline().split()]
result = 0
cur_place = 1
for destination in destinations:
	if destination < cur_place:
		result += n_houses - cur_place + destination
	else:
		result += destination - cur_place
	cur_place = destination
print(result)


l1 = input().split()
m = int(l1[1])
l = input().split()
time = int(l[0]) - 1
for i in range(0, m - 1):
	if int(l[i]) < int(l[i + 1]):
		time = time + int(l[i + 1]) - int(l[i])
	elif l[i] == l[i + 1]:
		pass
	else:
		time = time + int(l1[0]) + int(l[i + 1]) - int(l[i])
print(time)


line = input().split()
n = int(line[0])
m = int(line[1])
s = input().split()
x = 1
k = 0
for i in range(0, m):
	if int(s[i]) > x:
		k += int(s[i])
		k -= x
	elif x > int(s[i]):
		k += n - x + int(s[i])
	x = int(s[i])
print(k)


n = [int(i) for i in input().split()]
n1 = n[0]
m1 = n[1]
x = [int(i) for i in input().split()]
t = 0
t = x[0] - 1
for i in range(len(x)):
	if i == len(x) - 1:
		break
	if x[i] == x[i + 1]:
		t = t + 0
	elif x[i] < x[i + 1]:
		t = t + x[i + 1] - x[i]
	elif x[i + 1] < x[i]:
		a = x[i] - x[i + 1]
		t = t + n1 - a
print(t)


(a, n) = map(int, input().split())
N = [int(num) for num in input().split(' ', n - 1)]
i = 0
t = N[0] - 1
while i < n - 1:
	if N[i] <= N[i + 1]:
		t = t + (N[i + 1] - N[i])
	else:
		t = t + (a - N[i] + N[i + 1])
	i += 1
print(t)


t = [int(x) for x in input().split()]
a = [int(y) for y in input().split()]
n = t[0]
m = t[1]
s = 0
k = 1
for i in a:
	if i < k:
		s = s + (n - k)
		k = 0
	s = s + (i - k) % n
	k = i
print(s)


(N, M) = map(int, input().split())
A = list(map(int, input().split()))
now = 1
cnt = 0
for i in range(M):
	if A[i] >= now:
		cnt += A[i] - now
	else:
		cnt += N - now + A[i]
	now = A[i]
print(cnt)


__author__ = 'SAUMYA'

def solve():
	(n, m) = map(int, input().split())
	arr = list(map(int, input().split()))
	ans = arr[0] - 1
	for i in range(1, m):
		if arr[i] < arr[i - 1]:
			ans += n - arr[i - 1] + arr[i]
		elif arr[i] > arr[i - 1]:
			ans += arr[i] - arr[i - 1]
	print(ans)
solve()


(n, m) = map(int, input().split())
L = [int(i) for i in input().split()]
c = 0
for i in range(len(L)):
	try:
		if L[i] <= L[i + 1]:
			c += L[i + 1] - L[i]
		else:
			c += n - L[i] + L[i + 1]
	except IndexError:
		pass
print(c + L[0] - 1)


(n, m) = map(int, input().split())
mas = [int(x) for x in input().split()]
time = 0
begin = 1
for i in mas:
	time += (i - begin) % n
	begin = i
print(time)


import sys
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
count = 0
tr = [x for x in range(1, n + 1)]
prev = 1
for i in range(m):
	if prev > a[i]:
		count += n - prev + a[i]
	elif prev < a[i]:
		count += a[i] - prev
	prev = a[i]
print(count)


(n, m) = input().split()
n = int(n)
m = int(m)
x = input()
a = x.split()
c = 1
j = 0
while j < len(a) - 1:
	if int(a[j]) > int(a[j + 1]):
		c = c + 1
	j = j + 1
print(c * n - (n - int(a[len(a) - 1])) - 1)


(n, m) = [int(x) for x in input().split()]
nums = [int(x) for x in input().split()]
h = 1
t = 0
for x in nums:
	t += (x - h) % n
	h = x
print(t)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
ans = a[0] - 1
for i in range(m - 1):
	diff = abs(a[i] - a[i + 1])
	if diff != 0:
		if a[i + 1] > a[i]:
			ans += diff
		else:
			ans += n - diff
print(ans)


(n, m) = map(int, input().split())
a = [int(x) for x in input().split()]
place = 1
lenth = 0
for i in a:
	if i > place:
		lenth = lenth + i - place
		place = i
	elif i == place:
		pass
	elif i < place:
		lenth = lenth + n - place + i
		place = i
print(lenth)


(a, b) = map(int, input().split())
tasks = list(map(int, input().split()))
count = 0
timeunit = 0
location = 1
for task in tasks:
	if count == 0:
		timeunit = task - location
	elif tasks[count - 1] <= task:
		timeunit = timeunit + task - location
	else:
		timeunit = timeunit + a - location + task
	count += 1
	location = task
print(timeunit)


(n, m) = map(int, input().split())
A = list(map(int, input().split()))
last = 1
ans = 0
for a in A:
	cnt = a - last
	if cnt < 0:
		cnt += n
	ans += cnt
	last = a
print(ans)


(n, m) = map(int, input().split())
pos = int(0)
cnt = int(0)
for x in input().split():
	x = int(x) - 1
	if pos <= x:
		cnt += x - pos
	else:
		cnt += n - (pos - x)
	pos = x
print(cnt)


(n, m) = [int(i) for i in input().split()]
lst = [int(i) for i in input().split()]
total = 0
for i in range(m - 1):
	if lst[i] > lst[i + 1]:
		total += 1
print(n * total + lst[-1] - 1)


(n, m) = map(int, input().split())
num_list = list(map(int, input().split()))
k = 1
t = 0
for i in num_list:
	if i > k:
		t += i - k
		k = i
	elif i < k:
		t += n - k + i
		k = i
print(t)


hh = input()
(n, m) = [int(i) for i in hh.split()]
hhh = input()
list1 = [int(i) for i in hhh.split()]
t = 0
t += list1[0] - 1
for i in range(1, m):
	if list1[i] >= list1[i - 1]:
		t += list1[i] - list1[i - 1]
	else:
		t += list1[i] - list1[i - 1] + n
print(t)


(n, m) = [int(i) for i in input().split()]
list1 = [int(i) for i in input().split()]
list1.insert(0, 1)
initial = 0
for i in range(1, m + 1):
	gap = list1[i] - list1[i - 1]
	if gap >= 0:
		initial = initial + gap
	else:
		initial = initial + n + gap
print(initial)


(n, m) = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
arr = [1] + arr
tot = 0
for (i, a) in enumerate(arr[:-1]):
	tot += (arr[i + 1] - arr[i]) % n
print(tot)


(n, m) = map(int, input().strip().split(' '))
tasks = tuple(map(int, input().strip().split(' ')))
ans = 0
loc = 1
for t in tasks:
	ans += (n - loc + t, t - loc)[loc <= t]
	loc = t
print(ans)


(n, m) = map(int, input().split(' '))
arr = list(map(int, input().split()))
var = 1
ans = 0
for current in range(m):
	ans += (arr[current] - var + n) % n
	var = arr[current]
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
cur_time = 0
prev = 1
first = True
for i in a:
	if i < prev:
		cur_time += n - prev + i
		prev = i
	else:
		cur_time += i - prev
		prev = i
print(cur_time)


(n, m) = [int(x) for x in input().split()]
Arr = [int(r) for r in input().split()]
acu = 0
aux = 0
for i in range(m - 1):
	if Arr[i] <= Arr[i + 1]:
		aux = Arr[i + 1] - Arr[i]
	elif Arr[i] == n:
		aux = Arr[i + 1]
	else:
		aux = n - Arr[i] + Arr[i + 1]
	acu += aux
acu += Arr[0] - 1
print(acu)


(n, m) = map(int, input().split())
val = list(map(int, input().split()))
count = 0
for i in range(len(val) - 1):
	if val[i + 1] < val[i]:
		count += 1
final = count * n + val[len(val) - 1]
print(final - 1)


(n, m) = map(int, input().split())
line = list(map(int, input().split()))
cur = 1
k = 0
for i in line:
	if cur == i:
		continue
	if cur < i:
		k += i - cur
	else:
		k += n - cur + i
	cur = i
print(k)


(n, m) = list(map(int, input().split()))
l = list(map(int, input().split()))
(curr, cycle) = (1, 0)
for i in l:
	if i > curr:
		curr = i
	elif i < curr:
		cycle += 1
		curr = i
if cycle:
	print(n * cycle + curr - 1)
else:
	print(curr - 1)


(n, m) = [int(i) for i in input().split()]
tasks = [1] + [int(i) for i in input().split()]
res = 0
i = 0
while i < m:
	if tasks[i] < tasks[i + 1]:
		res += tasks[i + 1] - tasks[i]
	elif tasks[i] > tasks[i + 1]:
		res += n - tasks[i] + tasks[i + 1]
	else:
		while tasks[i] == tasks[i + 1]:
			i += 1
			if i == m:
				break
		continue
	i += 1
print(str(res))


def ringroad(n, array):
	house = 1
	time = 0
	for i in range(len(array)):
		if house > array[i]:
			time += n - house + 1 + (array[i] - 1)
			house = array[i]
		elif house < array[i]:
			time += array[i] - house
			house = array[i]
		else:
			time += 0
	print(time)
(n, m) = input().split()
array = [int(x) for x in input().split()]
ringroad(int(n), array)


ar = []
for i in input().split(' '):
	ar.append(int(i))
curr = 1
count = 0
for i in input().split(' '):
	temp = int(i)
	if temp > curr:
		count = count + temp - curr
		curr = temp
	elif temp < curr:
		count = count + ar[0] - (curr - temp)
		curr = temp
print(count)


(n, m) = input().split()
n = int(n)
m = int(m)
L = map(int, input().split())
L = list(L)
steps = 0
prev = 1
for i in range(m):
	house = L[i]
	if L[i] < prev:
		steps += n - prev + 1
		steps += abs(1 - house)
	elif L[i] > prev:
		steps += house - prev
	prev = house
print(steps)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
time = a[0] - 1
last = a[0]
for task in a[1:]:
	if last <= task:
		time += task - last
	else:
		time += n - last + task
	last = task
print(time)


string = input()
s = string.split(' ')
n = int(s[0])
m = int(s[1])
a = [0] * m
string = input()
k = string.split(' ')
for i in range(m):
	a[i] = int(k[i]) - 1
step = 0
rnd = 0
for i in range(m - 1):
	if a[i] > a[i + 1]:
		rnd += 1
step = rnd * n + a[m - 1]
print(step)


def calc(n, m, arr):
	pos = 1
	time = 0
	for house in arr:
		if house == pos:
			continue
		if house > pos:
			time += house - pos
		else:
			time += n - pos
			time += house
		pos = house
	return time
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
print(calc(n, m, arr))


(n, m) = [int(x) for x in input().split()]
a = [y for y in reversed([int(x) for x in input().split()])]
t = 0
cur_pos = 1
while len(a) > 0:
	nxt = a.pop()
	if nxt > cur_pos:
		t += nxt - cur_pos
	elif nxt < cur_pos:
		t += n - (cur_pos - nxt)
	cur_pos = nxt
print(t)


(n, m) = map(int, input().split())
work = list(map(int, input().split()))[:m]
x = 1
ch = 0
for i in range(len(work)):
	if work[i] > x:
		ch += work[i] - x
		x = work[i]
	elif work[i] == x:
		continue
	else:
		ch = ch + (n - x) + work[i]
		x = work[i]
print(ch)


from sys import stdin, stdout

def main():
	(n, m) = [int(i) for i in stdin.readline().split()]
	a = [int(i) for i in stdin.readline().split()]
	a.insert(0, 1)
	count = 0
	for i in range(1, m + 1):
		count += (a[i] - a[i - 1] + n) % n
	stdout.write(str(count) + '\n')
main()


__author__ = 'Utena'

def l(x):
	if x >= 0:
		return x
	else:
		return n + x
t = 1
a = 0
(n, m) = map(int, input().split())
s = list(map(int, input().split()))
for i in range(m):
	a += l(s[i] - t)
	t = s[i]
print(a)


from sys import stdin
_input = stdin.readline
(_range, _int) = (range, int)

def solution():
	(n, m) = [_int(i) for i in _input().split()]
	arr = [_int(i) for i in _input().split()]
	rounds = 0
	ans = 0
	for i in _range(1, m):
		if arr[i - 1] > arr[i]:
			rounds += 1
			ans = arr[i] - 1
		else:
			ans = arr[i] - 1
	print(rounds * n + ans)
solution()


l = input().split()
n = int(l[0])
m = int(l[1])
l = input().split()
now = 1
ans = 0
for i in range(m):
	if int(l[i]) >= now:
		ans += int(l[i]) - now
	else:
		ans += n - (now - int(l[i]))
	now = int(l[i])
print(ans)


(n, k) = input().split()
n = int(n)
k = int(k)
l = []
s = input().split()
for x in s:
	x = int(x)
	l.append(x)
l1 = []
for i in range(0, k):
	if i == k - 1:
		l1.append(l[i])
	elif l[i] > l[i + 1]:
		l1.append(l[i])
prev = l1[0]
t = prev - 1
for i in l1[1:]:
	t += n - prev + i
	prev = i
print(t)


a = [int(i) for i in input().split()]
b = [1] + [int(item) for item in input().split()]
ans = 0
for item in range(len(b) - 1):
	if b[item + 1] - b[item] >= 0:
		ans = ans + (b[item + 1] - b[item])
	else:
		ans = ans + a[0] + (b[item + 1] - b[item])
print(ans)


a = input()
b = input()
c = a.split()
d = b.split()
count = int(d[0]) - 1
i = 1
while i < int(c[1]):
	count += (int(d[i]) - int(d[i - 1])) % int(c[0])
	i += 1
print(count)


(n, m) = map(int, input().split())
a = [1] + list(map(int, input().split()))
print(sum(((n + j - i) % n for (i, j) in zip(a, a[1:]))))


(n, m) = map(int, input().split())
a = map(int, input().split())
t = 0
current = 1
for i in a:
	d = i - current
	if d < 0:
		t += n + d
	else:
		t += d
	current = i
print(t)


n = input()
m = input()
a = []
b = []
a = n.split(' ')
b = m.split(' ')
ans = 0
for i in range(0, len(b) - 1):
	if int(b[i + 1]) >= int(b[i]):
		ans += int(b[i + 1]) - int(b[i])
	else:
		ans += int(a[0]) - (int(b[i]) - int(b[i + 1]))
print(str(ans + int(b[0]) - 1))


import math
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
k = 0
p = 1
for i in range(m):
	c = a[i]
	if c < p:
		k += n - p + c
	elif c > p:
		k += min(n - p + c, c - p)
	p = c
print(k)


import math
import os
import sys
import re
import string
import itertools
import functools
import operator
from collections import Counter

def inputint():
	return int(input())
(n, m) = map(int, input().split())
A = map(int, input().split())
(res, p) = (0, 0)
for ai in A:
	diff = (ai - 1 - p + n) % n
	p = ai - 1
	res = res + diff
print(res)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
total = 0
j = 1
for i in range(m):
	total += (a[i] - j + n) % n
	j = a[i]
print(total)


(n, m) = map(int, input().split())
v = list(map(int, input().split()))
(ret, now) = (0, 1)
for i in v:
	if now <= i:
		ret += i - now
	else:
		ret += n - now + i
	now = i
print(ret)


(n, k) = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
t = 0
m = 1
while k > 0:
	k -= 1
	s = l[0]
	l.remove(s)
	if m > s:
		t += n - m + 1
		m = 1
	t += s - m
	m = s
print(t)


(n, m) = [int(i) for i in input().split()]
location = [int(i) for i in input().split()]
roundtime = 0
for i in range(1, m):
	if location[i] < location[i - 1]:
		roundtime += 1
answer = location[m - 1] - 1 + roundtime * n
print(answer)


(n, m) = map(int, input().split())
q = [int(x) for x in input().split()]
p = 1
k = 0
for qi in q:
	if qi >= p:
		k += qi - p
		p = qi
	else:
		k += qi - p + n
		p = qi
print(k)


(h, m) = input().split()
h = int(h)
m = int(m)
task = [int(x) for x in input().split()]
time = 0
for t in range(m - 1):
	if task[t] < task[t + 1]:
		time += task[t + 1] - task[t]
	elif task[t] > task[t + 1]:
		time += h - task[t] + task[t + 1]
time += task[0] - 1
print(time)


(n, m) = [int(i) for i in input().split()]
tasks = [int(i) for i in input().split()]
time = 0
prev = 0
for task in tasks:
	if task < prev:
		time += n
	prev = task
print(time + tasks[len(tasks) - 1] - 1)


(n, m) = [int(x) for x in input().split()]
a = [1] + [int(x) for x in input().split()]
b = 0
for i in range(m):
	if a[i + 1] > a[i]:
		b += a[i + 1] - a[i]
	elif a[i + 1] < a[i]:
		b += n + a[i + 1] - a[i]
print(b)


a = input().split()
n = int(a[0])
m = int(a[1])
mat = [int(i) for i in input().split()]
currPos = 1
counter = 0
for i in mat:
	if i > currPos:
		counter += i - currPos
		currPos = i
	elif i < currPos:
		counter += n - currPos
		counter += i
		currPos = i
print(counter)


(n, m) = input().split(' ')
n = int(n)
m = int(m)
a = input().split(' ')
a = list(map(lambda x: int(x), a))
a.insert(0, 1)
ans = 0
for i in range(0, m):
	if a[i + 1] < a[i]:
		ans += n - a[i] + a[i + 1]
	else:
		ans += a[i + 1] - a[i]
print(ans)


(n, m) = map(int, input().strip().split(' '))
li = list(map(int, input().strip().split(' ')))
start = 1
time = 0
for i in li:
	if i >= start:
		time += i - start
		start = i
	else:
		time += n - (start - i)
		start = i
print(time)


number = list(map(int, input().split()))
tasks = list(map(int, input().split()))
time = tasks[0] - 1
for i in range(1, number[1]):
	if tasks[i] > tasks[i - 1]:
		time += tasks[i] - tasks[i - 1]
	if tasks[i] < tasks[i - 1]:
		time += tasks[i] + number[0] - tasks[i - 1]
	else:
		time += 0
print(time)


from sys import stdin, stdout
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
(n, m) = iia()
arr = iia()
res = 0
for i in range(1, m):
	if arr[i] < arr[i - 1]:
		res += n
res += arr[-1] - 1
print(res)


a_list = input().split()
b_list = input().split()
n = int(a_list[0])
m = int(a_list[1])
sum = int(b_list[0]) - 1
p = int(b_list[0])
for i in range(m):
	if int(b_list[i]) < p:
		sum = sum + int(b_list[i]) + n - p
		p = int(b_list[i])
	else:
		sum = sum + int(b_list[i]) - p
		p = int(b_list[i])
print(sum)


(n, m) = map(int, input().split())
l1 = [1]
l = list(map(int, input().split()))
l1.extend(l)
a = 0
for i in range(m):
	b = l1[i + 1] - l1[i]
	if b >= 0:
		a += b
	else:
		a += n + b
print(a)


(houses, tasks) = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
last = 1
for x in arr:
	if last < x:
		ans += x - last
		last = x
	elif last > x:
		ans += houses - last + x
		last = x
print(ans)


a = list(input().split())
n = int(a[0])
m = int(a[1])
b = list(input().split())
c = [1]
for i in range(m):
	c.append(int(b[i]))
sum1 = 0
for i in range(m):
	if c[i + 1] >= c[i]:
		sum1 = sum1 + (c[i + 1] - c[i])
	else:
		sum1 = sum1 + (n - c[i] + c[i + 1])
print(sum1)


(n, m) = map(int, input().split())
w = list(map(int, input().split()))
t = w[0] - 1
for q in range(1, m):
	if w[q] == w[q - 1]:
		t += 0
	elif w[q] > w[q - 1]:
		t += int(w[q]) - int(w[q - 1])
	elif w[q] < w[q - 1]:
		t += n - w[q - 1] + w[q]
print(t)


p = input().split()
n = int(p[0])
m = int(p[1])
a = input().split()
i = 0
s = []
while i < m:
	s.append(int(a[i]))
	i += 1
i = 0
j = 0
while i < m - 1:
	if s[i] > s[i + 1]:
		j += 1
	i += 1
print(j * n + s[-1] - 1)


n = input().split()
num_houses = int(n[0])
num_tasks = int(n[1])
a = list((int(i) for i in input().split()))
time = 0
for i in range(num_tasks - 1):
	if a[i + 1] < a[i]:
		time += num_houses
time += a[-1] - 1
print(time)


(n, s) = map(int, input().split())
v = list(map(int, input().split()))
v.insert(0, 1)
ans = 0
for i in range(s):
	if v[i] <= v[i + 1]:
		ans += v[i + 1] - v[i]
	else:
		ans += n - v[i] + v[i + 1]
print(ans)


t = 0

def count(x1, x2):
	global t
	global n
	if x2 >= x1:
		t += x2 - x1
	else:
		t += n - x1
		t += x2
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
lastA = 1
for i in range(0, m):
	count(lastA, a[i])
	lastA = a[i]
print(t)


(n, m) = map(int, input().split())
numbers = list(map(int, input().split()))
cycle = 0
for i in range(m - 1):
	if numbers[i] > numbers[i + 1]:
		cycle += 1
print(cycle * n + numbers[-1] - 1)


input_s = input().split(' ')
input_l = input().split(' ')
max_index = int(input_s[0])
mount = 0
before = 1
for i in range(len(input_l)):
	now = int(input_l[i])
	if now >= before:
		mount += now - before
	else:
		mount += max_index - before + now
	before = now
print(mount)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
t = 0
i = 1
off = 0
while len(a) > 0:
	u = a[0]
	a.pop(0)
	if u < i:
		t += n - i + u
	else:
		t += abs(u - i)
	i = u
	off = 0
print(t)


(n, m) = map(int, input().split())
mm = list(map(int, input().split()))
nn = [0] * n
r = 0
for i in range(m - 1):
	if mm[i] > mm[i + 1]:
		r += 1
time = r * n + mm[m - 1] - 1
print(time)


(n, m) = map(int, input().split())
lis = list(map(int, input().split()))
time = 0
initial = 1
for i in range(m):
	if lis[i] >= initial:
		time += lis[i] - initial
		initial = lis[i]
	else:
		time += n - initial + lis[i]
		initial = lis[i]
print(time)


o = input().split()
n = int(o[0])
m = int(o[1])
a = input().split()
s = 0
t = 1
for i in range(m):
	a[i] = int(a[i])
	p = (a[i] - t + n) % n
	s += p
	t += p
	t = t % n
print(s)


(n, m) = [int(s) for s in input().split(' ')]
a = [1] + [int(s) for s in input().split(' ')]
print(sum([(a[i + 1] - a[i]) % n for i in range(m)]))


(a, b) = map(int, input().split())
L = list(map(int, input().split()))
cos = L[0] - 1
for k in range(1, b):
	if L[k] > L[k - 1]:
		cos += L[k] - L[k - 1]
	elif not L[k] == L[k - 1]:
		cos += a - L[k - 1] + L[k]
print(cos)


from sys import stdin
i = [[int(y) for y in x.rstrip().split()] for x in stdin.readlines()]
n = i[0][0]
prev = 1
d = 0
for x in i[1]:
	if x < prev:
		d += n - (prev - x)
	else:
		d += x - prev
	prev = x
print(d)


s = input()
s = s.split()
n = int(s[0])
m = int(s[1])
s = input()
s = s.split()
total = int(s[0]) - 1
for i in range(1, len(s)):
	if int(s[i]) < int(s[i - 1]):
		total = total + (n - int(s[i - 1])) + int(s[i])
	elif int(s[i]) > int(s[i - 1]):
		total = total + (int(s[i]) - int(s[i - 1]))
print(total)


(n, m) = map(int, input().split())
data = [int(i) for i in input().split()]
current = 1
total = 0
step = 0
for target in data:
	if target >= current:
		step = target - current
	else:
		step = target + n - current
	total += step
	current = target
print(total)


def cin():
	return map(int, input().split())
import collections
(n, m) = cin()
a = [1] + list(cin())
ans = 0
for i in range(1, m + 1):
	ans = (ans + a[i] - a[i - 1], ans + n - a[i - 1] + a[i])[a[i] < a[i - 1]]
print(ans)


(n, m) = input().split()
n = int(n)
m = int(m)
list1 = [int(x) for x in input().split()]
prev = 1
moves = 0
for i in list1:
	if i > prev:
		moves += i - prev
	elif prev > i:
		moves += n - prev + i
	prev = i
print(moves)


(n, kj) = map(int, input().split())
arr = list(map(int, input().split()))
count = 0
for i in range(len(arr) - 1):
	if i == 0:
		count += arr[i] - 1
	if arr[i + 1] < arr[i]:
		count += n - arr[i] + arr[i + 1]
	else:
		count += arr[i + 1] - arr[i]
print(count)


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
import bisect as bi
import heapq

def I():
	return int(input())

def In():
	return map(int, input().split())

def main():
	try:
		(n, k) = In()
		l = list(In())
		last = 1
		ans = 0
		for x in l:
			if x >= last:
				ans += x - last
				last = x
			else:
				ans += n - last + 1
				last = 1
				ans += x - 1
				last = x
		print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(1):
	main()


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
result = 0
pos = 1
for i in a:
	if i < pos:
		result += n - pos
		pos = 0
	result += i - pos
	pos = i
print(result)


import math
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
count = 0
for i in range(m - 1):
	if a[i] <= a[i + 1]:
		count += int(math.fabs(a[i] - a[i + 1]))
	else:
		count += n - a[i]
		count += a[i + 1]
count += a[0] - 1
print(count)


inp_a = list(map(int, input().split()))
inp_b = list(map(int, input().split()))
n = inp_a[0]
m = inp_a[1]
pos = 1
time = 0
for i in range(len(inp_b)):
	task = inp_b[i]
	if pos < task:
		time += task - pos
		pos = task
	elif pos == task:
		pass
	else:
		time += n - pos + task
		pos = task
print(time)


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
time = 0
now = 1
for i in range(m):
	if now < arr[i]:
		time = time + arr[i] - now
		now = arr[i]
	elif now > arr[i]:
		time = time + (n - now) + arr[i]
		now = arr[i]
	else:
		pass
print(time)


s = [int(i) for i in input().split()]
n = [int(i) for i in input().split()]
t = 0
z = n[0]
t += n[0] - 1
for i in range(1, s[1]):
	if n[i] < n[i - 1]:
		t += s[0] - n[i - 1] + n[i]
	elif n[i] == n[i - 1]:
		t += 0
	else:
		t += n[i] - n[i - 1]
print(t)


(n, m) = [int(x) for x in input().split()]
vals = [int(x) for x in input().split()]
steps = 0
curr = 1
for task in vals:
	if task >= curr:
		steps += task - curr
	else:
		steps += n - curr + task
	curr = task
print(steps)


line = input()
lis = line.split()
n = int(lis[0])
m = int(lis[1])
lt = input()
lts = lt.split()
t = int(lts[0]) - 1
for i in range(1, m):
	a = int(lts[i - 1])
	b = int(lts[i])
	if b >= a:
		t += b - a
	else:
		t += n - a + b
print(t)


(n, m) = input().split()
n = int(n)
m = int(m)
a = list(map(int, input().split()))
i = 1
j = 0
count = 0
while j < m:
	if a[j] > i:
		count = count + a[j] - i
	elif a[j] < i:
		count = count + a[j] + n - i
	else:
		count = count
	i = a[j]
	j += 1
print(count)


(n, k) = input().split()
pos = 1
move = 0
order = input().split()
for i in order:
	if int(i) > pos:
		move += int(i) - pos
	elif int(i) < pos:
		move += int(n) - pos + int(i)
	pos = int(i)
print(move)


(n, m) = map(int, input().split(' '))
list1 = [int(x) for x in input().split(' ')]
cou = list1[0] - 1
for i in range(len(list1) - 1):
	if list1[i + 1] >= list1[i]:
		cou += list1[i + 1] - list1[i]
	else:
		cou += n - list1[i] + list1[i + 1]
print(cou)


import math
st = ''

def func(n, m, l1):
	t = 0
	curr = 1
	for i in range(m):
		req = l1[i]
		if req < curr:
			t += n - curr + req
		else:
			t += req - curr
		curr = req
	return t
for _ in range(1):
	(n, m) = map(int, input().split())
	l1 = list(map(int, input().split()))
	st += str(func(n, m, l1)) + '\n'
print(st)


def solve(n, m, arr):
	cnt = arr[0] - 1
	for i in range(1, m):
		if arr[i - 1] <= arr[i]:
			cnt += arr[i] - arr[i - 1]
		else:
			cnt += n - (arr[i - 1] - arr[i])
	return cnt
(n, m) = map(int, input().split(' '))
arr = list(map(int, input().split(' ')))
print(solve(n, m, arr))


nm = list(map(int, input().split()))
a = list(map(int, input().split()))
steps = 0
currient = 1
for i in a:
	if i >= currient:
		steps += i - currient
		currient = i
	else:
		steps += nm[0] - currient + i
		currient = i
print(steps)


nmStr = input()
n = int(nmStr.split()[0])
m = int(nmStr.split()[1])
aStr = input()
aSLs = aStr.split()
a = [0] * m
for i in range(m):
	a[i] = int(aSLs[i])
curPos = 1
move = 0
for i in range(m):
	if curPos > a[i]:
		move = move + (a[i] + n - curPos)
		curPos = a[i]
	else:
		move = move + (a[i] - curPos)
		curPos = a[i]
print(move)


(n, m) = map(int, input().split())
t = tuple(map(int, input().split()))
current = 1
ans = 0
for a in t:
	if current <= a:
		ans = ans + a - current
	else:
		ans = ans + n - current + a
	current = a
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
cur = 1
for i in range(0, len(a)):
	if a[i] == cur:
		pass
	else:
		ans += (n - cur + a[i]) % n
		cur = a[i]
print(ans)


import sys
from sys import stdin, stdout

def time_taken(n, m, arr):
	t = 0
	a = 1
	for b in arr:
		if b > a:
			t += b - a
			a = b
		if b < a:
			t += n - a + b
			a = b
	return t
(n, m) = map(int, input().split())
arr = [int(v) for v in input().split()]
print(time_taken(n, m, arr))


R = lambda : list(map(int, input().split()))
(n, m) = R()
a = R()
d = lambda i, j: (n - i + j) % n
print(a[0] - 1 + sum((d(a[i], a[i + 1]) for i in range(m - 1))))


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
g = l[0] - 1
for x in range(0, m - 1):
	if l[x] <= l[x + 1]:
		g = g + l[x + 1] - l[x]
	else:
		g = g + (n - l[x]) + l[x + 1]
print(g)


(n, m) = map(int, input().strip().split(' '))
a = [int(x) for x in input().strip().split(' ')]
t = 0
for i in range(0, m):
	if i == 0:
		t += a[0] - 1
		continue
	if a[i] >= a[i - 1]:
		t += a[i] - a[i - 1]
	if a[i] < a[i - 1]:
		t += n - a[i - 1] + a[i]
print(t)


(n, m) = map(int, input().split())
tasks = map(int, input().split())
cur_idx = 1
accu = 0
for t in tasks:
	if t < cur_idx:
		accu += n - cur_idx + t
		cur_idx = t
	elif t == cur_idx:
		pass
	else:
		accu += t - cur_idx
		cur_idx = t
print(accu)


class TaskB(object):

	def __init__(self):
		self.get_data()
		self.solve()
		self.print_ans()
	pass

	def get_data(self):
		(self.n, self.m) = map(int, input().split())
		self.a = list(map(int, input().split()))
		pass

	def solve(self):
		self.ans = 0
		curr = 1
		for x in self.a:
			if x >= curr:
				self.ans += x - curr
			else:
				self.ans += self.n - curr + x
				pass
			curr = x
		pass

	def print_ans(self):
		print(self.ans)
		pass

def main():
	TaskB()
	pass
main()


(n, m) = list(map(int, input().split()))
a = list(map(int, input().split()))
total_time = 0
pos = 1
for i in range(m):
	val = a[i]
	if pos <= val:
		total_time += val - pos
	else:
		total_time += n - pos + val
	pos = val
print(total_time)


(n, m) = map(int, input().split())
tasks = map(int, input().split())
time = 0
house = 1
for a in tasks:
	if a >= house:
		time += a - house
	else:
		time += n - house + a
	house = a
print(time)


S = input().split()
n = int(S[0])
b = int(S[1])
l = list(map(int, input().split()))
k = 0
j = 1
for i in range(b):
	if j <= l[i]:
		k = k + l[i] - j
		j = l[i]
	else:
		k = k + n - j + l[i]
		j = l[i]
print(k)


(house, what_do) = list(map(int, input().split()))
what_do_conkretno = list(map(int, input().split()))
answer = 0
k = 0
for i in what_do_conkretno:
	if k > i:
		answer = answer + house - k + i
	elif k == i:
		answer += 0
	else:
		answer = answer + i - k
	k = i
print(answer - 1)


def solve(n, m, a):
	steps = a[0] - 1
	for i in range(m - 1):
		step = a[i + 1] - a[i]
		if step >= 0:
			steps += step
		else:
			steps += n + step
	return steps
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
print(solve(n, m, a))


(a, b) = map(int, input().split())
l = list(map(int, input().split()))
cp = 1
time = 0
for i in l:
	if cp <= i:
		time += i - cp
	else:
		time += i + a - cp
	cp = i
print(time)


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
output = 0
home = 1
task = 0
while task < m:
	if home < l[task]:
		output += l[task] - home
		home = l[task]
	elif home > l[task]:
		output += n - home + l[task]
		home = l[task]
	else:
		task += 1
print(output)


arr = [0] * 99
arr = list(map(int, input().split()))
n = arr[0]
m = arr[1]
loc = 1
ans = 0
now = [0] * 99
now = list(map(int, input().split()))
for i in range(0, m):
	if now[i] >= loc:
		ans += now[i] - loc
	else:
		ans += n - (loc - now[i])
	loc = now[i]
print(ans)


(m, n) = input().split()
m = int(m)
n = int(n)
a = []
a = arr = [int(i) for i in input().split()]
pres = 1
nex = 1
ans = 0
for i in range(0, n):
	nex = a[i]
	if pres <= nex:
		ans = ans + (nex - pres)
		pres = nex
	elif pres > nex:
		ans = ans + (m - pres) + nex
		pres = nex
print(ans)


(n, k) = map(int, input().split())
l = list(map(int, input().split()))
e = l[0] - 1
for i in range(1, k):
	if l[i] - l[i - 1] >= 0:
		e = e + l[i] - l[i - 1]
	else:
		e = e + n + (l[i] - l[i - 1])
print(e)


def read_nums():
	return [int(x) for x in input().split()]

def main():
	(n, m) = read_nums()
	nums = [x - 1 for x in read_nums()]
	res = 0
	place = 0
	for num in nums:
		res += (num - place) % n
		place = num
	print(res)
main()


(h, w) = map(int, input().split())
now = 1
works = list(map(int, input().split()))
s = 0
for i in works:
	if i >= now:
		s += i - now
		now = i
	else:
		s += h - (now - i)
		now = i
print(s)


(n, m) = tuple(map(int, input().split()))
values = list(map(int, input().split()))
res = 0
pos = 1
for k in range(m):
	if values[k] >= pos:
		res += values[k] - pos
	else:
		res += n - pos + values[k]
	pos = values[k]
print(res)


(n, m) = tuple(map(int, input().split()))
nr = 0
a = [int(i) for i in input().split()]
ant = a[0]
nr = ant - 1
for i in a[1:]:
	if i != ant:
		if i > ant:
			nr += i - ant
			ant = i
		else:
			nr += n - ant
			ant = i
			nr += ant
print(nr)


def fun(l, n, m):
	prev = 1
	c = 0
	for i in l:
		if i >= prev:
			c += i - prev
		else:
			c += n - prev + i
		prev = i
	return c
(n, m) = list(map(int, input().split()))
l = list(map(int, input().split()))
print(fun(l, n, m))


z = input().split()
t = int(z[0])
n = int(z[1])
l = [int(i) for i in input().split()]
p = -1
for i in range(1, n):
	if l[i - 1] > l[i]:
		p = p + t
print(p + l[-1])


(n, m) = [int(i) for i in input().split()]
list1 = [int(i) for i in input().split()]
place = 1
time = 0
for i in list1:
	if i >= place:
		time += i - place
	else:
		time += i + n - place
	place = i
print(time)


a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = 0
count = 0
while x < a[1] - 1:
	if b[x] > b[x + 1]:
		count += 1
	x += 1
answer = count * a[0] + b[a[1] - 1] - 1
print(answer)


(n, m) = map(int, input().split())
z = list(map(int, input().split()))
ask = z[0] - 1
for i in range(1, len(z)):
	if z[i] >= z[i - 1]:
		ask += z[i] - z[i - 1]
	else:
		ask += n - (z[i - 1] - z[i])
print(ask)


(n, m) = map(int, input().split())
ar = list(map(int, input().split()))
c = 0
x = 1
for i in ar:
	if i < x:
		c += n - x + 1
		x = 1
	c += i - x
	x = i
print(c)


k = list(map(int, input().strip().split()))
l = list(map(int, input().strip().split()))
m = 1
count = 0
for i in range(k[1]):
	if l[i] > m:
		count = count + (l[i] - m)
	if l[i] < m:
		count = count + (k[0] - (m - l[i]))
	if l[i] == m:
		True
	m = l[i]
print(count)


(n, tasks) = map(int, input().split())
lis = list(map(int, input().split()))
times = lis[0] - 1
for i in range(1, tasks):
	if lis[i] < lis[i - 1]:
		times = times + n + lis[i] - lis[i - 1]
	else:
		times = times + lis[i] - lis[i - 1]
print(times)


a = input().split()
n = int(a[0])
m = int(a[1])
b = [int(x) for x in input().split()]
s = 1
t = 0
for i in b:
	if i >= s:
		t += i - s
	else:
		t += n + i - s
	s = i
print(t)


[n, m] = list(map(int, input().split()))
A = list(map(int, input().split()))
A = [a if a != n else 0 for a in A]
time = 0
house = 1
for a in A:
	if house <= a:
		time += abs(house - a)
	elif house > a:
		time += n - abs(house - a)
	house = a
print(time)


(n, m) = map(int, input().split())
s = list(map(int, input().split()))
k = 1
summa = 0
while len(s) >= 2:
	summa += s[0] - k
	k = s[0]
	if s[1] < s[0]:
		summa += n - s[0] + 1
		k = 1
	del s[0]
summa = s[0] - k + summa
print(summa)


(num, task) = [int(x) for x in input().split(' ')]
line = [int(x) for x in input().split(' ')]
summ = 0
for i in range(len(line)):
	if i == 0:
		summ += line[0] - 1
	elif line[i] >= line[i - 1]:
		summ += line[i] - line[i - 1]
	else:
		summ += num - line[i - 1] + line[i]
print(summ)


def main():
	(n, m) = tuple(map(int, input().rstrip().split()))
	A = tuple(map(int, input().rstrip().split()))
	(cur, cnt) = (1, 0)
	for i in A:
		if i < cur:
			cnt += 1
		cur = i
	print(cnt * n + A[-1] - 1)
main()


(houses, tasks) = map(int, input().strip().split())
taskList = list(map(int, input().strip().split()))
timeTaken = taskList[-1] - 1
for (i, task) in enumerate(taskList[1:]):
	if task < taskList[i]:
		timeTaken += houses
print(timeTaken)


n = input().split()
k = input().split()
for i in range(len(n)):
	n[i] = int(n[i])
for i in range(len(k)):
	k[i] = int(k[i])
a = k[0] - 1
for i in range(0, len(k) - 1):
	if k[i] < k[i + 1]:
		a = a + k[i + 1] - k[i]
	elif k[i] > k[i + 1]:
		a = a + n[0] - (k[i] - k[i + 1])
print(a)


(n, k) = map(int, input().split())
ar = list(map(int, input().split()))
p = 1
count = 0
for i in ar:
	count += (i - p) % n
	p = i
print(count)


(n, m) = map(int, input().split())
lista = list(map(int, input().split()))
atual = 1
ans = 0
for i in lista:
	if atual > i:
		ans += i + n - atual
	else:
		ans += i - atual
	atual = i
print(ans)


from sys import stdin, stdout

def solve(l, n, m):
	count = 0
	pos = 1
	for task in tasks:
		if task > pos:
			count += task - pos
		if task < pos:
			count += n - pos + task
		pos = task
	return count
(n, m) = list(map(int, stdin.readline().strip().split()))
tasks = list(map(int, stdin.readline().strip().split()))
print(solve(tasks, n, m))


(n, m) = map(int, input().split())
current_pos = 1
arr = list(map(int, input().split()))
time = 0
for i in range(m):
	time += (arr[i] - current_pos) % n
	current_pos = arr[i]
print(time)


(n, m) = map(int, input().split())
arr = [int(x) for x in input().split()]
houses = [x for x in range(1, n + 1)]
(ans, loc) = (0, 1)
for i in range(0, m):
	if arr[i] >= loc:
		ans += arr[i] - loc
	else:
		ans += n - loc + arr[i]
	loc = arr[i]
print(ans)


from math import factorial
(n, m) = [int(x) for x in input().split()]
ls = [1] + [int(x) for x in input().split()]
ls = [x - y for (x, y) in zip(ls[1:], ls[:-1])]
ls = [x if x >= 0 else n + x for x in ls]
print(sum(ls))


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
coo = 1
num = 0
for i in a:
	if i >= coo:
		num += i - coo
	else:
		num += i - coo + n
	coo = i
print(num)


(n, m) = input().split()
n = int(n)
m = int(m)
a = input().split()
b = []
for i in a:
	b.append(int(i))
cycle = 0
k = 0
for i in range(m):
	if b[i] < max(b[k:i + 1]):
		cycle = cycle + 1
		k = i
t = cycle * n + b[-1] - 1
print(t)


(n, m) = [int(i) for i in input().split()]
lst = [int(i) for i in input().split()]
j = 1
nd = 0
for i in lst:
	if i >= j:
		nd = nd + (i - j)
		j = i
	else:
		nd = nd + (n - j) + i
		j = i
print(nd)


(n, m) = map(int, input().split())
l = [1]
L = list(map(int, input().split()))
l.extend(L)
ans = 0
for i in range(1, len(l)):
	if l[i] >= l[i - 1]:
		ans += l[i] - l[i - 1]
	else:
		ans += n - l[i - 1] + 1
		ans += l[i] - 1
print(ans)


(n, m) = [int(j) for j in input().split()]
a = [int(j) for j in input().split()]
c = a[0] - 1
prev = a[0]
for i in range(1, m):
	if a[i] != prev:
		if a[i] > prev:
			c += a[i] - prev
		elif a[i] < prev:
			c += n - prev + a[i]
	prev = a[i]
print(c)


import sys

def _dist_(a, b, length):
	if a <= b:
		return b - a
	else:
		return length - a + b
(road_length, n) = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
start = 1
for i in range(0, len(v)):
	ans += _dist_(start, v[i], road_length)
	start = v[i]
print(ans)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
rounds = 0
for i in range(m - 1):
	if tasks[i] > tasks[i + 1]:
		rounds += 1
print(n * rounds + tasks[-1] - 1)


def main():
	(n, m) = map(int, input().split())
	arr = [int(x) for x in input().split()]
	dist = 0
	curr = 1
	for x in arr:
		if x >= curr:
			dist += x - curr
		else:
			dist += n - curr + x
		curr = x
	print(dist)
	return
main()


import math
from collections import defaultdict, Counter, deque
INF = float('inf')

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactor(n):
	if n % 2 == 0:
		return 2
	i = 3
	while i ** 2 <= n:
		if n % i == 0:
			return i
		i += 1
	return n

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def main():
	(n, m) = vars()
	arr = array()
	ti = 0
	p = 1
	for i in range(m):
		if arr[i] < p:
			ti += n - (p - arr[i])
		else:
			ti += arr[i] - p
		p = arr[i]
	print(ti)
t = 1
for _ in range(t):
	main()


k = list(map(int, input().rstrip().split()))
a = list(map(int, input().rstrip().split()))
n = k[0]
m = k[1]
prev = 1
time = 0
for i in range(m):
	if a[i] > prev:
		time += a[i] - prev
		prev = a[i]
	elif a[i] < prev:
		time += n + a[i] - prev
		prev = a[i]
	elif a[i] == prev:
		time += 0
print(time)


(n, m) = [int(i) for i in input().split()]
ai = [int(i) for i in input().split()]
ai.insert(0, 1)
s = 0
for i in range(1, m + 1):
	if ai[i - 1] <= ai[i]:
		s += ai[i] - ai[i - 1]
	else:
		s += n + ai[i] - ai[i - 1]
print(s)


(n, m) = list(map(int, input().split()))
A = list(map(int, input().split()))
res = 0
prev = 1
for a in A:
	if prev <= a:
		res += a - prev
	else:
		res += n + a - prev
	prev = a
print(res)


def f(x, n):
	while x < 0:
		x += n
	return x
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
for i in range(m):
	if i == 0:
		a = f(l[i] - 1, n)
	else:
		a += f(l[i] - l[i - 1], n)
print(a)


(N, M) = map(int, input().split())
tasks = list(map(int, input().split()))
add = tasks[0] - 1
for i in range(1, M):
	if tasks[i] >= tasks[i - 1]:
		add += tasks[i] - tasks[i - 1]
	else:
		add += N - tasks[i - 1] + tasks[i]
print(add)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
temp = a[0]
cnt = 0
for x in a[1:]:
	if temp == x:
		pass
	elif temp < x and temp != 1:
		cnt += x - temp
	elif temp == 1:
		cnt += x - 1
	else:
		cnt += n - temp + x
	temp = x
print(a[0] - 1 + cnt)


(n, m) = [int(x) for x in input().split()]
arr = [int(X) for X in input().split()]
prev = 1
ans = 0
for i in arr:
	if prev > i:
		ans += n - prev + i
	else:
		ans += i - prev
	prev = i
print(ans)


(a, b) = map(int, input().split())
c = [int(x) for x in input().split()]
count = c[0] - 1
for i in range(1, b):
	if c[i] - c[i - 1] > 0:
		count += c[i] - c[i - 1]
	elif c[i] - c[i - 1] == 0:
		continue
	else:
		count += a - c[i - 1] + c[i]
print(count)


(n, m) = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
now = 1
sum = 0
for i in range(m):
	if a[i] >= now:
		sum += a[i] - now
		now = a[i]
	else:
		sum += a[i] + n - now
		now = a[i]
print(sum)


(n, m) = map(int, input().split())
L = list(map(int, input().split()))
pos = 1
ans = 0
for i in range(m):
	if L[i] == pos:
		continue
	elif L[i] > pos:
		ans += L[i] - pos
	else:
		ans += L[i] + n - pos
	pos = L[i]
print(ans)


(n, m) = map(int, input().split())
tasks = list(map(int, input().split()))
t = 0
y = 1
for x in range(m):
	if y < tasks[x]:
		t += tasks[x] - y
		y = tasks[x]
	elif y > tasks[x]:
		t += n - y + tasks[x]
		y = tasks[x]
print(t)


import pdb

def read_input(n):
	in_args = []
	for i in range(n):
		in_args.append(input())
	return in_args

def task_timer(n, m, a):
	count = 0
	for i in range(1, m):
		if a[i - 1] > a[i]:
			count += 1
	res = count * n + a[-1] - 1
	return res

def main():
	inputs = read_input(2)
	n_m = inputs[0].split(' ')
	n = int(n_m[0])
	m = int(n_m[1])
	a = [int(i) for i in inputs[1].split(' ')]
	print(str(task_timer(n, m, a)))
main()


(n, m) = [int(i) for i in input().split()]
t = [int(i) for i in input().split()]
t = [1] + t
sum = 0
for i in range(1, len(t)):
	sum += (t[i] - t[i - 1]) % n
print(sum)


l1 = input()
l2 = input()
a1 = l1.split()
a2 = l2.split()
n = int(a1[0])
m = int(a1[1])
i = 1
sum = int(a2[0]) - 1
while i < m:
	if int(a2[i]) >= int(a2[i - 1]):
		sum += int(a2[i]) - int(a2[i - 1])
	else:
		sum += n + int(a2[i]) - int(a2[i - 1])
	i += 1
print(sum)


(n, m) = map(int, input().split())
seq = [1] + [int(i) for i in input().split()]
total = seq[-1]
for i in range(m):
	if seq[i + 1] < seq[i]:
		total += n
	else:
		continue
print('{}'.format(total - 1))


(n, m) = map(int, input().split())
li = [int(x) for x in input().split()]
res = 0
curpos = 1
for i in li:
	if i - curpos < 0:
		res += n - curpos + i
	elif i - curpos > 0:
		res += i - curpos
	curpos = i
print(res)


(n, m) = input().split()
l = input().split()
n = int(n)
m = int(m)
i = 0
quansu = 0
while i < m - 1:
	if int(l[i]) <= int(l[i + 1]):
		i = i + 1
	else:
		quansu = quansu + 1
		i = i + 1
s = n * quansu + int(l[-1]) - 1
print(s)


a = input().split(' ')
n = int(a[0])
m = int(a[1])
h = [1] * n
for x in range(n):
	h[x] = x + 1
b = input().split(' ')
for x in range(m):
	b[x] = int(b[x])
c = 0
p = 1
for x in range(m):
	if b[x] < p:
		c += n - p + 1
		p = 1
	elif b[x] == p:
		continue
	c += b[x] - p
	p = b[x]
print(c)


(N, M) = map(int, input().split())
A = list(map(lambda v: int(v) - 1, input().split()))
cnt = 0
now = 0
for i in range(0, M):
	next = A[i]
	if next < now:
		next += N
	cnt += next - now
	now = A[i]
print(cnt)


(q_houses, q_occasions) = map(int, input().split())
occasions = [int(a) for a in input().split()]
house_before = 1
time = 0
for i in range(q_occasions):
	house_now = occasions[i]
	if house_now < house_before:
		time += q_houses - house_before + house_now
	else:
		time += house_now - house_before
	house_before = house_now
print(time)


from sys import stdin, exit
(n, m) = map(int, stdin.readline().rstrip().split(' '))
jobs = list(map(int, stdin.readline().rstrip().split(' ')))
cur = 1
cur_job = jobs[0]
moves = 0
for i in range(m + 1):
	if cur > cur_job:
		moves += n - cur + cur_job
	else:
		moves += cur_job - cur
	cur = cur_job
	if i != m:
		cur_job = jobs[i]
print(moves)


(n, k) = list(map(int, input().split()))
s = list(map(int, input().split()))
count = s[0] - 1
for i in range(1, k):
	count += s[i] - s[i - 1]
	if s[i] < s[i - 1]:
		count += n
print(count)


(n, k) = [int(i) for i in input().split()]
m = list(map(int, input().split()))[:k]
co = m[0] - 1
for i in range(len(m) - 1):
	if m[i] > m[i + 1]:
		co += n - m[i] + m[i + 1]
	elif m[i] < m[i + 1]:
		co += m[i + 1] - m[i]
print(co)


(n, m) = str(input()).split()
n = int(n)
m = int(m)
temp_list = str(input()).split()
temp_list = [int(it) for it in temp_list]
value = 0
past = 1
for i in range(m):
	if temp_list[i] < past:
		value += n
	value += temp_list[i] - past
	past = temp_list[i]
print(value)


import sys
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
x = 1
ans = 0
for y in a:
	if y >= x:
		ans += y - x
	else:
		ans += n - x + y
	x = y
print(ans)


import sys

def main():
	(n, m) = map(int, sys.stdin.readline().split())
	houses = list(map(int, sys.stdin.readline().split()))
	ans = 0
	start = 1
	aux = 0
	for i in houses:
		if start < i:
			aux = i - start
			ans += aux
			start += aux
		elif start > i:
			aux = n - start
			ans += aux
			start **= 0
			ans += 1
			aux = i - start
			ans += aux
			start += aux
	sys.stdout.write(f'{ans}')
main()


def xenia_and_ringroad(n_int, m_int, tasks_list) -> int:
	start = 1
	count = 0
	for each in tasks_list:
		if each < start:
			count += each + n_int - start
		else:
			count += each - start
		start = each
	return count
(n, m) = list(map(int, input().split()))
tasks = list(map(int, input().split()))
print(xenia_and_ringroad(n, m, tasks))


(n, l) = list(map(int, input().split(' ')))
arr = list(map(int, input().split(' ')))
now = 1
s = 0
for x in arr:
	if x < now:
		s += abs(n - now + x)
	else:
		s += abs(x - now)
	now = x
print(s)


(n, m) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
ttime = 0
pre = 1
for i in a:
	if i == pre:
		ttime += 0
	if i > pre:
		ttime += i - pre
	if i < pre:
		ttime += n + i - pre
	pre = i
print(ttime)


n = int(input().split(' ')[0])
errands = [int(x) - 1 for x in input().split()]
time = errands[0]
for (i, e) in enumerate(errands):
	if i == 0:
		continue
	time += e - errands[i - 1]
	if e < errands[i - 1]:
		time += n
print(time)