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