#include <iostream>
#include <vector>
#include <deque>
#define MAX 10
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, b, c, i, tmp;
cin >> n >> m;
deque<int> dq; // 양방향 순환 큐
deque<int> v; // 뽑아내려고 하는 수
for (i = 0; i < m; i++) {
cin >> tmp;
v.push_back(tmp);
}
for (i = 1; i <= n; i++) dq.push_back(i);
b = 0, c = 0;
while (!v.empty()) {
if (dq.front() == v.front()) {
dq.pop_front();
v.pop_front(); // 처리 완료
}
else {
int idx = 0;
while (idx < dq.size()) {
if (dq.at(idx) == v.front()) break;
else idx++;
}
int left = idx; // b
int right = dq.size() - left; // c
if (left <= right) {
for (i = 0; i < left; i++) {
int front = dq.front();
dq.pop_front();
dq.push_back(front);
}
b += left;
}
else {
for (i = 0; i < right; i++) {
int back = dq.back();
dq.pop_back();
dq.push_front(back);
}
c += right;
}
}
}
cout << b + c << endl;
return 0;
}