#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, m, p, q = 1;
  cin >> n >> m;
  long long int ans = 0;
  for (int i = 0; i < m; ++i) {
    cin >> p;
    if (p == q) continue;
    if (p > q) {
      ans += p - q;
      q = p;
    } else {
      ans += n - q + p;
      q = p;
    }
  }
  cout << ans << endl;
  return 0;
}
