#include <bits/stdc++.h>
using namespace std;
std::vector<long long> v;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long n, m;
  cin >> n >> m;
  v.push_back(-1);
  for (long long i = 1; i <= m; ++i) {
    long long x;
    cin >> x;
    v.push_back(x);
  }
  long long temp = 1;
  long long count = 0;
  for (long long i = 1; i <= m; ++i) {
    if (temp == v[i])
      continue;
    else if (temp < v[i]) {
      count = count + (v[i] - temp);
      temp = v[i];
    } else {
      count = count + (n - temp) + v[i];
      temp = v[i];
    }
  }
  cout << count;
  return 0;
}
