#include <bits/stdc++.h>
int main() {
  long long int n, m, k, c = 0, ans = 0;
  scanf("%lld%lld", &n, &m);
  while (m) {
    scanf("%lld", &k);
    if (k > c) {
      ans = ans + (k - c);
    } else if (k == c) {
    } else {
      ans = ans + (n - c + k);
    }
    c = k;
    m--;
  }
  printf("%lld", ans - 1);
  return 0;
}
