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