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