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