#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long i, j, k, a, b, c, l, m, n, p, q, r, s, t = 0, x, y, z, sum = 0,
                                                   cnt = 0;
  cin >> n >> m;
  int B[m];
  for (i = 0; i < m; i++) {
    cin >> B[i];
  }
  for (int i = 1; i < m; i++) {
    if (B[i] > B[i - 1]) {
      sum += (B[i] - B[i - 1]);
    } else if (B[i] < B[i - 1]) {
      sum += (n - (B[i - 1] - B[i]));
    }
  }
  sum += (B[0] - 1);
  cout << sum << endl;
  return 0;
}
