#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, cnt = 0, b = 1;
  cin >> n >> m;
  while (m--) {
    long long int a;
    cin >> a;
    if (a < b) {
      cnt += n + a - b;
    } else {
      cnt += a - b;
    }
    b = a;
  }
  cout << cnt << endl;
}
