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