#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  long long ans = 0;
  int step = 1;
  for (int i = 1; i <= M; ++i) {
    int T;
    cin >> T;
    if (T >= step) {
      ans += T - step;
    } else {
      ans += N - step + T;
    }
    step = T;
  }
  cout << ans << "\n";
  return 0;
}
