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