#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, l(1);
  long long c(0);
  cin >> n >> m;
  int Arr;
  while (m-- != 0) {
    cin >> Arr;
    c += (Arr - l + n) % n;
    l = Arr;
  }
  cout << c << endl;
}
