#include <bits/stdc++.h>
int main() {
  long long int n, m, i, t = 0;
  scanf("%lld %lld", &n, &m);
  long long int arr[m];
  for (i = 0; i < m; i++) {
    scanf("%lld", &arr[i]);
  }
  t = arr[0] - 1;
  for (i = 0; i < m - 1; i++) {
    if (arr[i] <= arr[i + 1]) {
      t += (arr[i + 1] - arr[i]);
    } else {
      t += n - arr[i];
      t++;
      t += arr[i + 1] - 1;
    }
  }
  printf("%lld", t);
  return 0;
}
