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