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