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