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