#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, a0, a1;
  cin >> n >> m;
  a0 = 1;
  long long sum = 0;
  for (int i = 0; i < m; i++) {
    cin >> a1;
    sum += (a1 + n - a0) % n;
    a0 = a1;
  }
  cout << sum;
}
