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