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