#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, ans = 0, last, i, temp;
  cin >> n >> m;
  long long int a[m];
  for (i = 0; i < m; i++) {
    cin >> temp;
    if (i == 0) {
      ans += temp - 1;
      last = temp;
    } else {
      if (temp >= last) {
        ans += temp - last;
        last = temp;
      } else {
        ans += n - last + temp;
        last = temp;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
