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