#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int n, m, time, a[100000];
  cin >> n >> m;
  a[0] = 1;
  for (long long int i = 1; i < m + 1; i++) {
    cin >> a[i];
    if (a[i] >= a[i - 1])
      time += a[i] - a[i - 1];
    else
      time += (n - a[i - 1] + a[i]);
  }
  cout << time << '\n';
  cerr << "Time : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC
       << "ms\n";
  ;
  return 0;
}
