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