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