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