class Deque():
def __init__(self, size):
self.queue = [0 for x in range(size + 1)]
self.front_flag = 0
self.rear = 0
self.max_size = size
self.list_size = size + 1
def push_back(self, num):
if (self.rear + 1) % self.list_size == self.front_flag:
return -1
self.queue[self.rear] = num
self.rear = (self.rear +1) % self.list_size
def push_front(self, num):
if (self.rear + 1) % self.list_size == self.front_flag:
return -1
self.front_flag = self.list_size - 1 if self.front_flag == 0 else self.front_flag - 1
self.queue[self.front_flag] = num
def pop_front(self):
if self.size() == 0:
return -1
temp = self.queue[self.front_flag]
self.front_flag = (self.front_flag + 1) % self.list_size
return temp
def pop_back(self):
if self.size() == 0:
return -1
self.rear = self.list_size - 1 if self.rear == 0 else self.rear - 1
return self.queue[self.rear]
def size(self):
if self.front_flag == self.rear:
return 0
elif self.rear > self.front_flag:
return self.rear - self.front_flag
else:
return self.max_size - (self.front_flag - self.rear) + 1
def empty(self):
return 1 if self.front_flag == self.rear else 0
def front(self):
if self.size() == 0:
return -1
return self.queue[self.front_flag]
def back(self):
if self.size() == 0:
return -1
return self.queue[self.rear - 1]
def find(self, num):
index = 0
i = self.front_flag
while(True):
if self.queue[i] == num:
return index
index = index + 1
i = (i + 1) % self.list_size
if i == self.rear:
break
n, m = map(int, input().split())
deque = Deque(n)
temp = list(map(int, input().split()))
for i in range(n):
deque.push_back(i+1)
#이동 회수를 저장하기 위한 변수
count = 0
for i in range(m):
# 찾아야 되는 숫자의 현재 위치
index = deque.find(temp[i])
mid = (n - i) / 2
if index <= mid or (n-i) <= 2:
#앞쪽임
while(deque.front() != temp[i]):
deque.push_back(deque.pop_front())
count += 1
else:
#뒤쪽임
while(deque.front() != temp[i]):
deque.push_front(deque.pop_back())
count += 1
deque.pop_front()
print(count)