#include <bits/stdc++.h>
int main() {
  int m, n, i, s = 1, j;
  unsigned long long int total = 0;
  scanf("%d %d", &n, &m);
  int arry[m];
  scanf("%d", &arry[0]);
  total = arry[0] - s;
  for (i = 1; i < m; i++) {
    scanf("%d", &arry[i]);
    if (arry[i] < arry[i - 1]) {
      total = total + n - arry[i - 1];
      total++;
      total = total + arry[i] - 1;
    } else {
      total = total + arry[i] - arry[i - 1];
    }
  }
  printf("%I64d", total);
}
