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