#include <bits/stdc++.h>
using namespace std;
void Time() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  Time();
  long long n, m, counter = 1, house = 0;
  scanf("%I64d", &n);
  ;
  scanf("%I64d", &m);
  ;
  long long arr[m];
  for (int i = 0; i < m; i++) {
    scanf("%I64d", &arr[i]);
    ;
  }
  for (int i = 0; i < m; i++) {
    if (arr[i] >= counter) {
      house += arr[i] - counter;
      counter = arr[i];
    } else if (arr[i] < counter) {
      house += (arr[i]) + (n - counter);
      counter = arr[i];
    }
  }
  cout << house << endl;
  return 0;
}
