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