#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, a[100000], min_time = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) cin >> a[i];
  min_time = a[0] - 1;
  for (int i = 0; i < m - 1; i++) {
    if (a[i] <= a[i + 1])
      min_time += a[i + 1] - a[i];
    else
      min_time += n - a[i] + a[i + 1];
  }
  cout << min_time;
}
