#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int c = 1;
  long long uni = 0;
  for (int u = 0; u < m; u++) {
    int i;
    cin >> i;
    if (i >= c) {
      uni += i - c;
    } else {
      uni += (n - c) + (i);
    }
    c = i;
  }
  cout << uni << "\n";
  return 0;
}
