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