import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class 실험실 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int len = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
LinkedList<Integer> list = new LinkedList<>();
for (int i = 1; i <= len; i++) {
list.add(i);
}
int temp = 0;
int count = 0;
st = new StringTokenizer(br.readLine());
for (int i = 0; i < M; i++) {
temp = Integer.parseInt(st.nextToken());
if (list.indexOf(temp) <= list.size() / 2) {
while(temp != list.getFirst()) {
list.addLast(list.removeFirst());
count ++;
}
}else {
while(temp != list.getFirst()) {
list.addFirst(list.removeLast());
count ++;
}
}
list.removeFirst();
}
System.out.print(count);
}
}