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