#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> values(m);
  for (auto &value : values) cin >> value;
  unsigned long long int result = values[0] - 1;
  for (int i = 1; i < values.size(); ++i) {
    if (values[i] < values[i - 1]) {
      result += (n - values[i - 1] + values[i]);
    } else {
      result += (values[i] - values[i - 1]);
    }
  }
  cout << result << endl;
}
int main() {
  int numTests = 1;
  while (numTests--) solve();
}
