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