#include <iostream>
#include <algorithm>
#include <deque>
#include <math.h>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
int cnt = 0;
cin >> n >> m;
deque<int> de;
for (int i = 0; i < n; ++i)
{
de.push_back(i + 1);
}
int current = 0;
int index;
for (int i = 0; i < m; ++i)
{
int num;
cin >> num;
for (int j = 0; j < de.size(); ++j)
{
if (de[j] == num)
{
index = j;
break;
}
}
int an = abs(current - index);
if (an <= de.size() - an)
{
while (true)
{
if (de.front() == num)
{
de.pop_front();
break;
}
cnt++;
de.push_back(de.front());
de.pop_front();
}
}
else
{
while (true)
{
if (de.front() == num)
{
de.pop_front();
break;
}
cnt++;
de.push_front(de.back());
de.pop_back();
}
}
}
cout << cnt;
}