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