#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  long long int m;
  cin >> n >> m;
  long long int curr = 1;
  long long int sum = 0;
  for (long long int i = 0; i < m; i++) {
    long long int x;
    cin >> x;
    if (x > curr) {
      sum += x - curr;
      curr = x;
    } else if (curr > x) {
      sum += n - (curr - x);
      curr = x;
    }
  }
  cout << sum << endl;
  return 0;
}
